【问题标题】:SendMessage fails to set ToolTip windowSendMessage 设置工具提示窗口失败
【发布时间】: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


    【解决方案1】:

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

    TTM.TTM_ADDTOOLW
    

    这是您将发送给收件人的自定义消息。必须有一个具有重写 WndProc 方法的控件,并且此方法必须捕获您的消息并在那里执行您的操作。

    我从Form1向Form2发送了一条消息:

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

    并从 Form2 中捕获,例如:

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

    也许它可能会为您提供解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      相关资源
      最近更新 更多