-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitorcontroller.cpp
More file actions
109 lines (78 loc) · 3.01 KB
/
Copy pathmonitorcontroller.cpp
File metadata and controls
109 lines (78 loc) · 3.01 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "monitorcontroller.h"
#include <iostream>
#include <Windows.h>
#include <QDebug>
MonitorController::MonitorController(QObject *parent)
: QThread{parent}
{
}
MonitorController::~MonitorController()
{
if(hwnd !=NULL){
DestroyWindow(hwnd);
}
UnregisterDeviceNotification(devNotify);
}
void MonitorController::run()
{
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = &MonitorController::WindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = CLASS_NAME;
_atom = RegisterClassEx(&wc);
if(_atom==NULL){
std::wcout << "Register class " << GetLastError() << std::endl;
}
hwnd = CreateWindowEx(
0, // Optional window styles.
MAKEINTATOM(_atom), // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
GetModuleHandle(NULL), // Instance handle
NULL // Additional application data
);
if(hwnd)
std::cout << "HWND NOT NULL" << std::endl;
else
std::cout << "HWND NULL" << std::endl;
SetWindowLongPtr(hwnd,GWLP_USERDATA,(LONG_PTR)this);
std::wcout << "Error " << GetLastError() << std::endl;
GUID GUID_DEVINTERFACE_MONITOR = { 0xe6f07b5f, 0xee97, 0x4a90, 0xb0, 0x76, 0x33, 0xf5, 0x7b, 0xf4, 0xea, 0xa7 };
DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
memset(¬ificationFilter, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
notificationFilter.dbcc_reserved = 0;
notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
notificationFilter.dbcc_classguid = GUID_DEVINTERFACE_MONITOR;
devNotify = RegisterDeviceNotification(hwnd, ¬ificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
ShowWindow(hwnd, SW_HIDE);
exec();
}
LRESULT CALLBACK MonitorController::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
std::cout << "WNDPROC " << uMsg << std::endl;
MonitorController* mc = reinterpret_cast<MonitorController*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (mc)
return mc->MyWindowProc(hwnd, uMsg, wParam, lParam);
else
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT MonitorController::MyWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
qDebug() << "MyWindowProc";
switch (uMsg)
{
case WM_DEVICECHANGE:
case WM_DISPLAYCHANGE:
std::cout << "DISPLAY CHANGE" << std::endl;
emit DisplaysChanged();
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}