using Entries; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ShortcutUtil { public partial class MainForm : Form { private EntryList entryList; private Font defaultFont; private Font nameFont; private Brush fontBrush; private bool shown = false; private Size listSizeOffset; private Point buttonOffset; public MainForm(EntryList entryList) { InitializeComponent(); defaultFont = label1.Font; nameFont = new Font(defaultFont, FontStyle.Bold); fontBrush = new SolidBrush(label1.ForeColor); this.entryList = entryList; entryListBox.Items.AddRange(this.entryList.Entries.ToArray()); } 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); if (icon != null) { iconsByFileIndex.Add(key, icon); return icon; } } catch (Exception) { } try { icon = IconExtractor.Extract(file, index, true); if (icon != null) { iconsByFileIndex.Add(key, icon); return icon; } } catch (Exception) { } MessageBox.Show("Couldn't load icon " + index + " from " + file); return null; } 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(); 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]; e.Graphics.DrawIcon(GetIcon(entry.Icon, entry.IconIndex, IconSize), 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); } } private void entryListBox_MeasureItem(object sender, MeasureItemEventArgs e) { e.ItemHeight = Size.Add(IconSize, new Size(0, ItemPadding.Height * 2)).Height; } private void MainForm_SizeChanged(object sender, EventArgs e) { if (shown) { entryListBox.Size = Size.Subtract(Size, listSizeOffset); createButton.Location = Point.Add(buttonOffset, entryListBox.Size); } } private void MainForm_Shown(object sender, EventArgs e) { listSizeOffset = Size.Subtract(Size, entryListBox.Size); buttonOffset = Point.Subtract(createButton.Location, entryListBox.Size); shown = true; } } }