char chFirstDriveFromMask (ULONG unitmask) { char i; for (i = 0; i < 26; ++i) //假设不会超过26个逻辑驱动器 { if (unitmask & 0x1) //看该驱动器的状态是否发生了变化 break; unitmask = unitmask >> 1; } return (i + 'A'); } |
BOOL WINAPI DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { BOOL fRet = TRUE; // 返回值 //通过响应WM_DEVICECHANGE消息得到的设备事件信息结构 PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; //对话框消息处理 switch (uMsg) { case WM_INITDIALOG: fRet = TRUE; break; //对 WM_DEVICECHANGE 消息进行处理 case WM_DEVICECHANGE: char szMsg[80]; // 对话框中要表示的字符串 switch (wParam) { //当一个设备变得被插入并变得可用时, //系统会发送广播事件DBT_DEVICEARRIVAL case DBT_DEVICEARRIVAL: // 判断CDROM碟片是否已经插入到光驱中 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME) { PDEV_BROADCAST_VOLUME lpdbv=(PDEV_BROADCAST_VOLUME) lpdb; //判断是否有CDROM碟片 if (lpdbv -> dbcv_flags & DBTF_MEDIA) { // 显示消息,获取光驱的逻辑驱动器号 wsprintf (szMsg, "驱动器 %c: 已经可用\n",chFirstDriveFromMask(lpdbv ->dbcv_unitmask)); MessageBox (hwnd, szMsg, "光驱自动监测", MB_OK |MB_ICONINFORMATION); } } break; //当一个设备变得被移走并变得不可用时, //系统会发送广播事件DBT_ DEVICEREMOVECOMPLETE case DBT_DEVICEREMOVECOMPLETE: // 判断CDROM碟片是否从光驱中移走 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME) { PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb; if (lpdbv -> dbcv_flags & DBTF_MEDIA) { //显示消息,获取光驱的逻辑驱动器号 wsprintf (szMsg, "驱动器 %c: 已经弹出\n",chFirstDriveFromMask(lpdbv ->dbcv_unitmask)); MessageBox (hwnd, szMsg, "光驱自动监测", MB_OK| MB_ICONINFORMATION); } } break; } //处理其他Windows消息 case WM_COMMAND: int wmId, wmEvent; wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case IDOK: EndDialog(hwnd, 0); break; } default: fRet = FALSE; break; } // 禁止光驱的AutoPlay功能 static UINT uMsgQueryCancelAutoPlay=RegisterWindowMessage("QueryCancelAutoPlay"); if (uMsg==uMsgQueryCancelAutoPlay) { int n = MessageBox(hwnd, "你想禁止AutoPlay功能吗?", NULL,MB_YESNO | MB_ICONQUESTION); // 1代表取消 AutoPlay // 0 t代表允许AutoPlay SetDlgMsgResult(hwnd, uMsg, (n == IDYES) ? 1 : 0); fRet = (n == IDYES) ? 1 : 0; } return(fRet); } |
int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //从对话框模版资源中创建一个模式对话框 DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),NULL, DlgProc); return 0; } |