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

本文展示了一个使用C#编写的Windows消息钩子示例,通过注册全局钩子监听特定消息(如设备变更通知),并演示了如何在接收到这些消息时执行自定义操作。
206

被折叠的 条评论
为什么被折叠?



