拦截窗体消息—使用NativeWindow

2023-11-18

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;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

拦截窗体消息—使用NativeWindow 的相关文章

随机推荐