【发布时间】:2017-03-08 06:15:02
【问题描述】:
我创建了自己的自定义控件,在这里我想将此自定义控件设置为其他控件的工具提示。所以我添加了代码,通过下面的代码通过 sendMessage 将自定义控件设置为 ToolTip,
public class MyControl : Control
{
Dictionary<IntPtr, Component> m_tools;
TOOLINFO m_toolInfo;
public MyControl()
{
m_tools = new Dictionary<IntPtr, Component>();
m_toolInfo = new TOOLINFO();
}
string myTipText;
public string MyTipText
{
get
{
return myTipText;
}
set
{
myTipText = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush brush = new SolidBrush(Color.Blue);
e.Graphics.DrawString(this.MyTipText, this.Font, brush, e.ClipRectangle.Location);
}
internal void AddControl(Component component)
{
if (component != null)
{
if (component is Control)
{
Control control = component as Control;
control.HandleCreated += control_HandleCreated;
if (control.IsHandleCreated)
{
control_HandleCreated(control, EventArgs.Empty);
}
}
}
}
void control_HandleCreated(object sender, EventArgs e)
{
Control control = sender as Control;
if(control!=null)
{
m_tools[control.Handle] = control;
m_toolInfo.hwnd = this.Handle;
m_toolInfo.uId = control.Handle;
**int result = WindowsAPI.SendMessage(this.Handle, (int)TTM.TTM_ADDTOOLW, 0, ref m_toolInfo);**
}
}
}
但是 SendMessage 导致失败。请问,为什么自定义控件无法设置为控件的工具提示。
这里是发送消息,
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError=true)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref TOOLINFO lParam);
我找不到失败的原因。请任何人帮助我。
问候, Amal Raj U.
【问题讨论】:
标签: c# windows winforms tooltip sendmessage