关键步骤是覆盖DefWndProc的消息预处理事件。如下,我们定义一个自己的消息进行处理 。
1.引用Sendmessage并定义一个消息
[DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint Lparam); private const int WM_User=0x0400+101;
2.覆盖DefWndProc事件
protected override void DefWndProc(ref Message m) { switch (m.Msg) { case WM_User: MessageBox.Show("User's Message"); break; default: base.DefWndProc(ref m); break; } }
3.调用SendMessage事件
private void button1_Click(object sender, EventArgs e) { SendMessage(Handle, WM_User, 0, 0); }