Ejemplo n.º 1
0
 public static void WaitForUsbDeviceRemove()
 {
     WndProcReceiverForm.WaitForSingleWndProc(
         m =>
         m.Msg == WM_DEVICECHANGE &&
         (int)m.WParam == DBT_DEVICEREMOVECOMPLETE
         );
 }
Ejemplo n.º 2
0
 public static Tuple <Message, TObject> WaitForSingleWndProc <TObject>(Func <Message, bool> predicate, Func <Message, TObject> generator)
 {
     using (WndProcReceiverForm form = new WndProcReceiverForm())
     {
         form.Show();
         return(form.WaitForWndProc(predicate, generator));
     }
 }
Ejemplo n.º 3
0
 public static Message WaitForSingleWndProc(Func <Message, bool> predicate)
 {
     using (WndProcReceiverForm form = new WndProcReceiverForm())
     {
         form.Show();
         return(form.WaitForWndProc(predicate));
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 等待 USB 存储设备接入。
        /// </summary>
        /// <returns>接入的 USB 设备盘符。</returns>
        public static char WaitForUsbDevice()
        {
            DevBroadcastVolume vol = WndProcReceiverForm.WaitForSingleWndProc(
                m =>
                m.Msg == WM_DEVICECHANGE &&
                (int)m.WParam == DBT_DEVICEARRIVAL &&
                Marshal.ReadInt32(m.LParam, 4) == DBT_DEVTYP_VOLUME,
                m =>
                Marshal.PtrToStructure <DevBroadcastVolume>(m.LParam)).Item2;

            BitArray bv     = new BitArray(new int[] { vol.Mask });
            char     letter = '\0';

            for (int i = 0; i < bv.Length; i++)
            {
                if (bv[i])
                {
                    letter = (char)('A' + i);
                }
            }

            return(letter);
        }