winforms - 捕获某个按钮点击的WndProc消息

标签 winforms .net-2.0 wndproc windows-messages

我的表单上有一个取消按钮。我想在 WndProc 方法中确定单击了这个 Cancel 按钮并为其编写一些代码。这是绝对必要的,因为否则我无法取消所有其他尚未执行的控件验证事件。

请帮忙。

.NET - 2.0,WinForms

最佳答案

这是您如何解析 WndProc 消息以在子控件上单击鼠标左键:

protected override void WndProc(ref Message m)
{
    // http://msdn.microsoft.com/en-us/library/windows/desktop/hh454920(v=vs.85).aspx
    // 0x210 is WM_PARENTNOTIFY
    // 513 is WM_LBUTTONCLICK
    if (m.Msg == 0x210 && m.WParam.ToInt32() == 513) 
    {
        var x = (int)(m.LParam.ToInt32() & 0xFFFF);
        var y = (int)(m.LParam.ToInt32() >> 16);

        var childControl = this.GetChildAtPoint(new Point(x, y));
        if (childControl == cancelButton)
        {
            // ...
        }
    }
    base.WndProc(ref m);
}

顺便说一句:这是 32 位代码。

关于winforms - 捕获某个按钮点击的WndProc消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10635518/

相关文章:

c# - 以编程方式解除事件处理程序的 Hook

c# - 我怎样才能只公开 IList<> 的一个片段?

c++ - "How can I make a WNDPROC or DLGPROC a member of my C++ class?"中的 LNK2001

c# - 我怎样才能每 5 分钟按一次空格键?

VB.net 如何为我自己的文件扩展名设置图标

c# - 使用 WM_SETTEXT 设置记事本文本不会影响记事本实例中的 Text_Changed 事件

c# - 在动态添加的 UserControl 上设置属性

.net - 将定时器间隔设置为无限

c++ - Win32 中的 WM_NOTIFY 和父类(super class)链接问题

c# - 从处理 NativeWindow 中的 WM_GETOBJECT 返回 IOleCommandTarget