首页 > 代码库 > MFC入门教程之控件(7)设备自动检测ondevicechange的添加和使用

MFC入门教程之控件(7)设备自动检测ondevicechange的添加和使用

MFC入门学习(1)了解MFC

 

源文档<http://blog.csdn.net/codectq/article/details/19131227>

 

MFC入门学习之控件(2)创建一个对话框窗口并在运行时显示

 

源文档<http://blog.csdn.net/codectq/article/details/19131289>

 

MFC入门学习之控件(3)标签控件Tab Control的添加和使用

 

源文档<http://blog.csdn.net/codectq/article/details/19131553>

 

MFC入门学习之控件(4)标签控件List Control的添加和使用(上)

 

源文档<http://blog.csdn.net/codectq/article/details/19176595>

 

MFC入门学习之控件(5)标签控件List Control的添加和使用(中)

 

源文档<http://blog.csdn.net/codectq/article/details/19204165>

 

MFC入门学习之控件(6)标签控件List Control的添加和使用(下)

 

源文档<http://blog.csdn.net/codectq/article/details/23550465> 


在对话框初始化过程中将设备通知消息注册到winproc中

//Register to receive notification when a USB device is plugged in.

broadcastInterface.dbcc_size =sizeof(DEV_BROADCAST_DEVICEINTERFACE);

broadcastInterface.dbcc_devicetype =DBT_DEVTYP_DEVICEINTERFACE;

 

memcpy( &(broadcastInterface.dbcc_classguid),

&(GUID_CLASS_USB_DEVICE),

sizeof(struct _GUID));

 

gNotifyDevHandle = RegisterDeviceNotification(hwnd,

&broadcastInterface,

DEVICE_NOTIFY_WINDOW_HANDLE);

 

// Now register for Hub notifications.

memcpy( &(broadcastInterface.dbcc_classguid),

&(GUID_CLASS_USBHUB),

sizeof(struct _GUID));

 

gNotifyHubHandle = RegisterDeviceNotification(hwnd,

&broadcastInterface,

DEVICE_NOTIFY_WINDOW_HANDLE);

 

 编写设备状态变化时的状态解析函数:

 

OnDeviceChange(UINTnEventType, DWORD dwData)

{

PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)dwData;

char cDrive;

TRACE("OnDeviceChange: %u\n",nEventType);

switch(nEventType)

{

case DBT_DEVICEARRIVAL:

TRACE("OnDeviceChange: DBT_DEVICEARRIVAL\n");

if(DBT_DEVTYP_VOLUME == lpdb->dbch_devicetype)

{

 

 

 

 

//Logical volume has been inserted and becomes available

PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

if(0 == lpdbv->dbcv_flags)

{

cDrive = FirstDriveFromMask(lpdbv->dbcv_unitmask);

RemovableDiskArrival(cDrive);

}

}

break;

case DBT_DEVICEQUERYREMOVE:

break;

case DBT_DEVICEQUERYREMOVEFAILED:

break;

case DBT_DEVICEREMOVEPENDING:

break;

case DBT_DEVICEREMOVECOMPLETE:

TRACE("OnDeviceChange:DBT_DEVICEREMOVECOMPLETE\n");

if(DBT_DEVTYP_VOLUME == lpdb->dbch_devicetype)

{

//Logical volume has been physically removed

PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

if(0 == lpdbv->dbcv_flags)

{

 

 

 

 

 

TRACE("OnDeviceChange: DBT_DEVICEREMOVECOMPLETERefreshTree\n");

cDrive = FirstDriveFromMask(lpdbv->dbcv_unitmask);

RemovableDiskRemove(cDrive);

 

 

 

 

}

}

 

break;

case DBT_DEVICETYPESPECIFIC:

break;

case DBT_CONFIGCHANGED:

break;

case DBT_DEVNODES_CHANGED:

break;

}

 

return TRUE;

}

 

 将消息添加到消息map表中

BEGIN_MESSAGE_MAP(CAutoDownDlg,CDialog)

ON_WM_DEVICECHANGE()

END_MESSAGE_MAP()