forked from hunterboy2023/win32-custom-menubar-aero-theme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwin32-custom-menubar-aero-theme.cpp
473 lines (418 loc) · 14.8 KB
/
win32-custom-menubar-aero-theme.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// win32-custom-menubar-aero-theme.cpp : Defines the entry point for the application.
//
#include "win32-custom-menubar-aero-theme.h"
#include "utils.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
static HHOOK hook_ = NULL;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
#ifndef NDEBUG
// set a hook for debugging purposes only
ASSERT(!hook_);
hook_ = SetWindowsHookEx(WH_CBT, CBTProc, NULL, GetCurrentThreadId());
ASSERT(hook_);
OutputDebugString(L"CBT Hook installed.\n");
#endif
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN32CUSTOMMENUBARAEROTHEME, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32CUSTOMMENUBARAEROTHEME));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if (hook_) {
UnhookWindowsHookEx(hook_);
hook_ = NULL;
OutputDebugString(L"CBT Hook uninstalled.\n");
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW; // | BS_OWNERDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32CUSTOMMENUBARAEROTHEME));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32CUSTOMMENUBARAEROTHEME);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
initDarkMode(); // dark popup menus
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 400, 240, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
setImmersiveDarkMode(hWnd); // dark window title bar
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
// https://github.com/microsoftarchive/msdn-code-gallery-microsoft/blob/master/OneCodeTeam/Owner-drawn%20menu%20item%20in%20C%2B%2B%20Windows%20app%20(CppWindowsOwnerDrawnMenu)/%5BC%2B%2B%5D-Owner-drawn%20menu%20item%20in%20C%2B%2B%20Windows%20app%20(CppWindowsOwnerDrawnMenu)/C%2B%2B/CppWindowsOwnerDrawnMenu/CppWindowsOwnerDrawnMenu.cpp
// Sructure associated with Character menu items
typedef struct tagCHARMENUITEM
{
// Font of text on the menu item.
HFONT hFont;
// The length of the string pointed to by szItemText.
int cchItemText;
// A pointer to a buffer that specifies the text string. The string does
// not need to be null-terminated, because the c parameter specifies the
// length of the string.
wchar_t szItemText[1];
} CHARMENUITEM, * PCHARMENUITEM;
#define MENUITEM_TEXTCOLOR RGB(255,0,0) // or GetSysColor(COLOR_MENUTEXT)
#define MENUITEM_BACKCOLOR RGB(0,255,0) // or GetSysColor(COLOR_MENU)
#define HIGHLIGHT_TEXTCOLOR GetSysColor(COLOR_HIGHLIGHTTEXT)
#define HIGHLIGHT_BACKCOLOR GetSysColor(COLOR_HIGHLIGHT)
HFONT CreateMenuItemFont()
{
LOGFONT lf = { sizeof(lf) };
wcscpy_s(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), L"Times New Roman");
lf.lfHeight = 20;
return CreateFontIndirect(&lf);
}
static HFONT menuItemFont = CreateMenuItemFont();
void prepareMenu(HMENU hMenu) {
BOOL fSucceeded = TRUE;
PCHARMENUITEM pcmi = NULL;
for (UINT uID = 0; uID < GetMenuItemCount(hMenu); uID++)
{
MENUITEMINFO mii = { sizeof(mii) };
// To retrieve a menu item of type MFT_STRING, first find the length
// of the string by setting the dwTypeData member of MENUITEMINFO to
// NULL and then calling GetMenuItemInfo. The value of cch is the
// length of the menu item text.
mii.fMask = MIIM_STRING;
mii.dwTypeData = NULL;
if (!GetMenuItemInfo(hMenu, uID, TRUE, &mii))
{
int a = GetLastError();
fSucceeded = FALSE;
break;
}
// Then allocate a buffer of this size.
pcmi = (PCHARMENUITEM)LocalAlloc(LPTR,
sizeof(*pcmi) + mii.cch * sizeof(*pcmi->szItemText));
if (NULL == pcmi)
{
fSucceeded = FALSE;
break;
}
// Place the pointer to the buffer in dwTypeData, increment cch, and
// call GetMenuItemInfo once again to fill the buffer with the string.
pcmi->cchItemText = mii.cch;
mii.dwTypeData = pcmi->szItemText;
mii.cch++;
if (!GetMenuItemInfo(hMenu, uID, TRUE, &mii))
{
fSucceeded = FALSE;
break;
}
// Create the font used to draw the item.
pcmi->hFont = menuItemFont;
if (NULL == pcmi->hFont)
{
fSucceeded = FALSE;
break;
}
// Change the item to an owner-drawn item, and save the
// address of the item structure as item data.
mii.fMask = MIIM_FTYPE | MIIM_DATA;
mii.fType = MF_OWNERDRAW;
mii.dwItemData = (ULONG_PTR)pcmi;
if (!SetMenuItemInfo(hMenu, uID, TRUE, &mii))
{
fSucceeded = FALSE;
break;
}
}
// Clean up the allocated resource when applicable.
if (!fSucceeded && pcmi)
{
LocalFree(pcmi);
}
}
//
// FUNCTION: OnCreate(HWND, LPCREATESTRUCT)
//
// PURPOSE: Process the WM_CREATE message. Because a menu template cannot
// specify owner-drawn items, the menu initially contains four text menu
// items with the following strings: "Regular," "Bold," "Italic," and
// "Underline." This function changes these to owner-drawn items, and
// attaches to each menu item a CHARMENUITEM structure that will be used
// when the menu item is created and drawn.
//
BOOL OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
HMENU hMenu = GetMenu(hWnd);
prepareMenu(hMenu);
return true;
}
//
// FUNCTION: OnMeasureItem(HWND, MEASUREITEMSTRUCT *)
//
// PURPOSE: Process the WM_MEASUREITEM message. A WM_MEASUREITEM message is
// sent for each owner-drawn menu item the first time it is displayed. The
// application processes this message by selecting the font for the menu
// item into a device context and then determining the space required to
// display the menu item text in that font. The font and menu item text are
// both specified by the menu item's CHARMENUITEM structure (the structure
// defined by the application).
//
void OnMeasureItem(HWND hWnd, MEASUREITEMSTRUCT* lpMeasureItem)
{
// Retrieve the menu item's CHARMENUITEM structure.
PCHARMENUITEM pcmi = (PCHARMENUITEM)lpMeasureItem->itemData;
if (pcmi)
{
// Retrieve a device context for the main window.
HDC hdc = GetDC(hWnd);
// Select the font associated with the item into the main window's
// device context.
HFONT hFontOld = (HFONT)SelectObject(hdc, pcmi->hFont);
// Retrieve the width and height of the item's string, and then copy
// the width and height into the MEASUREITEMSTRUCT structure's
// itemWidth and itemHeight members.
SIZE size;
GetTextExtentPoint32(hdc, pcmi->szItemText, pcmi->cchItemText, &size);
lpMeasureItem->itemWidth = size.cx;
lpMeasureItem->itemHeight = size.cy;
// Restore the original font and release the device context.
SelectObject(hdc, hFontOld);
ReleaseDC(hWnd, hdc);
}
}
//
// FUNCTION: OnDrawItem(HWND, const DRAWITEMSTRUCT *)
//
// PURPOSE: Process the WM_DRAWITEM message by displaying the menu item
// text in the appropriate font. The font and menu item text are both
// specified by the menu item's CHARMENUITEM structure. The application
// selects text and background colors appropriate to the menu item's state.
//
void OnDrawItem(HWND hWnd, const DRAWITEMSTRUCT* lpDrawItem)
{
// Retrieve the menu item's CHARMENUITEM structure.
PCHARMENUITEM pcmi = (PCHARMENUITEM)lpDrawItem->itemData;
ASSERT(pcmi);
if (pcmi)
{
COLORREF clrPrevText, clrPrevBkgnd;
HFONT hFontPrev;
int x, y;
// Set the appropriate foreground and background colors.
if (lpDrawItem->itemState & ODS_SELECTED)
{
clrPrevText = SetTextColor(lpDrawItem->hDC, HIGHLIGHT_TEXTCOLOR);
clrPrevBkgnd = SetBkColor(lpDrawItem->hDC, HIGHLIGHT_BACKCOLOR);
}
else
{
clrPrevText = SetTextColor(lpDrawItem->hDC, MENUITEM_TEXTCOLOR);
clrPrevBkgnd = SetBkColor(lpDrawItem->hDC, MENUITEM_BACKCOLOR);
}
// Determine where to draw and leave space for a check mark.
x = lpDrawItem->rcItem.left;
y = lpDrawItem->rcItem.top;
x += GetSystemMetrics(SM_CXMENUCHECK);
// Select the font and draw the text.
//hFontPrev = (HFONT)SelectObject(lpDrawItem->hDC, pcmi->hFont);
ExtTextOut(lpDrawItem->hDC, x, y, ETO_OPAQUE, &lpDrawItem->rcItem,
pcmi->szItemText, pcmi->cchItemText, NULL);
TCHAR buf[512];
ZeroMemory(buf, 512);
memcpy_s(buf, 512, (void*)pcmi->szItemText, pcmi->cchItemText * sizeof(TCHAR));
OutputDebugString(L"*** OnDrawItem: ");
OutputDebugString(buf);
OutputDebugString(L"\n");
// Restore the original font and colors.
//SelectObject(lpDrawItem->hDC, hFontPrev);
SetTextColor(lpDrawItem->hDC, clrPrevText);
SetBkColor(lpDrawItem->hDC, clrPrevBkgnd);
}
}
void OnInitMenuPopup(HMENU hMenu, UINT nIndex, BOOL bSysMenu)
{
UNREFERENCED_PARAMETER(nIndex);
prepareMenu(hMenu);
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
dbgMsg(hWnd, 0, message, wParam, lParam);
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
default:
break;
}
break;
}
case WM_CREATE:
{
OnCreate(hWnd, (LPCREATESTRUCT)lParam);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_DRAWITEM:
{
OnDrawItem(hWnd, (LPDRAWITEMSTRUCT)lParam);
break;
}
case WM_INITMENUPOPUP:
{
OnInitMenuPopup((HMENU)wParam, LOWORD(lParam), HIWORD(lParam));
break;
}
case WM_KEYDOWN:
{
if (wParam == 70) { // 'f'
//https://devblogs.microsoft.com/oldnewthing/20100412-00/?p=14353
static WINDOWPLACEMENT g_wpPrev = { sizeof(g_wpPrev) };
DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
ASSERT(dwStyle);
if (dwStyle & WS_OVERLAPPEDWINDOW) { // switch to fullscreen
MONITORINFO mi = { sizeof(mi) };
if (GetWindowPlacement(hWnd, &g_wpPrev) &&
GetMonitorInfo(MonitorFromWindow(hWnd,
MONITOR_DEFAULTTOPRIMARY), &mi)) {
SetWindowLong(hWnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
BOOL res = SetWindowPos(hWnd, HWND_TOP,
mi.rcMonitor.left,
mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
ASSERT(res);
}
}
else { // restore / exit from fullscreen
SetWindowLong(hWnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
SetWindowPlacement(hWnd, &g_wpPrev);
SetWindowPos(hWnd, NULL, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
else if (wParam == 81) { // 'q'
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
}
case WM_MEASUREITEM:
{
//if (wParam == 0) { // handle menus only
OnMeasureItem(hWnd, (LPMEASUREITEMSTRUCT)lParam);
//}
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
break;
}
case WM_SHOWWINDOW:
break;
default:
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}