-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomMenu.c
More file actions
375 lines (340 loc) · 10.6 KB
/
CustomMenu.c
File metadata and controls
375 lines (340 loc) · 10.6 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
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
// CustomMenu.c by Amash Shafi Jami
#include "CustomMenu.h"
#include "hoverBox.h"
#include "MenuPopUp.h"
HWND hCurrentPopup = NULL;
void CloseCurrentPopup()
{
if (hCurrentPopup) {
ShowWindow(hCurrentPopup, SW_HIDE);
hCurrentPopup = NULL;
}
}
void OnHelpClicked(LPCSTR title)
{
MessageBoxA(NULL,
"This is a custom menu project\nby Amash Shafi Jami",
"About",
MB_OK | MB_ICONINFORMATION);
}
void OnExitClicked(LPCSTR title)
{
PostQuitMessage(0);
}
void OnSettingsClicked(LPCSTR title)
{
MessageBoxA(NULL, "Settings Clicked!", "Info", MB_OK | MB_ICONINFORMATION);
}
void OnFileClicked(LPCSTR title)
{
MessageBoxA(NULL, "File Clicked!", "Info", MB_OK | MB_ICONINFORMATION);
}
ColorSchemeStruct Scheme_Normal = {
RGB(230,230,230),
RGB(248,248,248),
RGB(200,200,200),
1
};
PopupMenuItemStructArray* BuildHelpPMISA(void)
{
PopupMenuItemStructArray* pmisa = getBasePMISA();
PMISA_AddEx(pmisa, "About", OnHelpClicked, Scheme_Normal);
PMISA_AddEx(pmisa, "Settings", OnSettingsClicked, Scheme_Normal);
PMISA_AddEx(pmisa, "Exit", OnExitClicked, Scheme_Normal);
return pmisa;
}
PopupMenuItemStructArray* BuildFilePMISA(void)
{
PopupMenuItemStructArray* pmisa = getBasePMISA();
PMISA_AddEx(pmisa, "New", OnFileClicked, Scheme_Normal);
PMISA_AddEx(pmisa, "Open", OnFileClicked, Scheme_Normal);
PMISA_AddEx(pmisa, "Save", OnFileClicked, Scheme_Normal);
PMISA_AddEx(pmisa, "Exit", OnExitClicked, Scheme_Normal);
return pmisa;
}
HWND fnCreateMenuItem(
HWND parent,
MenuItemStruct* mis,
LPCSTR szMenuItemClass,
LPCSTR title,
int WidthFactor
){
HWND hMenuItem = CreateWindow(
szMenuItemClass,
title,
WS_CHILD | WS_VISIBLE,
mis->cxOffset,
0,
WidthFactor,
CY_MENU_ITEM,
parent,
NULL,
GetModuleHandle(NULL),
mis
);
return hMenuItem;
}
int fnGetHoverBoxWidth(HDC hdc, MenuItemStruct* mis){
SIZE sz;
int len = lstrlenA(mis->szTitle);
if (len == 0) return 0;
GetTextExtentPoint32A(hdc, mis->szTitle, len, &sz);
return sz.cx + 10;
}
MenuItemStruct* fnMenuWinProcGetMIS(HWND hWnd)
{
MenuItemStruct* mis =
(MenuItemStruct*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (!mis)
return GetBaseMIS();
return mis;
}
LRESULT CALLBACK fnMenuWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
MenuItemStruct* mis;
PAINTSTRUCT ps;
TEXTMETRIC tm;
HDC hdc;
static int cxHBox;
static HFONT hFont = NULL;
static int cxClient, cyClient;
static int cxChar, CxCaps, cyChar;
switch (msg)
{
case WM_LBUTTONUP:{
mis = fnMenuWinProcGetMIS(hWnd);
ShowWindow(hCurrentPopup,HIDE_WINDOW);
hCurrentPopup = NULL;
if (!mis->pmisa || mis->pmisa->count == 0)
return 0;
RECT rc;
GetWindowRect(hWnd, &rc);
POINT p = {
rc.left,
rc.bottom
};
hCurrentPopup = fnCreateMenuPopUp(p,mis->WidthFactor,mis->pmisa);
return 0;
}
case WM_CREATE:{
CREATESTRUCT *cs = (CREATESTRUCT*)(lParam);
mis = (MenuItemStruct*)cs->lpCreateParams;
SetWindowLongPtr(hWnd,GWLP_USERDATA,(LONG_PTR)mis);
hdc = GetDC(hWnd);
GetTextMetrics(hdc,&tm);
cxChar = tm.tmAveCharWidth;
CxCaps = (tm.tmPitchAndFamily & 1? 3 : 2)* cxChar /2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
if (!hFont) {
hFont = CreateFont(
-16,
0, 0, 0,
FW_MEDIUM,
FALSE, FALSE, FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
DEFAULT_PITCH | FF_SWISS,
TEXT("Segoe UI")
);
}
ReleaseDC(hWnd,hdc);
return 0;
}
case WM_SIZE:{
cxClient = GET_X_LPARAM(lParam);
cyClient = GET_Y_LPARAM(lParam);
mis = fnMenuWinProcGetMIS(hWnd);
SetWindowPos(hWnd,NULL,mis->cxOffset,0,cxClient,cyClient,0);
return 1;
}
case WM_PAINT: {
mis = fnMenuWinProcGetMIS(hWnd);
hdc = BeginPaint(hWnd, &ps);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(40, 40, 40));
RECT rc;
GetClientRect(hWnd, &rc);
int availableWidth = rc.right - rc.left - (2*mis->cxTextPadding);
SIZE sz;
int len = lstrlenA(mis->szTitle);
int charsThatFit = 0;
for (int i = 1; i <= len; i++)
{
GetTextExtentPoint32A(
hdc,
mis->szTitle,
i,
&sz
);
if (sz.cx > availableWidth)
break;
charsThatFit = i;
}
if(fnIsPsuedoStringRequired(charsThatFit,mis->szTitle)){
mis->isStrLong = TRUE;
cxHBox = fnGetHoverBoxWidth(hdc,mis);
}else{
mis->isStrLong = FALSE;
}
DrawText(
hdc,
fnGetPseudoString(charsThatFit,mis->szTitle),
-1,
&rc,
DT_CENTER | DT_VCENTER | DT_SINGLELINE
);
SelectObject(hdc, hOldFont);
HPEN hPen = CreatePen(
PS_SOLID,
mis->ColScheme.BorderWidth,
mis->ColScheme.Border
);
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
MoveToEx(hdc, 0, cyClient - 1, NULL);
LineTo(hdc, cxClient, cyClient - 1);
if (mis->isRightMost == TRUE){
MoveToEx(
hdc,
cxClient-mis->ColScheme.BorderWidth,
cyClient,
NULL
);
LineTo(
hdc,
cxClient-mis->ColScheme.BorderWidth,
0
);
}
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
EndPaint(hWnd, &ps);
return 0;
}
case WM_MOUSEMOVE:
{
mis = fnMenuWinProcGetMIS(hWnd);
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
ClientToScreen(hWnd, &pt);
if (!mis->isHovered) {
mis->isHovered = TRUE;
InvalidateRect(hWnd, NULL, TRUE);
if (mis->isStrLong) {
mis->hHoverBox = fnCreateHoverBox(
NULL,
pt.x,
pt.y + CY_MENU_ITEM,
mis->szTitle
);
}
TRACKMOUSEEVENT tme = { sizeof(tme) };
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hWnd;
TrackMouseEvent(&tme);
}
if (mis->hHoverBox) {
SetWindowPos(
mis->hHoverBox,
HWND_TOPMOST,
pt.x,
pt.y + CY_MENU_ITEM,
0, 0,
SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW
);
}
return 0;
}
return 0;
case WM_MOUSELEAVE:
mis = fnMenuWinProcGetMIS(hWnd);
if(mis->isHovered != FALSE){
mis->isHovered = FALSE;
if (mis->hHoverBox) {
DestroyWindow(mis->hHoverBox);
mis->hHoverBox = NULL;
}
InvalidateRect(hWnd,NULL,TRUE);
}
return 0;
case WM_ERASEBKGND:
mis = fnMenuWinProcGetMIS(hWnd);
hdc = (HDC)(wParam);
RECT rect;
GetClientRect(hWnd,&rect);
COLORREF cRef = (mis->isHovered == TRUE) ? (mis->ColScheme.Active) : (mis->ColScheme.Passive);
HBRUSH br = (HBRUSH)CreateSolidBrush(cRef);
FillRect(hdc,&rect,br);
DeleteObject(br);
return 1;
default:
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
void fnCreateMenu(HWND parent){
int WidthFactor = 100;
TCHAR szMenuItemClass[] = _TEXT("Menu Item About");
WNDCLASS wndClassMenu;
wndClassMenu.cbClsExtra = 0;
wndClassMenu.cbWndExtra = 0;
// wndClassMenu.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
wndClassMenu.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(255,255,255));
// wndClassMenu.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(220,220,220));
wndClassMenu.hCursor = LoadCursor(NULL,IDC_ARROW);
wndClassMenu.hIcon = NULL;
wndClassMenu.hInstance = GetModuleHandle(NULL);
wndClassMenu.lpfnWndProc = fnMenuWinProc;
wndClassMenu.lpszClassName = szMenuItemClass;
wndClassMenu.lpszMenuName = NULL;
wndClassMenu.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClass(&wndClassMenu)){
ERR_CHAR_RESERVE;
ERR_CHAR_FILL_1_VALUE(
"Cannot Register Menu Class Of name \"%s\"",
wndClassMenu.lpszClassName
);
MessageBox(
NULL,
ERR_CHAR,
_TEXT("ERROR : Cant Register Window Class"),
MB_ICONERROR | MB_APPLMODAL
);
return;
}
LPCTSTR arrMenuItemNames[COUNTMENUITEMS] = {
_TEXT("About"),
_TEXT("File"),
_TEXT("Edit"),
_TEXT("Help"),
_TEXT("Info"),
_TEXT("Testing Long Long Texts"),
_TEXT("STRThisIsAnotherLongLineOfTextHeheHeHeHeEND"),
};
HWND MENUITEMS[COUNTMENUITEMS];
for(int i = 0; i < COUNTMENUITEMS; i++){
MenuItemStruct* mis = GetBaseMIS();
mis->ColScheme.Active = RGB(220,20,20);
mis->ColScheme.Border = RGB(220,20,220);
if (i == (COUNTMENUITEMS-1)) mis->isRightMost = TRUE;
MenuItemAssignTitle(mis,arrMenuItemNames[i]);
mis->cxOffset = i * WidthFactor;
mis->isHovered = FALSE;
if (lstrcmpi(arrMenuItemNames[i], "Help") == 0)
{
mis->pmisa = BuildHelpPMISA();
}
else if(lstrcmpi(arrMenuItemNames[i], "File") == 0) mis->pmisa = BuildFilePMISA();
else
{
mis->pmisa = getBasePMISA();
}
MENUITEMS[i] = fnCreateMenuItem(
parent,
mis,
szMenuItemClass,
arrMenuItemNames[i],
WidthFactor
);
}
}