编程光驱监测

 
光驱监测
VC6 新建一个 Dialog 对话框类的程序,重载 WindowProc() 函数(在“查看” à “新建类向导”页面“ Messages ”中找到主对话框的 WindowProc() 虚汗数,点击右边创建并编辑它),参照下面内容添加进入即可。下面是源码,要说的都包含在注释中了,看看吧:
 
LRESULT CTestDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    // TODO: Add your specialized code here and/or call the base class
    PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
    CString szMsg;
    if(message == WM_DEVICECHANGE)
    {
         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)
                  {
                      // 显示消息,获取光驱的逻辑驱动器号
                      szMsg.Format(" 驱动器%c: 已经可用/n" ,chFirstDriveFromMask(lpdbv ->dbcv_unitmask));
                      AfxMessageBox(szMsg);
                  }
             }
             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)
                      {
                          // 显示消息,获取光驱的逻辑驱动器号
                          szMsg.Format(" 驱动器%c: 已经弹出/n" ,chFirstDriveFromMask(lpdbv ->dbcv_unitmask));
                          AfxMessageBox(szMsg);
                      }
                  }
                  break;
             }
    }
    return CDialog::WindowProc(message, wParam, lParam);
}
 
其中用到的函数 chFirstDriveFromMask() 如下:
char chFirstDriveFromMask (ULONG unitmask)
{
    char i;
    for (i = 0; i < 26; ++i) // 假设不会超过个逻辑驱动器
    {
         if (unitmask & 0x1) // 看该驱动器的状态是否发生了变化
         break;
         unitmask = unitmask >> 1;
    }
    return (i + 'A');
}
 
 
 

你可能感兴趣的:(编程,c,Class,dialog)