-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRadWndResizer.cpp
340 lines (285 loc) · 11.9 KB
/
RadWndResizer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#define NOMINMAX
#include "Window.h"
#include "Windowxx.h"
#include "dwmapi.h"
#include <tchar.h>
#include "HandlePtr.h"
#include <algorithm>
// TODO
// Needs a tray icon
// Settings for hotkey
// Move window immediatley while menu is still showing
#define HK_MENU 1
#define APPNAME TEXT("RadWndResizer")
inline LONG Width(const RECT& r)
{
return r.right - r.left;
}
inline LONG Height(const RECT& r)
{
return r.bottom - r.top;
}
inline RECT Multiply(RECT r, double scale)
{
r.top = lround(r.top * scale);
r.left = lround(r.left * scale);
r.bottom = lround(r.bottom * scale);
r.right = lround(r.right * scale);
return r;
}
inline SIZE Size(const RECT& r)
{
return { Width(r), Height(r) };
}
inline BOOL UnadjustWindowRectEx(LPRECT prc, DWORD dwStyle, BOOL fMenu, DWORD dwExStyle)
{
RECT rc = {};
BOOL fRc = AdjustWindowRectEx(&rc, dwStyle, fMenu, dwExStyle);
if (fRc)
{
prc->left -= rc.left;
prc->top -= rc.top;
prc->right -= rc.right;
prc->bottom -= rc.bottom;
}
return fRc;
}
class RootWindow : public Window
{
friend WindowManager<RootWindow>;
public:
static ATOM Register() { return WindowManager<RootWindow>::Register(); }
static RootWindow* Create() { return WindowManager<RootWindow>::Create(); }
protected:
static void GetCreateWindow(CREATESTRUCT& cs);
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override;
private:
BOOL OnCreate(LPCREATESTRUCT lpCreateStruct);
void OnDestroy();
void OnHotKey(int idHotKey, UINT fuModifiers, UINT vk);
void OnInitMenuPopup(HMENU hMenu, UINT item, BOOL fSystemMenu);
static LPCTSTR ClassName() { return TEXT("RADWNDRESIZER"); }
POINT m_InitMenuSelection = {};
};
void RootWindow::GetCreateWindow(CREATESTRUCT& cs)
{
Window::GetCreateWindow(cs);
cs.lpszName = APPNAME;
cs.style = WS_OVERLAPPEDWINDOW;
}
BOOL RootWindow::OnCreate(const LPCREATESTRUCT lpCreateStruct)
{
if (!RegisterHotKey(*this, HK_MENU, MOD_CONTROL | MOD_SHIFT, 'M'))
MessageBox(NULL, TEXT("Error registering hotkey"), APPNAME, MB_ICONERROR | MB_OK);
return TRUE;
}
void RootWindow::OnDestroy()
{
PostQuitMessage(0);
}
HBITMAPPtr CreateGridBitmap(const HDC hDC, const SIZE sz, const SIZE rowcols, const POINT selectedAnchor, const POINT selectedPivot)
{
HBITMAPPtr hBitmap(CreateCompatibleBitmap(hDC, sz.cx, sz.cy));
{
const HDCPtr hMemDC(CreateCompatibleDC(hDC));
const auto hOldBitmap = SelectTempObject(hMemDC, hBitmap);
const auto hOldPen = SelectTempObject(hMemDC, GetStockObject(DC_PEN));
const auto hOldBrush = SelectTempObject(hMemDC, GetStockObject(DC_BRUSH));
SetDCPenColor(hMemDC, RGB(0, 0, 0));
SetDCBrushColor(hMemDC, RGB(255, 255, 255));
Rectangle(hMemDC, 0, 0, sz.cx, sz.cy);
for (int i = 1; i < rowcols.cx; ++i)
{
const int x = (sz.cx * i) / rowcols.cx;
MoveToEx(hMemDC, x, 0, nullptr);
LineTo(hMemDC, x, sz.cy);
}
for (int j = 1; j < rowcols.cy; ++j)
{
const int y = (sz.cy * j) / rowcols.cy;
MoveToEx(hMemDC, 0, y, nullptr);
LineTo(hMemDC, sz.cx, y);
}
const POINT selectedMin = { std::min(selectedAnchor.x, selectedPivot.x ), std::min(selectedAnchor.y, selectedPivot.y) };
const POINT selectedMax = { std::max(selectedAnchor.x, selectedPivot.x), std::max(selectedAnchor.y, selectedPivot.y) };
POINT selected;
for (selected.y = selectedMin.y; selected.y <= selectedMax.y; ++selected.y)
{
for (selected.x = selectedMin.x; selected.x <= selectedMax.x; ++selected.x)
{
if (selected.x >= 0 && selected.x < rowcols.cx && selected.y >= 0 && selected.y < rowcols.cy)
{
SetDCBrushColor(hMemDC, RGB(0, 0, 255));
Rectangle(hMemDC, (sz.cx * selected.x) / rowcols.cx, (sz.cy * selected.y) / rowcols.cy, (sz.cx * (selected.x + 1)) / rowcols.cx, (sz.cy * (selected.y + 1)) / rowcols.cy);
}
}
}
}
return hBitmap;
}
HBITMAPPtr CreateGridBitmap(const HDC hDC, const SIZE sz, const SIZE rowcols, const POINT selected)
{
return CreateGridBitmap(hDC, sz, rowcols, selected, selected);
}
HBITMAPPtr CreateGridBitmap(const HDC hDC, const SIZE sz, const SIZE rowcols)
{
return CreateGridBitmap(hDC, sz, rowcols, POINT{ -1, -1 }, POINT{ -1, -1 });
}
void RootWindow::OnHotKey(int idHotKey, UINT fuModifiers, UINT vk)
{
switch (idHotKey)
{
case HK_MENU:
{
const SIZE layouts[] = {
{ 2, 1 },
{ 2, 2 },
{ 2, 3 },
{ 3, 1 },
{ 3, 2 },
{ 3, 3 },
};
//HWND hWnd = *this;
const HWND hWnd = GetForegroundWindow();
RECT include_shadow, exclude_shadow;
GetWindowRect(hWnd, &include_shadow);
DwmGetWindowAttribute(hWnd, DWMWA_EXTENDED_FRAME_BOUNDS, &exclude_shadow, sizeof(exclude_shadow));
RECT client_rect = include_shadow;
UnadjustWindowRectEx(&client_rect, GetWindowStyle(hWnd), FALSE, GetWindowExStyle(hWnd));
const POINT menupt = { client_rect.right, client_rect.top };
SetForegroundWindow(*this);
const HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO moninfo = { sizeof(MONITORINFO) };
GetMonitorInfo(hMonitor, &moninfo);
const RECT r = Multiply(moninfo.rcWork, 40.0 / Height(moninfo.rcWork));
const UINT FIRST_CMD = 2;
int selected = -1;
{
HBITMAPPtr hBitmapSet[ARRAYSIZE(layouts)];
{
auto hDC = MakeGetDC(*this);
for (UINT i = 0; i < ARRAYSIZE(layouts); ++i)
hBitmapSet[i] = CreateGridBitmap(hDC.get(), Size(r), layouts[i]);
}
HMENUPtr hMenu(CreatePopupMenu());
UINT i = 0;
for (const HBITMAPPtr& hBitmap : hBitmapSet)
{
//const bool dobreak = ARRAYSIZE(hBitmapSet) > 3 && (i == (ARRAYSIZE(hBitmapSet) / 2));
const bool dobreak = i > 0 && layouts[i - 1].cx != layouts[i].cx;
AppendMenu(hMenu, MF_BITMAP | (dobreak ? MF_MENUBREAK : 0), FIRST_CMD + i, reinterpret_cast<LPCTSTR>(hBitmap.get()));
++i;
}
selected = TrackPopupMenu(hMenu, TPM_RIGHTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD, menupt.x, menupt.y, 0, *this, nullptr) - FIRST_CMD;
}
if (selected >= 0 && selected < ARRAYSIZE(layouts))
{
const SIZE layout = layouts[selected];
const UINT count = layout.cx * layout.cy;
HBITMAPPtr hBitmapSet[100] = {};
{
auto hDC = MakeGetDC(*this);
UINT i = 0;
POINT selectedAnchor;
for (selectedAnchor.x = 0; selectedAnchor.x < layout.cx; ++selectedAnchor.x)
for (selectedAnchor.y = 0; selectedAnchor.y < layout.cy; ++selectedAnchor.y)
hBitmapSet[i++] = CreateGridBitmap(hDC.get(), Size(r), layout, selectedAnchor);
}
HMENUPtr hMenu(CreatePopupMenu());
for (UINT i = 0; i < count; ++i)
{
const bool dobreak = (i % (count / layout.cx)) == 0;
AppendMenu(hMenu, MF_BITMAP | (dobreak ? MF_MENUBREAK : 0), FIRST_CMD + i, reinterpret_cast<LPCTSTR>(hBitmapSet[i].get()));
}
selected = TrackPopupMenu(hMenu, TPM_RIGHTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD, menupt.x, menupt.y, 0, *this, nullptr) - FIRST_CMD;
if (selected >= 0 && selected < static_cast<int>(count))
{
const POINT selectedAnchor = { selected / layout.cy, selected % layout.cy };
{
{
auto hDC = MakeGetDC(*this);
UINT i = 0;
POINT selectedPivot;
for (selectedPivot.x = 0; selectedPivot.x < layout.cx; ++selectedPivot.x)
for (selectedPivot.y = 0; selectedPivot.y < layout.cy; ++selectedPivot.y)
hBitmapSet[i++] = CreateGridBitmap(hDC.get(), Size(r), layout, selectedAnchor, selectedPivot);
}
HMENUPtr hMenu(CreatePopupMenu());
for (UINT i = 0; i < count; ++i)
{
const bool dobreak = (i % (count / layout.cx)) == 0;
AppendMenu(hMenu, MF_BITMAP | (dobreak ? MF_MENUBREAK : 0), FIRST_CMD + i, reinterpret_cast<LPCTSTR>(hBitmapSet[i].get()));
}
m_InitMenuSelection = selectedAnchor;
selected = TrackPopupMenu(hMenu, TPM_RIGHTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD, menupt.x, menupt.y, 0, *this, nullptr) - FIRST_CMD;
}
if (selected >= 0 && selected < static_cast<int>(count))
{
const POINT selectedPivot = { selected / layout.cy, selected % layout.cy };
const POINT selectedMin = { std::min(selectedAnchor.x, selectedPivot.x), std::min(selectedAnchor.y, selectedPivot.y) };
const POINT selectedMax = { std::max(selectedAnchor.x, selectedPivot.x) + 1, std::max(selectedAnchor.y, selectedPivot.y) + 1 };
RECT newr;
newr.left = moninfo.rcWork.left + selectedMin.x * Width(moninfo.rcWork) / layout.cx;
newr.right = moninfo.rcWork.left + selectedMax.x * Width(moninfo.rcWork) / layout.cx;
newr.top = moninfo.rcWork.top + selectedMin.y * Height(moninfo.rcWork) / layout.cy;
newr.bottom = moninfo.rcWork.top + selectedMax.y * Height(moninfo.rcWork) / layout.cy;
newr.left += include_shadow.left - exclude_shadow.left;
newr.right += include_shadow.right - exclude_shadow.right;
newr.top += include_shadow.top - exclude_shadow.top;
newr.bottom += include_shadow.bottom - exclude_shadow.bottom;
ShowWindow(hWnd, SW_RESTORE);
MoveWindow(hWnd, newr.left, newr.top, Width(newr), Height(newr), TRUE);
}
}
}
SetForegroundWindow(hWnd);
}
break;
}
}
void RootWindow::OnInitMenuPopup(HMENU hMenu, UINT item, BOOL fSystemMenu)
{
if (!fSystemMenu)
{
INPUT inputs[2] = {};
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki = { VK_DOWN, 0, 0 };
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki = { VK_DOWN, 0, KEYEVENTF_KEYUP };
SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
for (LONG i = 0; i < m_InitMenuSelection.y; ++i)
SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
inputs[0].ki = { VK_RIGHT, 0, 0 };
inputs[1].ki = { VK_RIGHT, 0, KEYEVENTF_KEYUP };
for (LONG i = 0; i < m_InitMenuSelection.x; ++i)
SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
m_InitMenuSelection = {};
}
}
LRESULT RootWindow::HandleMessage(const UINT uMsg, const WPARAM wParam, const LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(WM_CREATE, OnCreate);
HANDLE_MSG(WM_DESTROY, OnDestroy);
HANDLE_MSG(WM_HOTKEY, OnHotKey);
HANDLE_MSG(WM_INITMENUPOPUP, OnInitMenuPopup);
HANDLE_DEF(Window::HandleMessage);
}
}
bool Run(_In_ const LPCTSTR lpCmdLine, _In_ const int nShowCmd)
{
if (RootWindow::Register() == 0)
{
MessageBox(NULL, TEXT("Error registering window class"), APPNAME, MB_ICONERROR | MB_OK);
return false;
}
RootWindow* prw = RootWindow::Create();
if (prw == nullptr)
{
MessageBox(NULL, TEXT("Error creating root window"), APPNAME, MB_ICONERROR | MB_OK);
return false;
}
ShowWindow(*prw, nShowCmd);
return true;
}