C# 监听windows系统消息
C# 监听windows系统消息
1、用于监听Windows消息
HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;//窗口过程 hwndSource.AddHook(new HwndSourceHook(DeveiceChanged)); //挂钩
2、勾子函数
public const int WM_DEVICECHANGE = 0x219; //Windows消息编号 COM消息 public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
//钩子函数 { { switch (wParam.ToInt32()) case DBT_DEVICEARRIVAL://设备插入
break;
case DBT_DEVICEREMOVECOMPLETE: //设备卸载 { ControlChange(true); break;
default: } return IntPtr.Zero;
}