1. 程式人生 > >Wpf WndProc 訊息處理

Wpf WndProc 訊息處理

public MainWindow()
        {
            InitializeComponent();
            this.SourceInitialized += new EventHandler(WSInitialized);
        }

void WSInitialized(object sender, EventArgs e)
        {
            HwndSource hs = PresentationSource.FromVisual(this) as HwndSource;
            hs.AddHook(new HwndSourceHook(WndProc));
        }

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case 0x219: //WM_DEVICECHANGE
                    FindPort();//處理事件
                    handled = true;
                    break;
                default: break;
            }
            return (System.IntPtr)0;
        }