资源简介
基于Qt5.5.1的应用程序,支持监控系统设备变化,当插入可移动设备、右键弹出和移除可移动设备时可以实时的通知应用程序
代码片段和文件信息
#include
#include
#include
#include
#include
#include “diskoperate.h“
namespace
{
bool IsDiskExist(char cDiskName)
{
DWORD dwDrivers;
int i = toupper(cDiskName) - ‘A‘;
//dwDrivers的每一个二进制位表示对应的驱动器是否存在。
dwDrivers = GetLogicalDrives();
//判断当前位是否有驱动器
if ((dwDrivers & (1 << (i))) != 0)
{
return true;
}
return false;
}
GUID WceusbshGUID = { 0x25dbce51 0x6c8f 0x4a72
0x8a0x6d0xb50x4c0x2b0x4f0xc80x35 };
}
diskoperate::diskoperate(QWidget * parent)
: QWidget(parent)
{
registerDiskNotify();
updateMoveDrives();
qApp->installNativeEventFilter(this);
}
diskoperate::~diskoperate()
{
}
void diskoperate::registerDiskNotify()
{
QList storages = QStorageInfo::mountedVolumes();
QStorageInfo rootStorage = QStorageInfo::root();
storages.push_back(rootStorage);
for (int i = 0; i < storages.count(); ++i)
{
QStorageInfo storage = storages[i];
qint64 bytesAvailable = storage.bytesAvailable();
qint64 bytesFree = storage.bytesFree();
qint64 bytesTotal = storage.bytesTotal();
QByteArray device = storage.device();
QString displayName = storage.displayName();
QByteArray systemtype = storage.fileSystemType();
bool isRead = storage.isReadOnly();
bool isReady = storage.isReady();
bool isRoot = storage.isRoot();
bool isValid = storage.isValid();
QString name = storage.name();
QString path = storage.rootPath();
path = storage.rootPath();
}
}
bool diskoperate::registerDisk(const QString & cDiskName)
{
if (!IsDiskExist(cDiskName.at(0).toLatin1()))
{
return false;
}
const UINT oldmode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);//不弹出系统提示
HANDLE handle = CreateFile(
cDiskName.toStdWString().c_str()
GENERIC_READ
FILE_SHARE_READ | FILE_SHARE_WRITE
0
OPEN_EXISTING
FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL
0);//拿到盘符句柄
if (handle == nullptr)
{
return false;
}
DEV_BROADCAST_HANDLE NotificationFilter;
ZeroMemory( &NotificationFilter sizeof (NotificationFilter) );
NotificationFilter.dbch_size = sizeof (DEV_BROADCAST_HANDLE );
NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
NotificationFilter.dbch_handle = handle;
HDEVNOTIFY hDevNotify = RegisterDeviceNotification((HWND)this->winId()
&NotificationFilter
DEVICE_NOTIFY_WINDOW_HANDLE);//注册设备通知
CloseHandle(handle);//关闭盘符句柄
::SetErrorMode(oldmode);//恢复之前错误模式
if (!hDevNotify)
{
return false;
}
m_lstMoveDrive[cDiskName.at(0)] = hDevNotify
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-29 17:43 diskoperate\Resources\
文件 7261 2017-12-29 17:30 diskoperate\diskoperate.cpp
文件 675 2017-12-26 16:03 diskoperate\diskoperate.h
文件 5861 2017-12-26 14:38 diskoperate\diskoperate.vcxproj
文件 1318 2017-12-26 10:48 diskoperate\diskoperate.vcxproj.filters
文件 235 2017-12-26 15:31 diskoperate\main.cpp
目录 0 2017-12-29 17:43 diskoperate\
- 上一篇:4乘4矩阵键盘扫描加数码管显示代码
- 下一篇:文本显示控件,支持长英文字符串自动换行
评论
共有 条评论