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;
}