NativeWindow提供窗口句柄和窗口过程的低级封装。下面是拦截ContextMenu的显示和消失的例子
public class NativeContextMenu : NativeWindow
{
private const int WM_EXITMENULOOP = 0x212;
private const int WM_ENTERMENULOOP = 0x0211;
public event System.EventHandler ExitMenuLoop;
private IntPtr handle;
public NativeContextMenu(IntPtr handle)//使用窗体句柄
{
this.handle = handle;
this.AssignHandle(handle);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_EXITMENULOOP:
if (ExitMenuLoop!=null)
{
ExitMenuLoop(this, new EventArgs());
}
break;
}
base.WndProc(ref m);
}
}
在CF中没有NativeWindow,使用using OpenNETCF.Windows.Forms;