資源簡介
基于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?????{
????????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矩陣鍵盤掃描加數碼管顯示代碼
- 下一篇:文本顯示控件,支持長英文字符串自動換行
評論
共有 條評論