我过去在使用虚拟对象编写文件浏览器并通过该方法HwndSource
添加事件处理程序时已经完成了这一点。HwndSource.AddHook()
// In object constructor
var hwndSource = new HwndSource(0, 0, 0, 0, 0, "", IntPtr.Zero); // Set up dummy HwndSource
hwndSource.AddHook(sourceHook);
IntPtr sourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg = WM_DEVICECHANGE)
if (wParam.ToInt32 == DBT_DEVICEARRIVAL) // Do what you need to do
if (wParam.ToInt32 == DBT_DEVICEREMOVALCOMPLETE) // Handle device removal
}
// Uses these defined constants:
private const int WM_DEVICECHANGE = 0x219;
private const int DBT_DEVICEARRIVAL = 0x8000;
private const int DBT_DEVICEREMOVALCOMPLETE = 0x8004;
WM_DEVICECHANGE 的 MSDN 还包含其他可能有用的 const 定义的信息:http:
//msdn.microsoft.com/en-us/library/windows/desktop/aa363480 (v=vs.85).aspx