using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ShortcutUtil { public interface IListBoxControl { void Hover(); void Unhover(); } public class ListBoxButton : Button, IListBoxControl { public void Hover() { OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0)); } public void Unhover() { OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0)); } } public class ListBoxControl<T> where T : Control, IListBoxControl, new() { public T Control { get; private set; } private ListBox listBox; public int? IndexOver { get; private set; } private MethodInfo refreshItem; public ListBoxControl(T control, ListBox listBox) { Control = control; this.listBox = listBox; listBox.MouseMove += ListBoxMouseMove; refreshItem = listBox.GetType().GetMethod("RefreshItem", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(int) }, null); } private void RefreshItem(int index) { listBox.BeginUpdate(); object item = listBox.Items[index]; listBox.Items.RemoveAt(index); listBox.Items.Insert(index, item); listBox.EndUpdate(); //listBox.Items[index] = item; //listBox.Items[index] = //if (refreshItem != null) refreshItem.Invoke(listBox, new object[] { index }); } public Rectangle BoundsInList(int index) { Point offsetLoc = listBox.GetItemRectangle(index).Location; offsetLoc.Offset(Control.Location); return new Rectangle(offsetLoc, Control.Size); } private void ListBoxMouseMove(object sender, MouseEventArgs e) { int? nowIndexOver = null; for (int i = 0; i < listBox.Items.Count; i++) { if (BoundsInList(i).Contains(e.X, e.Y)) { nowIndexOver = i; break; } } if (nowIndexOver != IndexOver) { if (IndexOver.HasValue) RefreshItem(IndexOver.Value); IndexOver = nowIndexOver; if (IndexOver.HasValue) RefreshItem(IndexOver.Value); } } public void RenderToItem(DrawItemEventArgs e) { using (Bitmap b = new Bitmap(Control.Size.Width, Control.Size.Height)) { if (e.Index == IndexOver) { Control.Hover(); } Control.DrawToBitmap(b, new Rectangle(new Point(), Control.Size)); Point offsetLoc = e.Bounds.Location; offsetLoc.Offset(Control.Location); e.Graphics.DrawImage(b, offsetLoc.X, offsetLoc.Y); if (e.Index == IndexOver) { Control.Unhover(); } } } } }