/// <summary> /// Message handler which must be called from client form. /// Processes Windows messages and calls event handlers. /// </summary> /// <param name="m"></param> public void WndProc(ref Message m) { int devType; char c; if (m.Msg == WM_DEVICECHANGE) { // WM_DEVICECHANGE can have several meanings depending on the WParam value... switch (m.WParam.ToInt32()) { // // New device has just arrived // case DBT_DEVICEARRIVAL: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME vol; vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); // Get the drive letter c = DriveMaskToLetter(vol.dbcv_unitmask); // // Call the client event handler // // We should create copy of the event before testing it and // calling the delegate - if any StorageDetectorEventHandler tempDeviceArrived = StorageDetected; if (tempDeviceArrived != null) { StorageDetectorEventArgs e = new StorageDetectorEventArgs(); e.Drive = c + ":\\"; tempDeviceArrived(this, e); // Register for query remove if requested if (e.HookQueryRemove) { // If something is already hooked, unhook it now if (mDeviceNotifyHandle != IntPtr.Zero) { RegisterForDeviceChange(false, null); } RegisterQuery(c + ":\\"); } } // if has event handler } break; } } }
private void Sd_StorageDetected(object sender, StorageDetectorEventArgs e) { _notifyIcon.ShowBalloonTip(5000); _policyForm.Drive = e.Drive; _policyForm.Show(); }