forked from tapetums/include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UWnd.hpp
972 lines (809 loc) · 25.8 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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
#pragma once
//---------------------------------------------------------------------------//
//
// UWnd.hpp
// ウィンドウをカプセル化するクラス
// Copyright (C) 2005-2016 tapetums
//
//---------------------------------------------------------------------------//
#include <utility>
#include <windows.h>
#include <windowsx.h>
#include <strsafe.h>
#pragma comment(lib, "shell32.lib") // Shell_NotifyIcon
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
//---------------------------------------------------------------------------//
// グローバル変数
//---------------------------------------------------------------------------//
extern UINT WM_NOTIFYICON; // 使用の際は実体定義が必要 (WinMain.cpp 内を推奨)
extern UINT WM_TASKBARCREATED; // 使用の際は実体定義が必要 (WinMain.cpp 内を推奨)
//---------------------------------------------------------------------------//
// 前方宣言
//---------------------------------------------------------------------------//
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);
}
//---------------------------------------------------------------------------//
// クラス
//---------------------------------------------------------------------------//
// ウィンドウの基底クラス
class tapetums::UWnd
{
private: // static members
static constexpr LPCTSTR class_name { TEXT("UWnd") };
protected: // members
INT32 m_x { CW_USEDEFAULT };
INT32 m_y { CW_USEDEFAULT };
INT32 m_w { CW_USEDEFAULT };
INT32 m_h { CW_USEDEFAULT };
LPCTSTR m_class_name { class_name };
HWND m_hwnd { nullptr };
bool m_is_fullscreen { false };
DWORD m_style_old { 0 };
RECT m_rc_old { };
public: // accessors
INT32 left() const noexcept { return m_x; }
INT32 top() const noexcept { return m_y; }
INT32 width() const noexcept { return m_w; }
INT32 height() const noexcept { return m_h; }
POINT position() const noexcept { return { m_x, m_y }; }
SIZE size() const noexcept { return { m_w, m_h }; }
RECT rect() const noexcept { return { m_w, m_h, m_x + m_w, m_y + m_h }; }
HWND handle() const noexcept { return m_hwnd; }
bool is_fullscreen() const noexcept { return m_is_fullscreen; }
public: // ctor / dtor
UWnd();
virtual ~UWnd() { Destroy(); }
UWnd(const UWnd&) = delete;
UWnd& operator= (const UWnd&) = delete;
UWnd(UWnd&& rhs) noexcept { swap(std::move(rhs)); }
UWnd& operator =(UWnd&& rhs) noexcept { swap(std::move(rhs)); return *this; }
public: // mover
void swap(UWnd && rhs) noexcept;
public: // operator
operator HWND() { return m_hwnd; }
public: // methods
ATOM WINAPI Register (LPCTSTR lpszClassName);
HWND WINAPI Create (LPCTSTR lpszWindowName, DWORD style, DWORD styleEx, HWND hwndParent, HMENU hMenu);
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 ();
void WINAPI ToggleFullScreen();
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);
LRESULT WINAPI Post(UINT msg, WPARAM wp, LPARAM lp);
public: // properties
DWORD WINAPI GetStyle () const noexcept;
DWORD WINAPI GetStyleEx () const noexcept;
HWND WINAPI GetParent () const noexcept;
HFONT WINAPI GetFont () const noexcept;
HICON WINAPI GetWindowIcon () const noexcept;
HICON WINAPI GetWindowIconSm() const noexcept;
SIZE_T WINAPI GetText(TCHAR* buf, SIZE_T buf_size) const noexcept;
void WINAPI SetStyle (DWORD style) noexcept;
void WINAPI SetStyleEx (DWORD styleEx) noexcept;
void WINAPI SetParent (HWND parent) noexcept;
void WINAPI SetFont (HFONT font) noexcept;
void WINAPI SetWindowIcon (HMODULE hInst, LPCTSTR lpszIconName) noexcept;
void WINAPI SetWindowIcon (HICON hIcon, HICON hIconSm) noexcept;
void WINAPI SetWindowIcon (HICON hIcon) noexcept;
void WINAPI SetWindowIconSm(HICON hIconSm) noexcept;
void WINAPI SetText (LPCTSTR txt) noexcept;
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);
};
//---------------------------------------------------------------------------//
// ユーティリティ関数
//---------------------------------------------------------------------------//
// エラーをメッセージボックスで表示する
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 コンストラクタ
//---------------------------------------------------------------------------//
inline tapetums::UWnd::UWnd()
{
// 自動的に "UWnd" ウィンドウクラスを登録
static ATOM atom { 0 };
if ( atom == 0 ) { atom = Register(class_name); }
}
//---------------------------------------------------------------------------//
// UWnd ムーバー
//---------------------------------------------------------------------------//
inline void tapetums::UWnd::swap(UWnd&& rhs) noexcept
{
if ( this == &rhs ) return;
std::swap(m_x, rhs.m_x);
std::swap(m_y, rhs.m_y);
std::swap(m_w, rhs.m_w);
std::swap(m_h, rhs.m_h);
std::swap(m_class_name, rhs.m_class_name);
std::swap(m_hwnd, rhs.m_hwnd);
std::swap(m_is_fullscreen, rhs.m_is_fullscreen);
std::swap(m_style_old, rhs.m_style_old);
std::swap(m_rc_old, rhs.m_rc_old);
}
//---------------------------------------------------------------------------//
// UWnd メソッド
//---------------------------------------------------------------------------//
// ウィンドウクラスを登録する
inline ATOM WINAPI tapetums::UWnd::Register
(
LPCTSTR lpszClassName
)
{
m_class_name = 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
(
LPCTSTR lpszWindowName,
DWORD style,
DWORD styleEx,
HWND hwndParent,
HMENU hMenu
)
{
if ( m_hwnd ) { return m_hwnd; } // 二重生成防止!
const auto hwnd = ::CreateWindowEx
(
styleEx, m_class_name, lpszWindowName, style,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwndParent, hMenu, ::GetModuleHandle(nullptr),
reinterpret_cast<LPVOID>(this)
);
if ( nullptr == hwnd )
{
ShowLastError(class_name);
}
else
{
::UpdateWindow(hwnd);
}
return hwnd;
}
//---------------------------------------------------------------------------//
// ウィンドウを破棄する
inline void WINAPI tapetums::UWnd::Destroy()
{
if ( nullptr == m_hwnd ) { return; }
::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);
mx = rc.left;
my = rc.top;
mw = rc.right - rc.left;
mh = rc.bottom - rc.top;
}
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 void WINAPI tapetums::UWnd::ToggleFullScreen()
{
m_is_fullscreen = ! m_is_fullscreen;
if ( m_is_fullscreen )
{
// ウィンドウスタイルを変更
m_style_old = GetStyle();
SetStyle(WS_VISIBLE | WS_POPUP | WS_MINIMIZEBOX);
// ウィンドウサイズを記憶
::GetWindowRect(m_hwnd, &m_rc_old);
// モニタのサイズを取得
POINT pt;
::GetCursorPos(&pt);
RECT rc;
GetRectForMonitor(pt, &rc);
const auto cx = rc.left;
const auto cy = rc.top;
const auto cw = rc.right - rc.left;
const auto ch = rc.bottom - rc.top;
// 全画面にする
::SetWindowPos
(
m_hwnd, nullptr,
cx, cy, cw, ch,
SWP_NOZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW
);
}
else
{
// ウィンドウスタイルを復帰
SetStyle(m_style_old);
// ウィンドウサイズを復帰
const auto cx = m_rc_old.left;
const auto cy = m_rc_old.top;
const auto cw = m_rc_old.right - m_rc_old.left;
const auto ch = m_rc_old.bottom - m_rc_old.top;
// ウィンドウの位置を復帰
::SetWindowPos
(
m_hwnd, nullptr,
cx, cy, cw, ch,
SWP_NOZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW
);
}
}
//---------------------------------------------------------------------------//
// タスクトレイアイコンを設定する
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
)
{
return ::SendMessage(m_hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
// ウィンドウにメッセージを送る
inline LRESULT WINAPI tapetums::UWnd::Post
(
UINT msg, WPARAM wp, LPARAM lp
)
{
return ::PostMessage(m_hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
// UWnd プロパティ
//---------------------------------------------------------------------------//
inline DWORD WINAPI tapetums::UWnd::GetStyle() const noexcept
{
return (DWORD)::GetWindowLongPtr(m_hwnd, GWL_STYLE);
}
//---------------------------------------------------------------------------//
inline DWORD WINAPI tapetums::UWnd::GetStyleEx() const noexcept
{
return (DWORD)::GetWindowLongPtr(m_hwnd, GWL_EXSTYLE);
}
//---------------------------------------------------------------------------//
inline HWND WINAPI tapetums::UWnd::GetParent() const noexcept
{
return (HWND)::GetWindowLongPtr(m_hwnd, GWLP_HWNDPARENT);
}
//---------------------------------------------------------------------------//
inline HFONT WINAPI tapetums::UWnd::GetFont() const noexcept
{
return (HFONT)::SendMessage(m_hwnd, WM_GETFONT, 0, 0);
}
//---------------------------------------------------------------------------//
inline HICON WINAPI tapetums::UWnd::GetWindowIcon() const noexcept
{
return (HICON)::GetClassLongPtr(m_hwnd, GCLP_HICON);
}
//---------------------------------------------------------------------------//
inline HICON WINAPI tapetums::UWnd::GetWindowIconSm() const noexcept
{
return (HICON)::GetClassLongPtr(m_hwnd, GCLP_HICONSM);
}
//---------------------------------------------------------------------------//
inline SIZE_T WINAPI tapetums::UWnd::GetText
(
TCHAR* buf, SIZE_T buf_size
)
const noexcept
{
return (SIZE_T)::SendMessage
(
m_hwnd, WM_GETTEXT, (WPARAM)buf_size, (LPARAM)buf
);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetStyle
(
DWORD style
)
noexcept
{
::SetWindowLongPtr(m_hwnd, GWL_STYLE, (LONG_PTR)style);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetStyleEx
(
DWORD styleEx
)
noexcept
{
::SetWindowLongPtr(m_hwnd, GWL_EXSTYLE, (LONG_PTR)styleEx);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetParent
(
HWND parent
)
noexcept
{
::SetWindowLongPtr(m_hwnd, GWLP_HWNDPARENT, (LONG_PTR)parent);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetFont
(
HFONT font
)
noexcept
{
::SendMessage(m_hwnd, WM_SETFONT, (WPARAM)font, (LPARAM)FALSE);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetWindowIcon
(
HMODULE hInst, LPCTSTR lpszIconName
)
noexcept
{
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
)
noexcept
{
::SetClassLongPtr(m_hwnd, GCLP_HICON, (LONG_PTR)hIcon);
::SetClassLongPtr(m_hwnd, GCLP_HICONSM, (LONG_PTR)hIconSm);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetWindowIcon
(
HICON hIcon
)
noexcept
{
::SetClassLongPtr(m_hwnd, GCLP_HICON, (LONG_PTR)hIcon);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetWindowIconSm
(
HICON hIconSm
)
noexcept
{
::SetClassLongPtr(m_hwnd, GCLP_HICONSM, (LONG_PTR)hIconSm);
}
//---------------------------------------------------------------------------//
inline void WINAPI tapetums::UWnd::SetText
(
LPCTSTR txt
)
noexcept
{
::SendMessage(m_hwnd, WM_SETTEXT, 0, (LPARAM)txt);
}
//---------------------------------------------------------------------------//
// UWnd ウィンドウプロシージャ
//---------------------------------------------------------------------------//
// UWnd 静的ウィンドウプロシージャ
inline LRESULT CALLBACK tapetums::UWnd::WndMapProc
(
HWND hwnd, UINT msg, WPARAM wp, LPARAM lp
)
{
UWnd* wnd;
// UWndオブジェクトのポインタを取得
if ( msg == WM_NCCREATE )
{
wnd = (UWnd*)((CREATESTRUCT*)lp)->lpCreateParams;
::SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)wnd);
}
else
{
wnd = (UWnd*)::GetWindowLongPtr(hwnd, GWLP_USERDATA);
}
// まだマップされていない場合はデフォルトプロシージャに投げる
if ( nullptr == wnd )
{
return ::DefWindowProc(hwnd, msg, wp, lp);
}
// メンバ変数に情報を保存
switch ( msg )
{
case WM_CREATE:
{
wnd->m_hwnd = hwnd; // ウィンドウハンドル
break;
}
case WM_MOVE:
{
wnd->m_x = GET_X_LPARAM(lp); // ウィンドウX座標
wnd->m_y = GET_Y_LPARAM(lp); // ウィンドウY座標
break;
}
case WM_SIZE:
{
wnd->m_w = LOWORD(lp); // ウィンドウ幅
wnd->m_h = HIWORD(lp); // ウィンドウ高
break;
}
default:
{
break;
}
}
// ウィンドウプロシージャの呼び出し
return wnd->WndProc(hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
// UWnd ウィンドウプロシージャ
inline LRESULT CALLBACK tapetums::UWnd::WndProc
(
HWND hwnd, UINT msg, WPARAM wp, LPARAM lp
)
{
return ::DefWindowProc(hwnd, msg, wp, lp);
}
//---------------------------------------------------------------------------//
// UWnd.hpp