(转)ComboBox设置ToolTip功能
原文:http://topic.csdn.net/u/20070911/11/c3f63685-f549-47b4-a903-ac24c7d2e918.html
功能:
在点开ComboBox下拉筐的的时候,鼠标移动到每一个选项时显示相对应的ToolTip
class ComboBoxEx : ComboBox
{
private SubWindow m_SubWindow;
public ComboBoxEx()
{
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x210 && (int)m.WParam == 0x3e80001)
{
SubWindow sw = new SubWindow();
sw.Owner = this;
sw.AssignHandle(m.LParam);
this.m_SubWindow = sw;
}
base.WndProc(ref m);
}
protected override void Dispose(bool disposing)
{
if (disposing && this.m_SubWindow != null)
{
this.m_SubWindow.DestroyHandle();
}
base.Dispose(disposing);
}
}
public class SubWindow : NativeWindow
{
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}
[DllImport( "user32.dll ", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] POINT pt, int cPoints);
[DllImport( "user32.dll ", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport( "user32.dll ", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);
private int m_Index;
private ToolTip toolTip;
private Control m_Owner;
public SubWindow()
{
this.m_Index = -1;
this.toolTip = new ToolTip();
}
public Control Owner
{
get { return m_Owner; }
set { m_Owner = value; }
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x200)
{
Point msPoint = Cursor.Position;
POINT pt = new POINT(msPoint.X, msPoint.Y);
MapWindowPoints(IntPtr.Zero, this.Handle, pt, 1);
int index = SendMessage(m.HWnd, 0x1a9, 0, (pt.y < < 0x10) | (pt.x & 0xffff));
if (((index > > 0x10) & 0xffff) == 0)
{
index = (index & 0xffff);
if (m_Index != index)
{
int num = SendMessage(this.Handle, 0x18a, index, 0);
StringBuilder lParam = new StringBuilder(num + 1);
SendMessage(this.Handle, 0x189, index, lParam);
Point owPoint = this.Owner.PointToClient(msPoint);
this.toolTip.RemoveAll();
this.toolTip.Show(lParam.ToString(), this.Owner, owPoint.X + 10, owPoint.Y + 10, 1000);
m_Index = index;
}
}
}
base.WndProc(ref m);
}
}
代码是直接COPY过来的,留作备用,没来得及测试,呵呵
浙公网安备 33010602011771号