2017-03-08 74 views
0

我已经创建了我自己的自定义控件,在这里我想将此自定义控件设置为其他控件的工具提示。所以我必须添加代码,以通过SendMessage的方式通过下面的代码设置的自定义控制,工具提示,SendMessage无法设置工具提示窗口

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函数的结果。我可否知道,为什么自定义控件未能设置为控件的工具提示?

这里是SendMessage函数,

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError=true)] 
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref TOOLINFO lParam); 

我找不到失败的原因。请任何人都帮助我。

问候, 阿迈勒拉吉U.

回答

0

我看不到你WndProc方法。所有消息都将发送到您的地址。但除非像“WndProc(参考消息m)”这样的传播者,并且你的自定义消息没有默认传输,否则他们会在空中飞行。

TTM.TTM_ADDTOOLW 

这是您的自定义消息将发送给recepient。必须使用WndProc方法进行控制,并且此方法必须能够捕捉到您的消息并在此处执行您的操作。

我发了消息,从Form1中到窗体2:

`SendMessage(Form2.handle, Msgs.Close, 0, null)` 

,并从窗体2钓到这样的:

protected override void WndProc(ref Message m) { 
    if (m.Msg == Msgs.Close) { 
     AskForExit(); 
    } else if (m.Msg == Msgs.Other) { 
     DoOtherStuffOwnedByMe(); 
    } 
     base.WndProc(ref m); 
} 

也许这可能是导致你的解决方案。