-->

某个按钮点击捕获的WndProc消息(Capturing WndProc message of a

2019-07-29 10:10发布

我有我的窗体上的取消按钮。 我想,以确定里面WndProc方法,该Cancel按钮被点击并编写一些代码吧。 否则我不能够取消那些尚未执行的所有其他控制验证事件这是绝对必要的。

请帮忙。

.NET - 2.0,的WinForms

Answer 1:

这是你如何能解析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位代码。



Answer 2:

如果有哪个失败验证控件然后CauseValidation没有帮助

哦,当然是这样,这就是财产被设计做。 这里有一个例子形式在工作中表现出这一点。 放一个文本框和窗体上的按钮。 请注意,您如何点击按钮来清除文本框,即使盒总是失败的验证。 以及如何关闭窗体。

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        textBox1.Validating += new CancelEventHandler(textBox1_Validating);
        button1.Click += new EventHandler(button1_Click);
        button1.CausesValidation = false;
        this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
    }

    private void textBox1_Validating(object sender, CancelEventArgs e) {
        // Always fail validation
        e.Cancel = true;
    }
    void button1_Click(object sender, EventArgs e) {
        // Your Cancel button
        textBox1.Text = string.Empty;
    }
    void Form1_FormClosing(object sender, FormClosingEventArgs e) {
        // Allow the form to close even though validation failed
        e.Cancel = false;
    }
}


文章来源: Capturing WndProc message of a certain button click