我的表單上有一個取消按鈕。我想確定在WndProc
方法裏面這個Cancel
按鈕被點擊併爲它編寫一些代碼。這是絕對必要的,否則我無法取消所有尚未執行的其他控件驗證事件。捕獲某個按鈕點擊的WndProc消息
請幫忙。
.NET - 2.0的WinForms
我的表單上有一個取消按鈕。我想確定在WndProc
方法裏面這個Cancel
按鈕被點擊併爲它編寫一些代碼。這是絕對必要的,否則我無法取消所有尚未執行的其他控件驗證事件。捕獲某個按鈕點擊的WndProc消息
請幫忙。
.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);
}
BTW:這是32位的代碼。
64位會有重大變化嗎? –
編譯.NET 64位時,IntPtr將爲64位。據我所知這應該仍然有效,但我沒有測試它。順便說一句:我修復了代碼中的錯誤(用0xFFFF代替了0xFF) –
這絕對適用於常規的Windows組件,但是當使用DevExpress時,有一個名爲LayoutControl的控件,其子控件是這個按鈕。所以當單擊子控件時總是返回LayoutControl而不是按鈕。任何想法如何解決這個問題? –
如果有哪個驗證失敗則控制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;
}
}
那麼我使用DevExpress組件,我可以肯定地告訴你它不起作用。如果你可以告訴我如何使用'WndProc'來做同樣的事情,那真的很有幫助。 –
我無法幫助你,請聯繫DevExpress尋求支持。 –
一個簡單的'WndProc'示例可以瞭解如何知道哪個消息來自該按鈕的點擊。 –
C#或VB?另外,你知道在運行時按鈕在窗體上嗎? –
那麼,當然這不是絕對必要的,並且重寫窗體的WndProc()方法肯定不會讓你得到任何地方。考慮將按鈕的CausesValidation屬性設置爲False,並使用表單的FormClosing事件將e.Cancel設置爲false。 –
@ C.Barlow任何人都可以。是的,它在我的表單上處於固定位置。 –