以下是在WPF中接收系统消息的方法,类似于c#里的WndProc函数
private HwndSource m_source;
public const int WM_DEVICECHANGE = 0x0219;
private void OnLoaded(Object sender,EventArgs e)
{
IntPtr hwnd = new WindowInteropHelper(this).Handle;
if (hwnd != null && hwnd != IntPtr.Zero)
{
m_source = HwndSource.FromHwnd(hwnd);
m_source.AddHook(new HwndSourceHook(MessageHookHandler));
}
}
public IntPtr MessageHookHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_DEVICECHANGE)
{
//do what you want
handled = true;
}
return IntPtr.Zero;
}