using Entries;
using NativeHelpers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Core;
using Windows.Management.Deployment;
namespace ShortcutUtil
{
public partial class MainForm : Form
{
private EntryList entryList;
private Font defaultFont;
private Font nameFont;
private Brush fontBrush;
private AppResolver appResolver;
private EntryListItem listLayout;
public MainForm(EntryList entryList, AppResolver appResolver)
{
InitializeComponent();
typeof(ListBox).InvokeMember("DoubleBuffered",
BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, entryListBox, new object[] { true });
DoubleBuffered = true;
Text = entryList.FilePath == null ? "New shortcut file" : entryList.FilePath;
defaultFont = defaultShortcutLabel.Font;
nameFont = new Font(defaultFont, FontStyle.Bold);
fontBrush = new SolidBrush(defaultShortcutLabel.ForeColor);
this.entryList = entryList;
this.appResolver = appResolver;
entryListBox.Items.AddRange(this.entryList.Entries.ToArray());
if (this.entryList.Entries.Count > 0)
{
entryListBox.SelectedIndex = 0;
}
listLayout = new EntryListItem(entryListBox);
}
private Dictionary<Tuple<string, int, Size>, Icon> iconsByFileIndex = new Dictionary<Tuple<string, int, Size>, Icon>();
private Icon GetIcon(string file, int index, Size preferred)
{
Tuple<string, int, Size> key = new Tuple<string, int, Size>(file, index, preferred);
if (!iconsByFileIndex.ContainsKey(key))
{
Icon icon = null;
try
{
icon = new Icon(file, preferred);
}
catch (Exception)
{
icon = null;
}
if (icon == null)
{
try
{
icon = IconExtractor.Extract(file, index, true);
}
catch (Exception)
{
icon = null;
}
}
iconsByFileIndex.Add(key, icon);
/*
if (icon == null)
{
MessageBox.Show("Couldn't load icon " + index + " from " + file);
}
*/
}
return iconsByFileIndex[key];
}
private static readonly Size IconSize = new Size(64, 64);
private static readonly Size ItemPadding = new Size(6, 6);
private void entryListBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
listLayout.LayoutFromList();
if (e.Index >= 0 && e.Index < entryList.Entries.Count)
{
Rectangle iconBounds = new Rectangle(Point.Add(e.Bounds.Location, ItemPadding), IconSize);
Size textSize = new Size(0, nameFont.Height + 2);
Entry entry = entryList.Entries[e.Index];
Icon icon = GetIcon(entry.Icon, entry.IconIndex, IconSize);
if (icon != null)
{
e.Graphics.DrawIcon(icon, iconBounds);
}
PointF textLocation = PointF.Add(e.Bounds.Location, new Size(IconSize.Width + ItemPadding.Width * 2, ItemPadding.Width));
e.Graphics.DrawString(e.Index + ": " + entry.Name, nameFont, fontBrush, textLocation);
textLocation = PointF.Add(textLocation, textSize);
e.Graphics.DrawString(entry.Target, defaultFont, fontBrush, textLocation);
textLocation = PointF.Add(textLocation, textSize);
e.Graphics.DrawString(entry.Description, defaultFont, fontBrush, textLocation);
textLocation = PointF.Add(textLocation, textSize);
listLayout.TestButton.RenderToItem(e);
}
}
private void entryListBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = GetItemSize().Height;
}
public Size GetItemSize()
{
return Size.Add(IconSize, new Size(0, ItemPadding.Height * 2));
}
private SearchForm search = new SearchForm();
private async void appButton_Click(object sender, EventArgs e)
{
appButton.Enabled = false;
PackageManager pm = new PackageManager();
List<Tuple<string, string>> options = new List<Tuple<string, string>>();
foreach (Package p in pm.FindPackagesForUser(string.Empty))
{
var apps = await p.GetAppListEntriesAsync().AsTask();
foreach (AppListEntry app in apps)
{
options.Add(new Tuple<string, string>(app.AppUserModelId, app.DisplayInfo.DisplayName + ": " + app.AppUserModelId));
}
}
string selectedAppUserModelId = search.GetSelection("Select UWP App", "Available Apps:", options);
if (selectedAppUserModelId != null)
{
appUserModelIdBox.Text = selectedAppUserModelId;
}
appButton.Enabled = true;
}
private static bool GetWindowHandle(IntPtr windowHandle, ref object windowHandles)
{
((ArrayList)windowHandles).Add(windowHandle);
return true;
}
private void hwndButton_Click(object sender, EventArgs e)
{
hwndButton.Enabled = false;
object handles = new ArrayList();
NativeCalls.EnumWindows(GetWindowHandle, ref handles);
List<Tuple<IntPtr, string, string>> titles = new List<Tuple<IntPtr, string, string>>();
foreach (object ptrObj in (ArrayList)handles)
{
IntPtr ptr = (IntPtr)ptrObj;
if (NativeCalls.IsAltTabWindow(ptr))
{
string title = NativeCalls.GetWindowTextManaged(ptr);
if (title != "")
{
/*
IPropertyStore store;
Guid guid = NativeValues.IID_IPropertyStore;
NativeCalls.VerifySucceeded((uint)NativeCalls.SHGetPropertyStoreForWindow(ptr, ref guid, out store));
using (PropVariant pv = new PropVariant())
{
NativeCalls.VerifySucceeded(store.GetValue(NativeValues.AppUserModelIDKey, pv));
string appUserModelId = pv.Value;
if (appUserModelId == null)
{
uint processId;
NativeCalls.GetWindowThreadProcessId(ptr, out processId);
IntPtr handle = NativeCalls.OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, (int)processId);
try
{
appUserModelId = NativeCalls.GetApplicationUserModelIdManaged(handle);
}
catch (Exception) { }
}
}
*/
string appUserModelId = appResolver.GetAppIDForWindow(ptr);
if (appUserModelId == null)
{
uint processId;
NativeCalls.GetWindowThreadProcessId(ptr, out processId);
appUserModelId = appResolver.GetAppIDForProcess(processId);
}
if (appUserModelId != null)
{
titles.Add(new Tuple<IntPtr, string, string>(ptr, title, appUserModelId == null ? "NULL" : appUserModelId));
}
}
}
}
string selectedAppUserModelId = search.GetSelection("Select Window", "Available Windows:", titles.Select(t => new Tuple<string, string>(t.Item3, t.Item2 + ": " + t.Item3)).ToList());
if (selectedAppUserModelId != null)
{
appUserModelIdBox.Text = selectedAppUserModelId;
}
hwndButton.Enabled = true;
}
}
}