-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
135 lines (115 loc) · 3.79 KB
/
main.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QPixmap>
#include <QIcon>
#include <QToolButton>
#include <QSize>
#include <QLabel>
#include <QObject>
#include <iostream>
#include <QRunnable>
#include <QThreadPool>
#include "mainwindow.h"
#include <cstdio>
#include <windows.h>
#include <cmath>
#include "winsw.h"
#include <string>
class Work : public QRunnable {
public:
void run() {
//qDebug() << "Hello from thread " << QThread::currentThread();
}
};
BOOL CALLBACK MonitorGetEnumProc(
HMONITOR hMonitor,
HDC hdcMonitor,
LPRECT lprcMonitor,
LPARAM dwData
)
{
MONITORINFOEXA mi;
ZeroMemory(&mi, sizeof(mi));
mi.cbSize = sizeof(mi);
GetMonitorInfoA(hMonitor, &mi);
std::string s(mi.szDevice);
reinterpret_cast<std::vector<int>*>(dwData)->push_back(stoi(s.substr(11)));
return TRUE;
}
BOOL CALLBACK MonitorSetProc(
HMONITOR hMonitor,
HDC hdcMonitor,
LPRECT lprcMonitor,
LPARAM dwData
)
{
MONITORINFOEXA mi;
ZeroMemory(&mi, sizeof(mi));
mi.cbSize = sizeof(mi);
GetMonitorInfoA(hMonitor, &mi);
if (dwData) {
if (strcmp(mi.szDevice, (char*)dwData) == 0)
{
HWND topWindowHwnd = GetTopWindow(NULL);
while (topWindowHwnd)
{
if (IsWindowVisible(topWindowHwnd)) {
HMONITOR mon = MonitorFromWindow(topWindowHwnd, 0);
MONITORINFOEXA mix;
mix.cbSize = sizeof(MONITORINFOEXA);
GetMonitorInfoA(mon, &mix);
if (strcmp(mix.szDevice, mi.szDevice) == 0)
{
char buf[256];
GetWindowTextA(topWindowHwnd, buf, 256);
if (strlen(buf) > 0) {
RECT rt;
GetWindowRect(topWindowHwnd, &rt);
printf("%d %d\n", rt.left, rt.top);
printf("%s\n", buf);
INPUT input = { 0 };
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = (rt.left + 6) * 65536 / GetSystemMetrics(SM_CXSCREEN);
input.mi.dy = (rt.top + 1) * 65536 / GetSystemMetrics(SM_CYSCREEN);
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN;
SendInput(1, &input, sizeof(input));
input.mi.dx = 0;
input.mi.dy = 0;
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &input, sizeof(input));
return TRUE;
}
}
}
topWindowHwnd = GetNextWindow(topWindowHwnd, GW_HWNDNEXT);
}
}
}
return TRUE;
}
std::vector<int> query_display_ids()
{
std::vector<int> ids;
EnumDisplayMonitors(NULL, NULL, MonitorGetEnumProc, (LPARAM)&ids);
return ids;
}
bool set_active_display(int displayId)
{
std::string path = "\\\\.\\DISPLAY" + std::to_string(displayId);
return EnumDisplayMonitors(NULL, NULL, MonitorSetProc, (LPARAM)path.c_str());
}
int main(int argc, char** argv) {
QApplication app(argc, argv);
Work work;
work.setAutoDelete(false);
QThreadPool *threadPool = QThreadPool::globalInstance();
threadPool->start(&work);
//qDebug() << "hello from GUI thread " << QThread::currentThread();
threadPool->waitForDone();
MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec();
}