Решение заключается в обыкновенном создании класса наследника класса ListBox и использовании нескольких API функций.
Код | using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices;
public class ToolTipListBox : System.Windows.Forms.ListBox { [StructLayout(LayoutKind.Sequential)] public struct SIZE { public int cx; public int cy; } [DllImport("gdi32.dll")] public static extern int GetTextExtentPoint32(IntPtr hdc, String str, int len, ref SIZE size);
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hWnd,IntPtr hdc);
private ToolTip tp = new ToolTip(); private int LastIndex = -1;
public ToolTipListBox() { tp.InitialDelay = 500; tp.ReshowDelay = 500; tp.AutoPopDelay = 3000; tp.Active = true; }
protected override void OnMouseMove( System.Windows.Forms.MouseEventArgs e) { /* Получаем индекс элемента на котором находится курсор */ int index = IndexFromPoint(e.X,e.Y);
if(index != ListBox.NoMatches ) { if( LastIndex != index ) { string s = Items[index].ToString();
IntPtr hdc = GetDC(this.Handle); SIZE size; size.cx = 0; size.cy = 0; GetTextExtentPoint32(hdc,s,s.Length,ref size); ReleaseDC(this.Handle,hdc);
/* показываем ToolTip только в том случае если содержимое элемента выходит за рамки listBox`а */ if(this.Width < size.cx) tp.SetToolTip(this,s);
LastIndex = index; } } }
}
|
|