基本要点是:你只需要从源代码中获取
AddHook
。每个WinForm都有一个方法
GetHandle()
,它会给你窗口/表单的句柄(而你已经自己找到了
PostMessage
)。
要翻译
AddHook
,你可以编写自己的类实现
IMessageFilter
(1),或者覆盖
WndProc()
(2)。
(1)将接收应用程序范围内的消息,无论您将其发送到哪个窗体,而(2)仅接收覆盖该方法的特定窗体的消息。
我找不到关于
WM_CALL
的任何信息,因为您必须将窗口消息指定为整数(通常是十六进制),所以这取决于您。
(1):
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
public Form1()
{
InitializeComponent();
IntPtr hWnd = this.Handle;
Application.AddMessageFilter(new MyMessageFilter());
PostMessage(hWnd, WM_xxx, IntPtr.Zero, IntPtr.Zero);
}
}
class MyMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_xxx)
{
}
return false;
}
}
(2):
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public partial class Form 1 {
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
public Form1()
{
InitializeComponent();
IntPtr hWnd = this.Handle;
PostMessage(hWnd, WM_xxx, IntPtr.Zero, IntPtr.Zero);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WMK_xxx)
{
}
}
}