-
Notifications
You must be signed in to change notification settings - Fork 1
/
UWnd.hpp
845 lines (690 loc) · 20.6 KB
/
UWnd.hpp
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#pragma once
//---------------------------------------------------------------------------//
//
// UWnd.hpp
// Class to capsulize window objects
// Copyright (C) 2005-2018 tapetums
//
//---------------------------------------------------------------------------//
#include <utility>
#include <windows.h>
#include <windowsx.h>
#include <strsafe.h>
#include <shellapi.h>
#pragma comment(lib, "shell32.lib") // Shell_NotifyIcon
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
//---------------------------------------------------------------------------//
// Global Variables
//---------------------------------------------------------------------------//
extern UINT WM_NOTIFYICON; // Declare the entity somewhere before use
extern UINT WM_TASKBARCREATED; // Declare the entity somewhere before use
//---------------------------------------------------------------------------//
// Forward Declarations
//---------------------------------------------------------------------------//
namespace tapetums
{
class UWnd;
inline void ShowLastError (LPCTSTR window_title);
inline void AdjustRect (HWND hwnd, INT32* w, INT32* h);
inline void GetRectForMonitor(HWND hwnd, RECT* rect);
inline void GetRectForMonitor(const POINT& pt, RECT* rect);
inline void GetDpiForMonitor (HWND hwnd, POINT* dpi);
inline void GetDpiForMonitor (const POINT& pt,POINT* dpi);
}
//---------------------------------------------------------------------------//
// Classes
//---------------------------------------------------------------------------//
class tapetums::UWnd
{
private: // static members
static constexpr LPCTSTR uwnd_class_name { TEXT("UWnd") };
protected: // members
HWND m_hwnd { nullptr };
public: // accessors
HWND handle() const noexcept { return m_hwnd; }
public: // ctor / dtor
UWnd() noexcept;
virtual ~UWnd() { Destroy(); }
UWnd(const UWnd&) = delete;
UWnd& operator= (const UWnd&) = delete;
UWnd(UWnd&& rhs) noexcept = default;
UWnd& operator =(UWnd&& rhs) noexcept = default;
public: // operators
operator HWND() const noexcept { return m_hwnd; }
public: // methods
ATOM WINAPI Register(LPCTSTR lpszClassName);
HWND WINAPI Create (DWORD style = WS_OVERLAPPEDWINDOW, DWORD styleEx = 0, HWND hwndParent = nullptr, HMENU hMenu = nullptr);
void WINAPI Destroy ();
void WINAPI Close ();
void WINAPI Bounds (INT32 x, INT32 y, INT32 w, INT32 h);
void WINAPI Hide ();
void WINAPI Move (INT32 x, INT32 y);
void WINAPI Refresh ();
void WINAPI Resize (INT32 w, INT32 h);
void WINAPI Show ();
void WINAPI ToCenter();
bool WINAPI AddNotifyIcon (UINT uID, HICON hIcon);
void WINAPI DeleteNotifyIcon (UINT uID);
void WINAPI SetNotifyIconTip (UINT uID, LPCTSTR szTip);
bool WINAPI ShowNotifyIconInfo(UINT uID, DWORD dwInfoFlags, LPCTSTR szInfoTitle, LPCTSTR szInfo, UINT uTimeout = -1);
LRESULT WINAPI Send(UINT msg, WPARAM wp, LPARAM lp) const;
BOOL WINAPI Post(UINT msg, WPARAM wp, LPARAM lp) const;
public: // properties
DWORD WINAPI GetStyle () const;
DWORD WINAPI GetStyleEx () const;
HWND WINAPI GetParent () const;
HFONT WINAPI GetFont () const;
HICON WINAPI GetWindowIcon () const;
HICON WINAPI GetWindowIconSm() const;
SIZE_T WINAPI GetText (LPSTR buf, SIZE_T buf_size) const;
SIZE_T WINAPI GetText (LPWSTR buf, SIZE_T buf_size) const;
void WINAPI SetStyle (DWORD style);
void WINAPI SetStyleEx (DWORD styleEx);
void WINAPI SetParent (HWND parent);
void WINAPI SetFont (HFONT font);
void WINAPI SetWindowIcon (HMODULE hInst, LPCTSTR lpszIconName);
void WINAPI SetWindowIcon (HICON hIcon, HICON hIconSm);
void WINAPI SetWindowIcon (HICON hIcon);
void WINAPI SetWindowIconSm(HICON hIconSm);
void WINAPI SetText (LPCSTR text);
void WINAPI SetText (LPCWSTR text);
public: // window procedures
static LRESULT CALLBACK WndMapProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
virtual LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
};
//---------------------------------------------------------------------------//
// UWnd ctor
//---------------------------------------------------------------------------//
inline tapetums::UWnd::UWnd() noexcept
{
static ATOM atom { 0 };
if ( atom == 0 ) { atom = Register(uwnd_class_name); }
}
//---------------------------------------------------------------------------//
// UWnd Methods
//---------------------------------------------------------------------------//
inline ATOM WINAPI tapetums::UWnd::Register
(
LPCTSTR lpszClassName
)
{
WNDCLASSEX wcex
{
sizeof(WNDCLASSEX),
CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
WndMapProc,
0, 0,
::GetModuleHandle(nullptr),
nullptr,
::LoadCursor(nullptr, IDC_ARROW),
nullptr,
nullptr,
lpszClassName,
nullptr,
};
return ::RegisterClassEx(&wcex);
}
//---------------------------------------------------------------------------//
inline HWND WINAPI tapetums::UWnd::Create
(
DWORD style,
DWORD styleEx,
HWND hwndParent,
HMENU hMenu
)
{
if ( m_hwnd ) { return m_hwnd; }
const auto hwnd = ::CreateWindowEx
(
styleEx, uwnd_class_name, nullptr, style,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwndParent, hMenu, ::GetModuleHandle(nullptr),
reinterpret_cast<LPVOID>(this)
);
if ( hwnd == nullptr )
{
ShowLastError(uwnd_class_name);
}
else
{
::UpdateWindow(hwnd);
}
return hwnd;
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Destroy()
{
if ( m_hwnd )
{
::DestroyWindow(m_hwnd);
m_hwnd = nullptr;
}
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Close()
{
::SendMessage(m_hwnd, WM_CLOSE, 0, 0);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Bounds
(
INT32 x, INT32 y, INT32 w, INT32 h
)
{
AdjustRect(m_hwnd, &w, &h);
::SetWindowPos
(
m_hwnd, nullptr,
x, y, w, h,
SWP_NOZORDER | SWP_FRAMECHANGED
);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Hide()
{
::ShowWindowAsync(m_hwnd, SW_HIDE);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Move
(
INT32 x, INT32 y
)
{
::SetWindowPos
(
m_hwnd, nullptr,
x, y, 0, 0,
SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED
);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Refresh()
{
::InvalidateRect(m_hwnd, nullptr, FALSE);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Resize
(
INT32 w, INT32 h
)
{
AdjustRect(m_hwnd, &w, &h);
::SetWindowPos
(
m_hwnd, nullptr,
0, 0, w, h,
SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED
);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::Show()
{
::ShowWindowAsync(m_hwnd, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::ToCenter()
{
RECT rc;
INT32 mx, my, mw, mh;
if ( const auto parent = GetParent() )
{
::GetWindowRect(parent, &rc);
}
else
{
POINT pt;
::GetCursorPos(&pt);
GetRectForMonitor(pt, &rc);
}
mx = rc.left;
my = rc.top;
mw = rc.right - rc.left;
mh = rc.bottom - rc.top;
::GetClientRect(m_hwnd, &rc);
const auto w = rc.right - rc.left;
const auto h = rc.bottom - rc.top;
const auto x = (mw - w) / 2 + mx;
const auto y = (mh - h) / 2 + my;
return Move(x, y);
}
//---------------------------------------------------------------------------//
inline bool WINAPI tapetums::UWnd::AddNotifyIcon
(
UINT uID, HICON hIcon
)
{
if ( WM_TASKBARCREATED == 0 )
{
WM_TASKBARCREATED = ::RegisterWindowMessage(TEXT("TaskbarCreated"));
}
if ( WM_NOTIFYICON == 0 )
{
WM_NOTIFYICON = ::RegisterWindowMessage(TEXT("NotifyIcon"));
}
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hwnd;
nid.uID = uID;
nid.uFlags = NIF_MESSAGE | NIF_ICON;
nid.uCallbackMessage = WM_NOTIFYICON;
nid.hIcon = hIcon;
for ( ; ; )
{
if ( ::Shell_NotifyIcon(NIM_ADD, &nid) )
{
return true;
}
// 以下、スタートアッププログラムとして起動した際
// タイミングによってアイコンの登録が失敗する問題への対策
// Microsoft: KB418138
if ( ::GetLastError() != ERROR_TIMEOUT )
{
::MessageBox
(
nullptr, nullptr, TEXT("Failed to add notify icon"),
MB_ICONEXCLAMATION | MB_OK
);
return false;
}
if ( ::Shell_NotifyIcon(NIM_MODIFY, &nid) )
{
return true;
}
else
{
::Sleep(1000);
}
}
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::DeleteNotifyIcon
(
UINT uID
)
{
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hwnd;
nid.uID = uID;
nid.uFlags = 0;
::Shell_NotifyIcon(NIM_DELETE, &nid);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetNotifyIconTip
(
UINT uID, LPCTSTR szTip
)
{
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hwnd;
nid.uID = uID;
nid.uFlags = NIF_TIP;
#if (NTDDI_VERSION < NTDDI_WIN2K)
::StringCchCopy(nid.szTip, 64, szTip);
#else
::StringCchCopy(nid.szTip, 128, szTip);
#endif
::Shell_NotifyIcon(NIM_MODIFY, &nid);
}
//---------------------------------------------------------------------------//
inline bool WINAPI tapetums::UWnd::ShowNotifyIconInfo
(
UINT uID, DWORD dwInfoFlags,
LPCTSTR szInfoTitle, LPCTSTR szInfo, UINT uTimeout
)
{
#if NTDDI_VERSION < NTDDI_WIN2K
return false;
#endif
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hwnd;
nid.uID = uID;
nid.uFlags = NIF_INFO;
::StringCchCopy(nid.szInfo, 256, szInfo);
nid.uTimeout = uTimeout;
::StringCchCopy(nid.szInfoTitle, 64, szInfoTitle);
nid.dwInfoFlags = dwInfoFlags;
::Shell_NotifyIcon(NIM_MODIFY, &nid);
return true;
}
//---------------------------------------------------------------------------//
inline LRESULT WINAPI tapetums::UWnd::Send
(
UINT msg, WPARAM wp, LPARAM lp
)
const
{
return ::SendMessage(m_hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
inline BOOL WINAPI tapetums::UWnd::Post
(
UINT msg, WPARAM wp, LPARAM lp
)
const
{
return ::PostMessage(m_hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
// UWnd Properties
//---------------------------------------------------------------------------//
inline DWORD WINAPI tapetums::UWnd::GetStyle() const
{
return (DWORD)::GetWindowLongPtr(m_hwnd, GWL_STYLE);
}
//---------------------------------------------------------------------------//
inline DWORD WINAPI tapetums::UWnd::GetStyleEx() const
{
return (DWORD)::GetWindowLongPtr(m_hwnd, GWL_EXSTYLE);
}
//---------------------------------------------------------------------------//
inline HWND WINAPI tapetums::UWnd::GetParent() const
{
return (HWND)::GetWindowLongPtr(m_hwnd, GWLP_HWNDPARENT);
}
//---------------------------------------------------------------------------//
inline HFONT WINAPI tapetums::UWnd::GetFont() const
{
return (HFONT)::SendMessage(m_hwnd, WM_GETFONT, 0, 0);
}
//---------------------------------------------------------------------------//
inline HICON WINAPI tapetums::UWnd::GetWindowIcon() const
{
return (HICON)::GetClassLongPtr(m_hwnd, GCLP_HICON);
}
//---------------------------------------------------------------------------//
inline HICON WINAPI tapetums::UWnd::GetWindowIconSm() const
{
return (HICON)::GetClassLongPtr(m_hwnd, GCLP_HICONSM);
}
//---------------------------------------------------------------------------//
inline SIZE_T WINAPI tapetums::UWnd::GetText
(
LPSTR buf, SIZE_T buf_size
)
const
{
return (SIZE_T)::SendMessageA
(
m_hwnd, WM_GETTEXT, WPARAM(buf_size), LPARAM(buf)
);
}
//---------------------------------------------------------------------------//
inline SIZE_T WINAPI tapetums::UWnd::GetText
(
LPWSTR buf, SIZE_T buf_size
)
const
{
return (SIZE_T)::SendMessageW
(
m_hwnd, WM_GETTEXT, WPARAM(buf_size), LPARAM(buf)
);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetStyle
(
DWORD style
)
{
::SetWindowLongPtr(m_hwnd, GWL_STYLE, LONG_PTR(style));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetStyleEx
(
DWORD styleEx
)
{
::SetWindowLongPtr(m_hwnd, GWL_EXSTYLE, LONG_PTR(styleEx));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetParent
(
HWND parent
)
{
::SetWindowLongPtr(m_hwnd, GWLP_HWNDPARENT, LONG_PTR(parent));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetFont
(
HFONT font
)
{
::SendMessage(m_hwnd, WM_SETFONT, WPARAM(font), LPARAM(FALSE));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetWindowIcon
(
HMODULE hInst, LPCTSTR lpszIconName
)
{
const auto hIcon = (HICON)::LoadImage
(
hInst, lpszIconName,
IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED
);
const auto hIconSm = (HICON)::LoadImage
(
hInst, lpszIconName,
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR | LR_SHARED
);
return SetWindowIcon(hIcon, hIconSm);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetWindowIcon
(
HICON hIcon, HICON hIconSm
)
{
::SetClassLongPtr(m_hwnd, GCLP_HICON, LONG_PTR(hIcon));
::SetClassLongPtr(m_hwnd, GCLP_HICONSM, LONG_PTR(hIconSm));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetWindowIcon
(
HICON hIcon
)
{
::SetClassLongPtr(m_hwnd, GCLP_HICON, LONG_PTR(hIcon));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetWindowIconSm
(
HICON hIconSm
)
{
::SetClassLongPtr(m_hwnd, GCLP_HICONSM, LONG_PTR(hIconSm));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetText
(
LPCSTR text
)
{
::SendMessageA(m_hwnd, WM_SETTEXT, 0, LPARAM(text));
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetText
(
LPCWSTR text
)
{
::SendMessageW(m_hwnd, WM_SETTEXT, 0, LPARAM(text));
}
//---------------------------------------------------------------------------//
// UWnd ウィンドウプロシージャ
//---------------------------------------------------------------------------//
inline LRESULT CALLBACK tapetums::UWnd::WndMapProc
(
HWND hwnd, UINT msg, WPARAM wp, LPARAM lp
)
{
UWnd* wnd;
if ( msg == WM_NCCREATE )
{
wnd = (UWnd*)((CREATESTRUCT*)lp)->lpCreateParams;
::SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG_PTR(wnd));
}
else
{
wnd = (UWnd*)::GetWindowLongPtr(hwnd, GWLP_USERDATA);
}
if ( wnd == nullptr )
{
return ::DefWindowProc(hwnd, msg, wp, lp);
}
if ( msg == WM_CREATE )
{
wnd->m_hwnd = hwnd;
}
return wnd->WndProc(hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
inline LRESULT CALLBACK tapetums::UWnd::WndProc
(
HWND hwnd, UINT msg, WPARAM wp, LPARAM lp
)
{
return ::DefWindowProc(hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
// Utility Functions
//---------------------------------------------------------------------------//
inline void tapetums::ShowLastError
(
LPCTSTR window_title
)
{
LPTSTR lpMsgBuf{ nullptr };
::FormatMessage
(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
LPTSTR(&lpMsgBuf),
0,
nullptr
);
::MessageBox(nullptr, lpMsgBuf, window_title, MB_OK);
::LocalFree(lpMsgBuf);
}
//---------------------------------------------------------------------------//
inline void tapetums::AdjustRect
(
HWND hwnd, INT32* w, INT32* h
)
{
RECT rc{ 0, 0, *w, *h };
const auto style = (DWORD)::GetWindowLongPtr(hwnd, GWL_STYLE);
const auto styleEx = (DWORD)::GetWindowLongPtr(hwnd, GWL_EXSTYLE);
const auto isChild = style & WS_CHILD;
const BOOL hasMenu = (!isChild && ::GetMenu(hwnd)) ? TRUE : FALSE;
::AdjustWindowRectEx(&rc, style, hasMenu, styleEx);
*w = rc.right - rc.left;
*h = rc.bottom - rc.top;
}
//---------------------------------------------------------------------------//
inline void tapetums::GetRectForMonitor
(
HWND hwnd, RECT* rect
)
{
const auto hMonitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFOEX miex{ };
miex.cbSize = (DWORD)sizeof(MONITORINFOEX);
::GetMonitorInfo(hMonitor, &miex);
*rect = miex.rcMonitor;
}
//---------------------------------------------------------------------------//
inline void tapetums::GetRectForMonitor
(
const POINT& pt, RECT* rect
)
{
const auto hMonitor = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
MONITORINFOEX miex{ };
miex.cbSize = (DWORD)sizeof(MONITORINFOEX);
::GetMonitorInfo(hMonitor, &miex);
*rect = miex.rcMonitor;
}
//---------------------------------------------------------------------------//
#include <ShellScalingApi.h>
inline void tapetums::GetDpiForMonitor
(
HWND hwnd, POINT* dpi
)
{
static const auto Shcore = ::LoadLibraryEx
(
TEXT("Shcore.dll"), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH
);
using F = HRESULT (__stdcall *)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*);
static const auto GetDpiForMonitor = (F)::GetProcAddress
(
Shcore, "GetDpiForMonitor"
);
if ( GetDpiForMonitor )
{
const auto hMonitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
GetDpiForMonitor
(
hMonitor, MONITOR_DPI_TYPE::MDT_EFFECTIVE_DPI,
(UINT*)&dpi->x, (UINT*)&dpi->y
);
}
else
{
const auto hDC = ::GetDC(nullptr);
if ( hDC )
{
dpi->x = ::GetDeviceCaps(hDC, LOGPIXELSX);
dpi->y = ::GetDeviceCaps(hDC, LOGPIXELSY);
::ReleaseDC(nullptr, hDC);
}
}
}
//---------------------------------------------------------------------------//
inline void tapetums::GetDpiForMonitor
(
const POINT& pt, POINT* dpi
)
{
static const auto Shcore = ::LoadLibraryEx
(
TEXT("Shcore.dll"), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH
);
using F = HRESULT (__stdcall *)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*);
static const auto GetDpiForMonitor = (F)::GetProcAddress
(
Shcore, "GetDpiForMonitor"
);
if ( GetDpiForMonitor )
{
const auto hMonitor = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
GetDpiForMonitor
(
hMonitor, MONITOR_DPI_TYPE::MDT_EFFECTIVE_DPI,
(UINT*)&dpi->x, (UINT*)&dpi->y
);
}
else
{
const auto hDC = ::GetDC(nullptr);
if ( hDC )
{
dpi->x = ::GetDeviceCaps(hDC, LOGPIXELSX);
dpi->y = ::GetDeviceCaps(hDC, LOGPIXELSY);
::ReleaseDC(nullptr, hDC);
}
}
}
//---------------------------------------------------------------------------//
// UWnd.hpp