1、检索U盘
void MainWindow::onSigDeviceChange(MSG *msg)
{
mVectorDiskVolume.clear();
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
switch(msg->wParam)
{
case DBT_DEVICETYPESPECIFIC:
break;
case DBT_DEVICEARRIVAL:
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if(lpdbv->dbcv_flags ==0)
{
QString USBDisk = QString(this->FirstDriveFromMask(lpdbv ->dbcv_unitmask));
mVectorDiskVolume.append(USBDisk);
setCheckBoxState();
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if(lpdbv->dbcv_flags == 0)
{
QString USBDisk = QString(this->FirstDriveFromMask(lpdbv ->dbcv_unitmask));
mVectorDiskVolume.removeAt(mVectorDiskVolume.indexOf(USBDisk));
}
}
break;
default:
break;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
char MainWindow::FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
重写nativeEvent函数
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG* msg = reinterpret_cast<MSG*>(message);
if (msg->message == WM_DEVICECHANGE)
{
emit(sigDeviceChange(msg));
}
if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") {
MSG* msg = static_cast<MSG *>(message);
switch (msg->message)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
if ((VK_F4 == msg->wParam) && (::GetKeyState(VK_MENU) & 0xF000))
{
return TRUE;
}
}
break;
default:
break;
}
}
return QWidget::nativeEvent(eventType, message, result);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
关联信号槽
connect(this, SIGNAL(sigDeviceChange(MSG*)), SLOT(onSigDeviceChange(MSG*)));
头文件
#include
#include "dbt.h"