1 char FirstDriveFromMask(ULONG unitmask) //獲取盤符
2 {
3 char i;
4 for (i = 0; i < 26; ++i)
5 {
6 if (unitmask & 0x1)
7 break;
8 unitmask = unitmask >> 1;
9 }
10 return (i + 'A');
11 }
12
13
14 LRESULT CUpanDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
15 {
16 CString detectMsg;
17
18 switch (message)
19 {
20 //WM_DEVICECHANGE,系统硬件改变发出的系统消息
21 case WM_DEVICECHANGE:
22 {
23 PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)lParam;
24 switch(wParam)
25 {
26 case WM_DEVICECHANGE:
27 break;
28 case DBT_DEVICEARRIVAL://设备检测结束,并且可以使用
29 {
30 if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
31 {
32 PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
33 switch(lpdbv->dbcv_flags)
34 {
35 case 0: //U盘
36 {
37 CString decDriver;
38 decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
39 detectMsg.Format(_T("检测到U盘:[%s]插入!"), decDriver.GetBuffer(0));
40 m_editControl.SetWindowText(detectMsg);
41 }
42 break;
43 case DBTF_MEDIA: //光盘
44 break;
45 }
46 }
47 }
48 break;
49 case DBT_DEVICEREMOVECOMPLETE://设备卸载或者拔出
50 {
51 if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
52 {
53 PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
54 switch(lpdbv->dbcv_flags)
55 {
56 case 0: //U盘
57 {
58 CString decDriver;
59 decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
60 detectMsg.Format(_T("检测到U盘:[%s]拔出!"), decDriver.GetBuffer(0));
61 m_editControl.SetWindowText(detectMsg);
62 }
63 break;
64 case DBTF_MEDIA: //光盘
65
66 break;
67 }
68 }
69 }
70 break;
71 }
72 }
73 break;
74 }
75
76 return CDialog::WindowProc(message, wParam, lParam);
77 }