forked from fdwr/TextLayoutSampler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageBoxShaded.ixx
More file actions
325 lines (263 loc) · 10.2 KB
/
Copy pathMessageBoxShaded.ixx
File metadata and controls
325 lines (263 loc) · 10.2 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
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
//+---------------------------------------------------------------------------
// Window layout functions.
//
// History: 2008-02-11 Dwayne Robinson - Created
//----------------------------------------------------------------------------
#if USE_CPP_MODULES
module;
#endif
#include "precomp.h"
#include "resource.h"
#include <string>
#include <Windows.h>
#if USE_CPP_MODULES
import Common.ArrayRef;
import Common.String;
import WindowUtility;
export module MessageBoxShaded;
export
{
#include "MessageBoxShaded.h"
}
#else
#include "Common.ArrayRef.h"
#include "Common.String.h"
#include "WindowUtility.h"
#include "MessageBoxShaded.h"
#endif
////////////////////////////////////////
namespace
{
static RECT GetStaticDefaultSize(
HWND hwnd,
RECT rect
)
{
RECT defaultRect = rect;
// Measure the text height and width.
int32_t textLength = Static_GetTextLength(hwnd);
if (textLength >= 0)
{
std::u16string text;
text.resize(textLength + 1);
Static_GetText(hwnd, OUT ToWChar(&text[0]), textLength + 1);
HFONT font = GetWindowFont(hwnd);
HDC hdc = GetDC(hwnd); // Get the static control's DC.
HFONT oldFont = SelectFont(hdc, font);
DrawText(hdc, ToWChar(text.data()), textLength, IN OUT &defaultRect, DT_CALCRECT|DT_NOPREFIX|DT_WORDBREAK);
SelectObject(hdc, oldFont);
ReleaseDC(hwnd, hdc); // free the DC
}
return defaultRect;
}
}
int32_t MessageBoxShaded::Show(
HWND ownerHwnd,
_In_z_ const char16_t* text,
_In_z_ const char16_t* caption,
uint32_t type
)
{
// Note the type parameter exists to simplify using this as a drop-in
// substitute, but it actually ignores most of the flags.
// It only accepts MB_OK, none of the other Yes/No/Cancel variants.
HMODULE moduleHandle = nullptr;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<const wchar_t*>(&MessageBoxShaded::Show), OUT &moduleHandle);
MSGBOXPARAMS params = {};
params.hwndOwner = ownerHwnd;
params.lpszText = ToWChar(text);
params.lpszCaption = ToWChar(caption);
params.dwStyle = type;
auto result = ::DialogBoxParam(moduleHandle, MAKEINTRESOURCE(IddMessageDialog), ownerHwnd, &MessageBoxShaded::StaticDialogProc, (LPARAM)¶ms);
return static_cast<int32_t>(result);
}
MessageBoxShaded::MessageBoxShaded(HWND hwnd)
: hwnd_(hwnd)
{
}
INT_PTR MessageBoxShaded::Initialize(MSGBOXPARAMS const& params)
{
HWND iconHwnd = GetDlgItem(hwnd_, IdcMessageDialogIcon);
HICON icon = nullptr;
if (params.dwStyle & MB_USERICON)
{
icon = LoadIcon(nullptr, IDI_WARNING);
}
else
{
// Convert the message box type enum into an icon id.
INT32 iconId = int(reinterpret_cast<ULONG_PTR>(IDI_APPLICATION)) + ((params.dwStyle & 0x00000070) >> 4);
icon = LoadIcon(nullptr, MAKEINTRESOURCE(iconId));
}
Static_SetIcon(iconHwnd, icon);
Static_SetText(GetDlgItem(hwnd_, IdcMessageDialogMessage), params.lpszText);
// Check for either right to left (RLM) or left to right mark (LRM).
if ((params.lpszText[0] & 0x200E) == 0x200E)
{
UINT32 extendedStyle = GetWindowLong(hwnd_, GWL_EXSTYLE);
if (params.lpszText[0] == 0x200E && params.lpszText[1] == 0x200E) extendedStyle &= ~WS_EX_LAYOUTRTL;
if (params.lpszText[0] == 0x200F && params.lpszText[1] == 0x200F) extendedStyle |= WS_EX_LAYOUTRTL;
SetWindowLong(hwnd_, GWL_EXSTYLE, extendedStyle);
}
// Overlay the modal dialog atop the owner window with the same size.
RECT ownerRect = {};
GetWindowRect(params.hwndOwner, OUT &ownerRect);
if (IsMaximized(params.hwndOwner))
{
// Avoid the border from spilling over onto the adjacent monitors.
// This compensates for a GetWindowRect backwards compat annoyance
// where it returns a size larger than the actual size because back
// before the days of multiple monitors, it didn't matter. So to
// get the true size, clamp to the work area.
// todo::: Consider using DWMWA_EXTENDED_FRAME_BOUNDS instead.
MONITORINFO monitorInfo = {};
monitorInfo.cbSize = sizeof(monitorInfo);
HMONITOR monitor = MonitorFromWindow(params.hwndOwner, MONITOR_DEFAULTTONEAREST);
GetMonitorInfo(monitor, OUT &monitorInfo);
IntersectRect(OUT &ownerRect, &ownerRect, &monitorInfo.rcWork);
}
SetWindowPos(hwnd_, nullptr, ownerRect.left, ownerRect.top, ownerRect.right - ownerRect.left, ownerRect.bottom - ownerRect.top, SWP_NOZORDER|SWP_NOACTIVATE);
SetLayeredWindowAttributes(hwnd_, 0, 224, LWA_ALPHA);
SetFocus(hwnd_);
return true;
}
INT_PTR CALLBACK MessageBoxShaded::StaticDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
Self* window = GetClassFromWindow<Self>(hwnd);
if (window == nullptr)
{
window = new(std::nothrow) Self(hwnd);
if (window == nullptr)
{
return -1; // failed creation
}
::SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)window);
}
const DialogProcResult result = window->DialogProc(hwnd, message, wParam, lParam);
if (result.handled)
{
::SetWindowLongPtr(hwnd, DWLP_MSGRESULT, result.value);
}
return result.handled;
}
DialogProcResult CALLBACK MessageBoxShaded::DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
Initialize(*reinterpret_cast<MSGBOXPARAMS*>(lParam));
return false; // Do not attempt to set key focus.
case WM_COMMAND:
return ProcessCommand(hwnd, message, wParam, lParam);
case WM_NOTIFY:
return ProcessNotification(hwnd, message, wParam, lParam);
case WM_SIZE:
Resize();
break;
case WM_ERASEBKGND:
{
// Shade the background.
RECT rect = {};
HDC hdc = (HDC)wParam;
GetClientRect(hwnd, OUT &rect);
FillRect(hdc, &rect, GetSysColorBrush(COLOR_3DSHADOW));
}
return true;
case WM_ACTIVATE:
if (wParam == WA_INACTIVE)
SetTimer(hwnd, IDCANCEL, 30, nullptr);
// Set a short timer rather than calling EndDialog directly (which
// confuses Windows and fails to reset the focus to the control when
// Alt+Tabbing back) or PostMessage'ing directly (which confuses the
// Alt+Tab order and switches right back to the current window).
return false; // Let the default procedure handle it.
case WM_TIMER: // A timer was set to dismiss the dialog.
KillTimer(hwnd, wParam);
PostMessage(hwnd, WM_COMMAND, wParam, 0);
break;
case WM_LBUTTONDOWN:
PostMessage(hwnd, WM_COMMAND, IDCANCEL, 0);
break;
default:
return false;
}
return true;
}
DialogProcResult CALLBACK MessageBoxShaded::ProcessCommand(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
uint32_t wmId = LOWORD(wParam);
uint32_t wmEvent = HIWORD(wParam);
UNREFERENCED_PARAMETER(wmEvent);
switch (wmId)
{
case IDOK:
case IDCANCEL:
case IDABORT:
case IDRETRY:
case IDIGNORE:
case IDYES:
case IDNO:
case IDCLOSE:
case IDHELP:
case IDTRYAGAIN:
case IDCONTINUE:
{
EndDialog(hwnd, wmId); // not DestroyWindow
// Note, do not reference the class after this.
}
break;
default:
return DialogProcResult(false, -1); // unhandled
}
return DialogProcResult(true, 0); // handled
}
DialogProcResult CALLBACK MessageBoxShaded::ProcessNotification(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int idCtrl = (int) wParam;
NMHDR const& nmh = *(NMHDR*)lParam;
switch (idCtrl)
{
//case IdcActionsList:
// switch (nmh.code)
// {
// case NM_RETURN:
// case NM_DBLCLK:
// break;
// }
// break;
default:
return false;
#pragma warning(suppress: 4065) // Yes, I know there are no case labels above.
} // switch idCtrl
return true;
}
void MessageBoxShaded::Resize()
{
// Resize contained controls.
long const spacing = 16;
RECT clientRect;
GetClientRect(hwnd_, OUT &clientRect);
WindowPosition windowPositions[] = {
WindowPosition( GetDlgItem(hwnd_, IdcMessageDialogBackground), PositionOptionsIgnored),
WindowPosition( GetDlgItem(hwnd_, IdcMessageDialogIcon), PositionOptionsAlignLeft|PositionOptionsAlignTop),
WindowPosition( GetDlgItem(hwnd_, IdcMessageDialogMessage), PositionOptionsAlignTop),
WindowPosition( GetDlgItem(hwnd_, IDOK), PositionOptionsUseSlackWidth|PositionOptionsPreNewLine),
};
// Measure the text height and width.
windowPositions[2].rect = GetStaticDefaultSize(
windowPositions[2].hwnd,
windowPositions[2].rect
);
// Call once to compute size only.
RECT groupRect = WindowPosition::ReflowGrid(windowPositions, int(countof(windowPositions)), clientRect, spacing, 0, PositionOptionsFlowHorizontal|PositionOptionsUnwrapped, /*queryOnly*/ true);
// Align all the controls in the middle so the horizontal band is centered.
WindowPosition groupPosition(nullptr, PositionOptionsAlignHCenter|PositionOptionsAlignVCenter, groupRect);
IntersectRect(OUT &groupPosition.rect, &groupPosition.rect, &clientRect);
groupPosition.AlignToRect(clientRect);
// Compute final layout.
WindowPosition::ReflowGrid(windowPositions, int(countof(windowPositions)), groupPosition.rect, spacing, 0, PositionOptionsFlowHorizontal|PositionOptionsUnwrapped);
// Assign the background layer's rect, and give it padding.
InflateRect(IN OUT &groupPosition.rect, spacing, spacing);
windowPositions[0].rect = groupPosition.rect;
WindowPosition::Update(windowPositions, int(countof(windowPositions)));
}