forked from reupen/ui_helpers
/
win32_helpers.cpp
498 lines (436 loc) · 13.4 KB
/
win32_helpers.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "stdafx.h"
class param_utf16_from_utf8 : public pfc::stringcvt::string_wide_from_utf8 {
bool is_null;
WORD low_word;
public:
param_utf16_from_utf8(const char * p) :
is_null(p == 0),
low_word(HIWORD((DWORD)p) == 0 ? LOWORD((DWORD)p) : 0),
pfc::stringcvt::string_wide_from_utf8(p && HIWORD((DWORD)p) != 0 ? p : "")
{}
inline operator const WCHAR *()
{
return get_ptr();
}
const WCHAR * get_ptr()
{
return low_word ? (const WCHAR*)(DWORD)low_word : is_null ? 0 : pfc::stringcvt::string_wide_from_utf8::get_ptr();
}
};
namespace uih {
const RECT rect_null = { 0,0,0,0 };
bool TestOsVersion(DWORD major, DWORD minor)
{
static OSVERSIONINFO ov;
static bool blah = false;
if (!blah)
{
ov.dwOSVersionInfoSize = sizeof(ov);
GetVersionEx(&ov);
}
return ov.dwMajorVersion >= major && ov.dwMinorVersion >= minor;
}
bool IsVistaOrNewer()
{
return TestOsVersion(6, 0);
}
void SetListViewWindowExplorerTheme(HWND wnd)
{
if (TestOsVersion(6, 0))
{
ListView_SetExtendedListViewStyleEx(wnd, 0x00010000 | LVS_EX_FULLROWSELECT, 0x00010000 | LVS_EX_FULLROWSELECT);
if (TestOsVersion(6, 1))
{
SetWindowTheme(wnd, L"ItemsView", NULL);
//SetWindowTheme(ListView_GetHeader (wnd), L"ItemsView", NULL);
}
else
{
SetWindowTheme(wnd, L"Explorer", NULL);
}
}
}
bool GetKeyboardCuesEnabled()
{
BOOL a = true;
SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &a, 0);
return a != 0;
}
#define TVS_EX_UNKNOWN 0x0001 //
#define TVS_EX_MULTISELECT 0x0002
#define TVS_EX_DOUBLEBUFFER 0x0004 //
#define TVS_EX_NOINDENTSTATE 0x0008
#define TVS_EX_RICHTOOLTIP 0x0010 //
#define TVS_EX_AUTOHSCROLL 0x0020 //
#define TVS_EX_FADEINOUTEXPANDOS 0x0040 //
#define TVS_EX_PARTIALCHECKBOXES 0x0080
#define TVS_EX_EXCLUSIONCHECKBOXES 0x0100
#define TVS_EX_DIMMEDCHECKBOXES 0x0200
#define TVS_EX_DRAWIMAGEASYNC 0x0400 //
void SetTreeViewWindowExplorerTheme(HWND wnd, bool b_reduce_indent)
{
if (IsVistaOrNewer())
{
UINT_PTR stylesex = /*TVS_EX_FADEINOUTEXPANDOS|*/TVS_EX_DOUBLEBUFFER | TVS_EX_AUTOHSCROLL;
UINT_PTR styles = NULL;//TVS_TRACKSELECT;
SendMessage(wnd, TV_FIRST + 44, stylesex, stylesex);
SetWindowTheme(wnd, L"Explorer", NULL);
SetWindowLongPtr(wnd, GWL_STYLE, (GetWindowLongPtr(wnd, GWL_STYLE) & ~(TVS_HASLINES/*|TVS_NOHSCROLL*/)) | styles);
if (b_reduce_indent)
TreeView_SetIndent(wnd, 0xa);
}
}
void RemoveTreeViewWindowExplorerTheme(HWND wnd)
{
if (IsVistaOrNewer())
{
UINT_PTR stylesex = /*TVS_EX_FADEINOUTEXPANDOS|*/TVS_EX_DOUBLEBUFFER;
UINT_PTR styles = NULL;//TVS_TRACKSELECT;
SendMessage(wnd, TV_FIRST + 44, stylesex, NULL);
SetWindowTheme(wnd, L"", NULL);
SetWindowLongPtr(wnd, GWL_STYLE, (GetWindowLongPtr(wnd, GWL_STYLE) | TVS_HASLINES));
}
}
int ListView_InsertColumnText(HWND wnd_lv, UINT index, const TCHAR * text, int cx)
{
LVCOLUMN lvc;
memset(&lvc, 0, sizeof(LVCOLUMN));
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = const_cast<TCHAR*>(text);
lvc.cx = cx;
return ListView_InsertColumn(wnd_lv, index, &lvc);
}
LRESULT ListView_InsertItemText(HWND wnd_lv, UINT item, UINT subitem, const TCHAR * text, bool b_set, LPARAM lp, int image_index)
{
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.mask = LVIF_TEXT | (b_set ? 0 : LVIF_PARAM);
lvi.iItem = item;
lvi.iSubItem = subitem;
lvi.pszText = const_cast<TCHAR*>(text);
lvi.lParam = lp;
if (image_index != I_IMAGENONE)
{
lvi.mask |= LVIF_IMAGE;
lvi.iImage = image_index;
}
return b_set ? ListView_SetItem(wnd_lv, &lvi) : ListView_InsertItem(wnd_lv, &lvi);
}
LRESULT ListView_InsertItemText(HWND wnd_lv, UINT item, UINT subitem, const char * text, bool b_set, LPARAM lp, int image_index)
{
pfc::stringcvt::string_os_from_utf8 wide(text);
return ListView_InsertItemText(wnd_lv, item, subitem, const_cast<TCHAR*>(wide.get_ptr()), b_set, lp, image_index);
}
HTREEITEM TreeView_InsertItemSimple(HWND wnd_tree, const char * sz_text, LPARAM data, DWORD state, HTREEITEM ti_parent, HTREEITEM ti_after, bool b_image, UINT image, UINT integral_height)
{
return TreeView_InsertItemSimple(wnd_tree, pfc::stringcvt::string_os_from_utf8(sz_text), data, state, ti_parent, ti_after, b_image, image, integral_height);
}
HTREEITEM TreeView_InsertItemSimple(HWND wnd_tree, const WCHAR * sz_text, LPARAM data, DWORD state, HTREEITEM ti_parent, HTREEITEM ti_after, bool b_image, UINT image, UINT integral_height)
{
TVINSERTSTRUCT is;
memset(&is, 0, sizeof(is));
is.hParent = ti_parent;
is.hInsertAfter = ti_after;
is.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_STATE | (b_image ? TVIF_IMAGE | TVIF_SELECTEDIMAGE : NULL) | (integral_height > 1 ? TVIF_INTEGRAL : NULL);
is.item.pszText = const_cast<WCHAR*>(sz_text);
is.item.state = state;
is.item.stateMask = state;
is.item.lParam = data;
is.item.iImage = image;
is.item.iSelectedImage = image;
is.itemex.iIntegral = integral_height;
return TreeView_InsertItem(wnd_tree, &is);
}
t_size TreeView_GetChildIndex(HWND wnd_tv, HTREEITEM ti)
{
HTREEITEM item = ti;
unsigned n = 0;
while (item = TreeView_GetPrevSibling(wnd_tv, item))
n++;
return n;
}
BOOL
FileTimeToLocalFileTime2(
__in CONST FILETIME *lpFileTime,
__out LPFILETIME lpLocalFileTime
)
{
SYSTEMTIME stUTC, stLocal;
memset(&stUTC, 0, sizeof(stUTC));
memset(&stLocal, 0, sizeof(stLocal));
FileTimeToSystemTime(lpFileTime, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
return SystemTimeToFileTime(&stLocal, lpLocalFileTime);
}
FILETIME filetimestamp_to_FileTime(t_filetimestamp time)
{
FILETIME ret;
ret.dwLowDateTime = (DWORD)(time & 0xFFFFFFFF);
ret.dwHighDateTime = (DWORD)(time >> 32);
return ret;
}
void FormatDate(t_filetimestamp time, std::basic_string<TCHAR> & str, bool b_convert_to_local)
{
FILETIME ft1 = filetimestamp_to_FileTime(time), ft2 = ft1;
if (b_convert_to_local)
FileTimeToLocalFileTime2(&ft1, &ft2);
SYSTEMTIME st;
FileTimeToSystemTime(&ft2, &st);
int size = GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, NULL, NULL);
int size2 = GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, NULL, NULL);
pfc::array_t<TCHAR> buf, buf2;
buf.set_size(size);
buf2.set_size(size2);
GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, buf.get_ptr(), size);
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, buf2.get_ptr(), size2);
str = _T("");
str += buf.get_ptr();
str += _T(" ");
str += buf2.get_ptr();
}
BOOL SetProcessDpiAware()
{
typedef BOOL(WINAPI* SETPROCESSDPIAWAREPROC)();
HINSTANCE hinstDll = LoadLibrary(_T("user32.dll"));
if (hinstDll)
{
SETPROCESSDPIAWAREPROC pSetProcessDPIAware = (SETPROCESSDPIAWAREPROC)GetProcAddress(hinstDll, "SetProcessDPIAware");
if (pSetProcessDPIAware)
{
return pSetProcessDPIAware();
}
}
return FALSE;
}
SIZE get_system_dpi()
{
HDC dc = GetDC(0);
SIZE ret = { GetDeviceCaps(dc, LOGPIXELSX), GetDeviceCaps(dc, LOGPIXELSY) };
ReleaseDC(0, dc);
return ret;
}
SIZE GetSystemDpiCached()
{
static const SIZE size = get_system_dpi();
return size;
}
int ScaleDpiValue(int value, unsigned original_dpi)
{
return MulDiv(value, GetSystemDpiCached().cx, original_dpi);
}
HRESULT GetComCtl32Version(DLLVERSIONINFO2 & p_dvi, pfc::string_base * p_path_out)
{
static bool have_version = false;
static HRESULT rv = E_FAIL;
static DLLVERSIONINFO2 g_dvi;
if (!have_version || p_path_out)
{
HINSTANCE hinstDll = LoadLibrary(_T("comctl32.dll"));
if (hinstDll)
{
if (p_path_out)
uGetModuleFileName(hinstDll, *p_path_out);
if (!have_version)
{
DLLGETVERSIONPROC pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll, "DllGetVersion");
if (pDllGetVersion)
{
memset(&g_dvi, 0, sizeof(DLLVERSIONINFO2));
g_dvi.info1.cbSize = sizeof(DLLVERSIONINFO2);
rv = (*pDllGetVersion)(&g_dvi.info1);
if (FAILED(rv))
{
memset(&g_dvi, 0, sizeof(DLLVERSIONINFO));
g_dvi.info1.cbSize = sizeof(DLLVERSIONINFO);
rv = (*pDllGetVersion)(&g_dvi.info1);
}
}
have_version = true;
}
FreeLibrary(hinstDll);
}
}
p_dvi = g_dvi;
return rv;
}
BOOL ShellNotifyIconSimple(DWORD dwMessage, HWND wnd, UINT id, UINT callbackmsg, HICON icon,
const char * tip, const char * balloon_title, const char * balloon_msg)
{
//param_utf16_from_utf8 wtip(tip), wbtitle(balloon_title), wbmsg(balloon_msg);
NOTIFYICONDATA nid;
memset(&nid, 0, sizeof(nid));
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.hWnd = wnd;
nid.uID = id;
nid.uCallbackMessage = callbackmsg;
nid.hIcon = icon;
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | (balloon_msg ? NIF_INFO : NULL);
if (tip)
wcscpy_s(nid.szTip, pfc::stringcvt::string_wide_from_utf8(tip).get_ptr());
if (balloon_title)
wcscpy_s(nid.szInfoTitle, pfc::stringcvt::string_wide_from_utf8(balloon_title).get_ptr());
if (balloon_msg)
wcscpy_s(nid.szInfo, pfc::stringcvt::string_wide_from_utf8(balloon_msg).get_ptr());
nid.uTimeout = 10000;
nid.dwInfoFlags = NIIF_INFO;
return Shell_NotifyIcon(dwMessage, &nid);
}
BOOL run_action(DWORD action, NOTIFYICONDATA * data)
{
if (Shell_NotifyIcon(action, data)) return TRUE;
if (action == NIM_MODIFY)
{
if (Shell_NotifyIcon(NIM_ADD, data)) return TRUE;
}
return FALSE;
}
BOOL ShellNotifyIcon(DWORD action, HWND wnd, UINT id, UINT version, UINT callbackmsg, HICON icon, const char * tip)
{
NOTIFYICONDATA nid;
memset(&nid, 0, sizeof(nid));
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.hWnd = wnd;
nid.uID = id;
nid.uFlags = 0;
if (action & NIM_SETVERSION)
nid.uVersion = version;
if (callbackmsg)
{
nid.uFlags |= NIF_MESSAGE;
nid.uCallbackMessage = callbackmsg;
}
if (icon)
{
nid.uFlags |= NIF_ICON;
nid.hIcon = icon;
}
if (tip)
{
nid.uFlags |= NIF_TIP;
_tcsncpy_s(nid.szTip, pfc::stringcvt::string_os_from_utf8(tip), tabsize(nid.szTip) - 1);
}
return run_action(action, &nid);
}
BOOL ShellNotifyIconEx(DWORD action, HWND wnd, UINT id, UINT callbackmsg, HICON icon, const char * tip, const char * balloon_title, const char * balloon_msg)
{
NOTIFYICONDATA nid;
memset(&nid, 0, sizeof(nid));
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.hWnd = wnd;
nid.uID = id;
if (callbackmsg)
{
nid.uFlags |= NIF_MESSAGE;
nid.uCallbackMessage = callbackmsg;
}
if (icon)
{
nid.uFlags |= NIF_ICON;
nid.hIcon = icon;
}
if (tip)
{
nid.uFlags |= NIF_TIP;
_tcsncpy_s(nid.szTip, pfc::stringcvt::string_os_from_utf8(tip), tabsize(nid.szTip) - 1);
}
nid.dwInfoFlags = NIIF_INFO | NIIF_NOSOUND;
//if (balloon_title || balloon_msg)
{
nid.uFlags |= NIF_INFO;
if (balloon_title) _tcsncpy_s(nid.szInfoTitle, pfc::stringcvt::string_os_from_utf8(balloon_title), tabsize(nid.szInfoTitle) - 1);
if (balloon_msg) _tcsncpy_s(nid.szInfo, pfc::stringcvt::string_os_from_utf8(balloon_msg), tabsize(nid.szInfo) - 1);
}
return run_action(action, reinterpret_cast<NOTIFYICONDATA*>(&nid));
}
int ComboBox_AddStringData(HWND wnd, const TCHAR * str, LPARAM data)
{
int index = ComboBox_AddString(wnd, str);
ComboBox_SetItemData(wnd, index, data);
return index;
}
void RegisterShellHookWindowHelper(HWND wnd)
{
typedef BOOL(WINAPI * RegisterShellHookWindowProc)(HWND);
HINSTANCE inst = LoadLibrary(L"user32.dll");
if (inst)
{
RegisterShellHookWindowProc pRegisterShellHookWindow = (RegisterShellHookWindowProc)GetProcAddress(inst, "RegisterShellHookWindow");
if (pRegisterShellHookWindow)
pRegisterShellHookWindow(wnd);
FreeLibrary(inst);
}
}
void DeregisterShellHookWindowHelper(HWND wnd)
{
typedef BOOL(WINAPI * DeregisterShellHookWindowProc)(HWND);
HINSTANCE inst = LoadLibrary(L"user32.dll");
if (inst)
{
DeregisterShellHookWindowProc pDeregisterShellHookWindow = (DeregisterShellHookWindowProc)GetProcAddress(inst, "DeregisterShellHookWindow");
if (pDeregisterShellHookWindow)
pDeregisterShellHookWindow(wnd);
FreeLibrary(inst);
}
}
int ComboBox_FindItemByData(HWND wnd, t_size id)
{
t_size i, count = ComboBox_GetCount(wnd);
for (i = 0; i < count; i++)
if (ComboBox_GetItemData(wnd, i) == id)
return i;
return -1;
}
int Rebar_FindItemById(HWND wnd, unsigned id)
{
/* Avoid RB_IDTOINDEX for backwards compatibility */
REBARBANDINFO rbbi;
memset(&rbbi, 0, sizeof(rbbi));
rbbi.cbSize = sizeof(rbbi);
rbbi.fMask = RBBIM_ID;
UINT count = uSendMessage(wnd, RB_GETBANDCOUNT, 0, 0);
unsigned n;
for (n = 0; n < count; n++)
{
uSendMessage(wnd, RB_GETBANDINFO, n, (long)&rbbi);
if (rbbi.wID == id) return n;
}
return -1;
}
void Rebar_ShowAllBands(HWND wnd)
{
UINT count = uSendMessage(wnd, RB_GETBANDCOUNT, 0, 0);
unsigned n;
for (n = 0; n < count; n++)
{
uSendMessage(wnd, RB_SHOWBAND, n, TRUE);
}
}
void HandleModernBackgroundPaint(HWND wnd, HWND wnd_button)
{
PAINTSTRUCT ps;
HDC dc = BeginPaint(wnd, &ps);
if (dc)
{
RECT rc_client, rc_button;
GetClientRect(wnd, &rc_client);
RECT rc_fill = rc_client;
if (wnd_button)
{
GetWindowRect(wnd_button, &rc_button);
rc_fill.bottom -= RECT_CY(rc_button) + 11;
rc_fill.bottom -= 11;
}
FillRect(dc, &rc_fill, GetSysColorBrush(COLOR_WINDOW));
if (wnd_button)
{
rc_fill.top = rc_fill.bottom;
rc_fill.bottom += 1;
FillRect(dc, &rc_fill, GetSysColorBrush(COLOR_3DLIGHT));
}
rc_fill.top = rc_fill.bottom;
rc_fill.bottom = rc_client.bottom;
FillRect(dc, &rc_fill, GetSysColorBrush(COLOR_3DFACE));
EndPaint(wnd, &ps);
}
}
}