:275.256KB : :1 :2022-08-28 22:40:06
1. 获取WM_DEVICECHANGE消息的方法
WM_DEVICECHANGE的使用方式有两种,一种是通过WindowProc()的方式,另一种是通过OnDeviceChange()消息响应函数的方式,底下就先介紹第一種方式。
1)通过WindowProc()的方式检测U盘
需要包含的头文件:
#include
#include
重写WindowProc()函数实现U盘检测,代码如下:
char FirstDriveFromMask(ULONG unitmask) //获取盘符
{
char i;
for (i = 0; i < 26; i)
{
if (unitmask & 0x1)break;
unitmask = unitmask >> 1;
}
return (i 'A');
}
LRESULT CUpanDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
CString detectMsg;
switch (message)
{
//WM_DEVICECHANGE,系统硬件改变发出的系统消息
case WM_DEVICECHANGE:
{
PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)lParam;
switch(wParam)
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL://设备检测结束,并且可以使用
{
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
switch(lpdbv->dbcv_flags)
{
case 0: //U盘
{
CString decDriver;
decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
detectMsg.Format(_T("检测到U盘:[%s]插入!"), decDriver.GetBuffer(0));
m_editControl.SetWindowText(detectMsg);
}
break;
case DBTF_MEDIA: //光盘
break;
}
}
}
break;
case DBT_DEVICEREMOVECOMPLETE://设备卸载或者拔出
{
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
switch(lpdbv->dbcv_flags)
{
case 0: //U盘
{
CString decDriver;
decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
detectMsg.Format(_T("检测到U盘:[%s]拔出!"), decDriver.GetBuffer(0));
m_editControl.SetWindowText(detectMsg);
}
break;
case DBTF_MEDIA: //光盘
break;
default:break;
}
}
}
break;
default:break;
}
}
break;
default:break;
}
return CDialog::WindowProc(message, wParam, lParam);
}
2.通过OnDeviceChange()的方式检测U盘
1)首先在头文件声明一个消息函数:
afx_msg BOOL OnDeviceChange(UINT nEventType, DWORD dwData);
2)消息映射:
BEGIN_MESSAGE_MAP(OGrgFrmRepair, CDialog)
ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()
3)实现
char FirstDriveFromMask(ULONG unitmask) //获取盘符
{
char i;
for (i = 0; i < 26; i)
{
if (unitmask & 0x1) break;
unitmask = unitmask >> 1;
}
return (i 'A');
}
BOOL CUpanDlg::OnDeviceChange(UINT nEventType, DWORD dwData)
{
CString detectMsg = _T("");
PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)dwData;
switch (nEventType)
{
case DBT_DEVICEREMOVECOMPLETE:
{
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
switch(lpdbv->dbcv_flags)
{
case 0: //U盘
{
CString decDriver;
decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
detectMsg.Format(_T("检测到U盘:[%s]拔出!"), decDriver.GetBuffer(0));
m_editControl.SetWindowText(detectMsg);
}
break;
case DBTF_MEDIA: //光盘
break;
default:break;
}
}
}
break;
case DBT_DEVICEARRIVAL:
{
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
switch(lpdbv->dbcv_flags)
{
case 0: //U盘
{
CString decDriver;
decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
detectMsg.Format(_T("检测到U盘:[%s]插入!"), decDriver.GetBuffer(0));
m_editControl.SetWindowText(detectMsg);
}
break;
case DBTF_MEDIA: //光盘
break;
default:break;
}
}
}
break;
default:break;
}
return TRUE;
}