-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowManager.cpp
More file actions
69 lines (59 loc) · 2.18 KB
/
WindowManager.cpp
File metadata and controls
69 lines (59 loc) · 2.18 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "WindowManager.h"
#include "DeviceMonitor.h"
#include <stdexcept>
#include <wchar.h>
WindowManager::WindowManager(DeviceMonitor* diskMonitor)
: m_hwnd(NULL), m_hDeviceNotify(NULL), m_diskMonitor(diskMonitor) {
}
WindowManager::~WindowManager() {
UnregisterDeviceNotification();
if (m_hwnd) {
DestroyWindow(m_hwnd);
m_hwnd = NULL;
}
}
void WindowManager::InitDeviceNotification() {
// 注册窗口类
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = L"DiskMonitorWindowClass";
if (!RegisterClassEx(&wc)) {
throw std::runtime_error("注册窗口类失败");
}
// 创建设备通知窗口
m_hwnd = CreateWindowEx(0, L"DiskMonitorWindowClass", L"Disk Monitor Hidden Window",
0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
HWND_MESSAGE, NULL, GetModuleHandle(NULL), m_diskMonitor);
if (!m_hwnd) {
throw std::runtime_error("创建设备通知窗口失败");
}
// 注册设备通知
DEV_BROADCAST_DEVICEINTERFACE filter = { 0 };
filter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filter.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;
m_hDeviceNotify = RegisterDeviceNotification(m_hwnd, &filter, DEVICE_NOTIFY_WINDOW_HANDLE);
if (!m_hDeviceNotify) {
throw std::runtime_error("注册设备通知失败");
}
}
void WindowManager::UnregisterDeviceNotification() {
if (m_hDeviceNotify) {
::UnregisterDeviceNotification(m_hDeviceNotify);
m_hDeviceNotify = NULL;
}
}
LRESULT CALLBACK WindowManager::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (uMsg == WM_CREATE) {
LPCREATESTRUCT pCreateStruct = (LPCREATESTRUCT)lParam;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pCreateStruct->lpCreateParams);
return 0;
}
DeviceMonitor* pThis = (DeviceMonitor*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (pThis && uMsg == WM_DEVICECHANGE) {
return pThis->HandleDeviceChange(static_cast<UINT>(wParam), lParam);
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}