-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslimbar.hpp
More file actions
931 lines (777 loc) · 25.8 KB
/
Copy pathslimbar.hpp
File metadata and controls
931 lines (777 loc) · 25.8 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
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
#pragma once
namespace my
{
//
// このクラスはスリムバーです。
//
struct slimbar_t : my::WindowT<slimbar_t>
{
//
// ウィンドウメッセージです。
//
inline static constexpr struct message_t {
inline static const auto c_apply_config = ::RegisterWindowMessageW(L"uah::slimbar::apply_config");
inline static const auto c_update_layout = ::RegisterWindowMessageW(L"uah::slimbar::update_layout");
} c_message;
//
// このクラスはスリムバーの設定です。
//
inline static struct config_t
{
//
// TRUEの場合はメニューバーをタイトルバーと一体化します。
//
BOOL flag_use = TRUE;
//
// TRUEの場合はタイトルをスリムバー全体の中央に描画します。
//
BOOL flag_whole_title = FALSE;
//
// スリムバー時のタイトルの書式です。
//
std::wstring title_format = L"%title%";
//
// ボタンの幅(%)です。
//
int32_t button_width = 200;
} config;
//
// このクラスはスリムバーのボタンです。
//
struct button_t { int ht; RECT rc, icon_rc; };
//
// ホバーまたはクリックされているボタンです。
//
int current_ht = {};
//
// TRUEの場合はホバー中です。
//
BOOL is_hovering = {};
//
// ボタンの配列です。
//
button_t buttons[4] = {
{ HTCLOSE, },
{ HTMAXBUTTON, },
{ HTMINBUTTON, },
{ HTSYSMENU, },
};
//
// タイトルの描画位置です。
//
RECT title_rc = {};
//
// TRUEの場合はアクティブなスリムバーを描画します。
//
BOOL is_active = {};
//
// スリムバーの設定をウィンドウに適用します。
//
BOOL apply_config()
{
// スリムバーを使用する場合は
if (config.flag_use)
{
// ウィンドウスタイルからボーダーを削除します。
my::modify_style(*this, WS_BORDER, 0);
}
// スリムバーを使用しない場合は
else
{
// ウィンドウスタイルにキャプションを追加します。
my::modify_style(*this, 0, WS_CAPTION);
}
// ウィンドウレイアウトを更新します。
::SetWindowPos(*this, nullptr, 0, 0, 0, 0,
SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
// スリムバー全体を再描画します。
return redraw(*this);
}
//
// スリムバーのレイアウトを更新します。
//
BOOL update_layout()
{
//
// この関数は指定された矩形の中央に位置する指定された幅の矩形を返します。
//
const auto centering = [](const RECT& base_rc, int width)
{
auto rc = base_rc;
rc.left = (base_rc.left + base_rc.right - width) / 2;
rc.right = rc.left + width;
return rc;
};
// ウィンドウ矩形を取得します。
auto window_rc = my::get_window_rect(hwnd);
// スリムバー矩形を取得します。
auto bar_rc = get_bar_rc(hwnd);
::OffsetRect(&bar_rc, -window_rc.left, -window_rc.top);
// タイトル矩形を初期化します。
title_rc = bar_rc;
// メニューを取得します。
auto menu = ::GetMenu(hwnd);
// メニュー項目の数を取得します。
auto c = ::GetMenuItemCount(menu);
// メニュー項目を走査します。
for (decltype(c) i = 0; i < c; i++)
{
// メニュー項目の矩形を取得します。
auto item_rc = RECT {};
::GetMenuItemRect(hwnd, menu, i, &item_rc);
::OffsetRect(&item_rc, -window_rc.left, -window_rc.top);
// メニュー項目のタイプを取得します。
MENUITEMINFO mii = { sizeof(mii) };
mii.fMask = MIIM_FTYPE;
::GetMenuItemInfo(menu, i, TRUE, &mii);
// メニュー項目がメニューバーの右側にある場合は
if (mii.fType & MFT_RIGHTJUSTIFY)
{
// タイトル矩形の右端を更新します。
title_rc.right = item_rc.left;
// ループを終了します。
break;
}
// メニュー項目がメニューバーの左側にある場合は
else
{
// タイトル矩形の左端を更新します。
title_rc.left = item_rc.right;
}
}
// ボタンのサイズを算出します。
auto button_height = my::get_height(title_rc);
auto button_width = ::MulDiv(button_height, config.button_width, 100);
// 一番右のボタンの矩形を算出します。
auto button_rc = title_rc;
button_rc.left = button_rc.right - button_width;
// 一番右のボタンのアイコン矩形を算出します。
auto icon_rc = button_rc;
auto icon_size = my::get_height(icon_rc);
icon_rc.left = (icon_rc.left + icon_rc.right) / 2 - icon_size / 2;
icon_rc.right = icon_rc.left + icon_size;
::InflateRect(&icon_rc, -2, -2);
// ボタンを走査します。
for (auto& button : buttons)
{
// タイトル矩形の右端を更新します。
title_rc.right = button_rc.left;
// ボタン位置をセットします。
button.rc = button_rc;
button.icon_rc = icon_rc;
// 次のボタン位置に移動します。
::OffsetRect(&button_rc, -button_width, 0);
::OffsetRect(&icon_rc, -button_width, 0);
}
// スリムバー全体を再描画します。
return redraw(*this);
}
//
// スリムバー矩形を返します。
//
RECT get_bar_rc(HWND hwnd)
{
MENUBARINFO mbi = { sizeof(mbi) };
::GetMenuBarInfo(hwnd, OBJID_MENU, 0, &mbi);
return mbi.rcBar;
}
//
// ヒットテストコードを返します。
// ptはウィンドウ座標で指定します。
//
int hittest(POINT pt)
{
// スリムバーボタンを走査します。
for (const auto& button : buttons)
{
// スリムバーボタン内の場合は
if (::PtInRect(&button.rc, pt))
{
// スリムバーボタンのヒットテストコードを返します。
return button.ht;
}
}
return HTNOWHERE;
}
//
// スリムバーを再描画します。
//
BOOL redraw(HWND hwnd)
{
return ::DrawMenuBar(hwnd);
}
//
// スリムバーを再描画します。
//
BOOL redraw(HWND hwnd, auto... _hts)
{
// 再描画対象のヒットテストコードです。
const int hts[] = { _hts... };
// 再描画対象のリージョンです。
my::gdi::unique_ptr<HRGN> rgn(
::CreateRectRgn(0, 0, 0, 0));
// 再描画フラグです。
auto redraw = BOOL {};
// ヒットテストコードを走査します。
for (auto ht : hts)
{
// スリムバーボタンを走査します。
for (const auto& button : buttons)
{
// ヒットテストコードが等しい場合は
if (button.ht == ht)
{
// スリムバーボタンリージョンを作成します。
my::gdi::unique_ptr<HRGN> button_rgn(
::CreateRectRgnIndirect(&button.rc));
// スリムバーボタンリージョンを結合します。
::CombineRgn(rgn.get(), rgn.get(), button_rgn.get(), RGN_OR);
// 再描画フラグをセットします。
redraw = TRUE;
break;
}
}
}
// リージョンをスクリーン座標に変換します。
{
auto rc = my::get_window_rect(hwnd);
::OffsetRgn(rgn.get(), rc.left, rc.top);
}
// 再描画が必要な場合は
if (redraw)
{
// NC領域を再描画します。
return ::SendMessage(hwnd, WM_NCPAINT, (WPARAM)rgn.release(), 0), TRUE;
}
return FALSE;
}
//
// WM_NCHITTESTを処理します。
//
LRESULT on_nc_hittest(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// スコープ終了時にスリムバーを再描画するようにします。
my::scope_exit scope_exit([&, prev_ht = current_ht]()
{
// 直前のヒットテストコードと異なる場合は
if (current_ht != prev_ht)
{
// スリムバーを再描画します。
redraw(hwnd, current_ht, prev_ht);
}
});
// ホバー中フラグをリセットします。
current_ht = HTNOWHERE;
is_hovering = FALSE;
// まず、デフォルト処理を実行します。
auto result = __super::on_wnd_proc(hwnd, message, w_param, l_param);
// メニュー内の場合は
if (result == HTMENU)
{
// カーソル位置を取得します。
auto pt = my::lp_to_pt(l_param);
// メニューを取得します。
auto menu = ::GetMenu(hwnd);
// メニュー項目の数を取得します。
auto c = ::GetMenuItemCount(menu);
// メニュー項目を走査します。
for (decltype(c) i = 0; i < c; i++)
{
// メニュー項目の矩形を取得します。
auto rc = RECT {};
::GetMenuItemRect(hwnd, menu, i, &rc);
// メニュー項目内の場合はデフォルト処理を実行します。
if (::PtInRect(&rc, pt)) return result;
}
// ウィンドウ矩形を取得します。
auto rc = my::get_window_rect(hwnd);
// ヒットテストコードを取得できた場合は
if (auto ht = hittest(pt - my::get_near(rc)))
{
// ホバー中フラグをセットします。
current_ht = ht;
is_hovering = TRUE;
#if 1
// タイマーをセットします。
::SetTimer(hwnd, 0, 50,
[](HWND hwnd, UINT message, UINT_PTR timer_id, DWORD time)
{
// ウィンドウ矩形を取得します。
auto rc = my::get_window_rect(hwnd);
// カーソル位置を取得します。
auto pt = my::get_cursor_pos();
// マウスカーソルがウィンドウ外に移動した場合は
if (!::PtInRect(&rc, pt))
{
// タイマーをリセットします。
::KillTimer(hwnd, timer_id);
// WM_NCMOUSELEAVEをポストします。
::PostMessage(hwnd, WM_NCMOUSELEAVE, 0, 0);
}
});
#else
// WM_NCMOUSELEAVEをトラックします。
my::track_mouse_event(hwnd, TME_LEAVE | TME_NONCLIENT);
#endif
return ht; // ヒットテストコードを返します。
}
// それ以外の場合はキャプションののヒットテストコードを返します。
return HTCAPTION;
}
return result;
}
//
// WM_NCLBUTTONDOWNを処理します。
//
LRESULT on_nc_l_button_down(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// ヒットテストコード取得します。
auto ht = (int)w_param;
// ヒットテストコードが無効の場合は何もしません。
if (ht == HTNOWHERE)
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
// ボタンをホバーしていない場合は何もしません。
if (!is_hovering)
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
// ヒットテストコードがシステムメニューの場合は
if (ht == HTSYSMENU)
{
// システムメニューを表示します。
// ※0x313は非公開メッセージです。
return __super::on_wnd_proc(hwnd, 0x313, w_param, l_param);
}
// ヒットテストコードからボタンのインデックスを取得します。
auto i = [&]()
{
// スリムバーボタンを走査します。
for (size_t i = 0; i < std::size(buttons); i++)
{
// スリムバーボタンを取得します。
const auto& button = buttons[i];
// ヒットテストコードが等しい場合はインデックスを返します。
if (button.ht == ht) return i;
}
// ボタンが見つからなかった場合は無効なインデックスを返します。
return std::size(buttons);
}();
// ボタンのインデックスが無効の場合は何もしません。
if (i >= std::size(buttons))
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
// スリムバーボタンを取得します。
const auto& button = buttons[i];
// ドラッグ中のメッセージループを処理します。
[&]()
{
// マウスキャプチャを開始します。
::SetCapture(hwnd);
// メッセージループを開始します。
MSG msg = {};
while (::GetMessage(&msg, nullptr, 0, 0) > 0)
{
// マウスキャプチャが解除されている場合はループを終了します。
if (::GetCapture() != hwnd) break;
switch (msg.message)
{
case WM_MOUSEMOVE:
{
// カーソル位置を取得します。
auto pt = my::lp_to_pt(msg.lParam);
my::map_window_points(hwnd, nullptr, &pt);
pt = pt - my::get_near(my::get_window_rect(hwnd));
// ホバー状態が異なる場合は
if (is_hovering != ::PtInRect(&button.rc, pt))
{
// ホバー状態を更新します。
is_hovering = !is_hovering;
// スリムバーを再描画します。
redraw(hwnd, current_ht);
}
break;
}
case WM_LBUTTONUP:
{
// マウスキャプチャを終了します。
::ReleaseCapture();
// カーソル位置を取得します。
auto pt = my::lp_to_pt(msg.lParam);
my::map_window_points(hwnd, nullptr, &pt);
pt = pt - my::get_near(my::get_window_rect(hwnd));
// ホバー状態の場合はボタンコマンドを実行します。
if (is_hovering = ::PtInRect(&button.rc, pt))
{
// カーソル位置を取得します。
auto l_param = my::pt_to_lp(my::get_cursor_pos());
switch (button.ht)
{
case HTCLOSE: ::PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, l_param); break;
case HTMAXBUTTON:
{
::PostMessage(hwnd, WM_SYSCOMMAND,
::IsZoomed(hwnd) ? SC_RESTORE : SC_MAXIMIZE, l_param);
break;
}
case HTMINBUTTON: ::PostMessage(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, l_param); break;
case HTSYSMENU: ::PostMessage(hwnd, WM_NCLBUTTONDOWN, HTSYSMENU, l_param); break;
}
}
return;
}
}
}
} ();
// スリムバーを再描画します。
redraw(hwnd, ht);
// この時点で左クリックは処理済みなので、
// デフォルト処理が実行されないようにします。
return 0;
}
//
// WM_NCRBUTTONDOWNを処理します。
//
LRESULT on_nc_r_button_down(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// キャプションで右クリックが発生した場合は
if (w_param == HTCAPTION)
{
// システムメニューを表示します。
return __super::on_wnd_proc(hwnd, 0x313, w_param, l_param);
}
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
//
// WM_NCMOUSELEAVEを処理します。
//
LRESULT on_nc_mouse_leave(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// 直前のヒットテストコードを取得しておきます。
auto prev_ht = current_ht;
// ホバー中フラグをリセットします。
current_ht = HTNOWHERE;
is_hovering = FALSE;
// スリムバーを再描画します。
redraw(hwnd, current_ht, prev_ht);
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
//
// WM_STYLECHANGEDを処理します。
// キャプションを外したのでキャプションが存在するかのように偽装します。
// これにより、キャプションなしでもスリムバーがテーマで描画されるようになります。
//
LRESULT on_style_changed(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
#if 1
return 0; // デフォルト処理を省略するだけでも正常に動作するようです。
#else
if (w_param == GWL_STYLE)
{
auto ss = (STYLESTRUCT*)l_param;
ss->styleNew |= WS_CAPTION;
}
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
#endif
}
//
// WM_DPICHANGEDを処理します。
//
LRESULT on_dpi_changed(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// 一旦スリム化を解除します。
my::modify_style(hwnd, 0, WS_CAPTION);
// スリムバーに設定を適用します。
// これにより、再びスリム化されます。
apply_config();
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
//
// WM_WINDOWPOSCHANGINGを処理します。
// キャプションを外したので最大化位置を手動で調整します。
//
LRESULT on_window_pos_changing(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// 引数を取得します。
auto wp = (WINDOWPOS*)l_param;
// ウィンドウが最大化される場合は
if (!(wp->flags & SWP_NOMOVE) && !(wp->flags & SWP_NOSIZE) && ::IsZoomed(hwnd))
{
#if 1
// マウスカーソルが存在するモニタを取得します。
auto monitor = ::MonitorFromPoint(my::get_cursor_pos(), MONITOR_DEFAULTTONEAREST);
#else
// ウィンドウが存在するモニタを取得します。
auto monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
#endif
// モニタ情報を取得します。
MONITORINFOEX mi = { sizeof(mi) };
::GetMonitorInfo(monitor, &mi);
// ワークエリア矩形を取得します。
auto rc = mi.rcWork;
// 最大化位置になるように矩形を広げます。
::InflateRect(&rc, rc.left - wp->x, rc.top - wp->y);
// 算出した矩形をウィンドウ位置に設定します。
wp->x = rc.left;
wp->y = rc.top;
wp->cx = my::get_width(rc);
wp->cy = my::get_height(rc);
}
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
//
// WM_SIZEを処理します。
// スリムバーボタンの矩形を更新します。
//
LRESULT on_size(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// スリムバーのレイアウトを更新します。
update_layout();
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
//
// WM_SETTEXTを処理します。
// ウィンドウテキストが変更されるのでスリムバーを再描画します。
//
LRESULT on_set_text(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// スコープ終了時にスリムバーを再描画するようにします。
my::scope_exit scope_exit([&]()
{
// NC領域を再描画します。
::SendMessage(hwnd, WM_NCPAINT, 0, 0);
});
// デフォルト処理を実行してウィンドウテキストを更新します。
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
//
// WM_UAHDRAWMENUを処理します。
// メニューバーを描画した後にスリムバーを描画します。
//
LRESULT on_uah_draw_menu(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param)
{
// デフォルト処理の後に実行します。
my::scope_exit scope_exit([&]()
{
// 引数を取得します。
auto uah_menu = (UAHMENU*)l_param;
// テーマを取得します。
my::theme::unique_ptr<> theme(::OpenThemeData(hwnd, VSCLASS_MENU));
auto part_id = MENU_BARBACKGROUND;
auto state_id = is_active ? MB_ACTIVE : MB_INACTIVE;
// ウィンドウ矩形を取得します。
auto window_rc = my::get_window_rect(hwnd);
// スリムバー矩形を取得します。
auto bar_rc = get_bar_rc(hwnd);
::OffsetRect(&bar_rc, -window_rc.left, -window_rc.top);
// スリムバーを描画します。
return on_draw(hwnd, theme.get(), uah_menu->hdc, part_id, state_id, &bar_rc);
});
// デフォルト処理を実行します。
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
//
// スリムバーを描画します。
//
BOOL on_draw(HWND hwnd, HTHEME theme, HDC dc, int part_id, int state_id, LPCRECT rc)
{
// スリムバー矩形を取得します。
auto bar_rc = *rc;
// スリムバーの中央にタイトルを描画します。
{
// タイトル描画位置の矩形を取得します。
auto text_rc = bar_rc;
// タイトルをスリムバー全体の中央に描画する場合は
if (config.flag_whole_title)
{
// 縮小量を算出します。
auto deflate = std::max(title_rc.left - bar_rc.left, bar_rc.right - title_rc.right);
// メニュー項目と干渉しないように描画範囲を縮小します。
::InflateRect(&text_rc, -deflate, 0);
}
// それ以外の場合は
else
{
// タイトル矩形のスペースに描画します。
text_rc = title_rc;
}
// ウィンドウテキストを取得します。
auto text = my::get_window_text(hwnd);
// タイトルの書式が指定されている場合は
if (config.title_format.length())
{
// タイトルを書式化します。
text = my::replace(config.title_format, L"%title%", text);
}
// タイトル描画用フォントをセットします。
NONCLIENTMETRICS ncm = { sizeof(ncm) };
::SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
my::gdi::unique_ptr<HFONT> font(::CreateFontIndirect(&ncm.lfMenuFont));
my::gdi::selector font_selector(dc, font.get());
// タイトルを描画します。
::DrawThemeText(theme, dc, part_id, state_id,
text.c_str(), (int)text.length(),
DT_CENTER | DT_VCENTER | DT_SINGLELINE |
DT_END_ELLIPSIS | DT_NOPREFIX | DT_NOCLIP, 0, &text_rc);
}
// スリムバーボタンを走査します。
for (const auto& button : buttons)
{
// ホット状態を取得します。
auto is_hot = is_hovering && current_ht == button.ht;
// ホット状態の場合は
if (is_hot)
{
// 背景を描画します。
::DrawThemeBackground(theme, dc, MENU_BARITEM, MBI_HOT, &button.rc, nullptr);
}
// アイコン矩形を取得します。
auto icon_rc = button.icon_rc;
// アイコンのパートIDです。
auto part_id = 0;
switch (button.ht)
{
case HTCLOSE: part_id = MENU_SYSTEMCLOSE; break;
case HTMAXBUTTON: part_id = ::IsZoomed(hwnd) ? MENU_SYSTEMRESTORE : MENU_SYSTEMMAXIMIZE; break;
case HTMINBUTTON: part_id = MENU_SYSTEMMINIMIZE; break;
case HTSYSMENU:
{
// システムメニューアイコンを描画します。
auto icon = (HICON)::GetClassLongPtr(hwnd, GCLP_HICONSM);
auto icon_size = my::get_height(icon_rc);
::DrawIconEx(dc, icon_rc.left, icon_rc.top,
icon, icon_size, icon_size, 0, nullptr, DI_NORMAL);
break;
}
}
// アイコンのパートIDが有効の場合は
if (part_id)
{
// テーマを使用してアイコンを描画します。
::DrawThemeBackground(theme, dc, part_id, state_id, &icon_rc, nullptr);
}
}
return TRUE;
}
//
// ウィンドウプロシージャです。
//
virtual LRESULT on_wnd_proc(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param) override
{
MY_TRACE_FUNC("{/hex}, {/}, {/hex}, {/hex}", hwnd, my::message_to_string(message), w_param, l_param);
//
// このクラスはデバッグ用のテキストを出力します。
//
struct ScopeText
{
#ifdef _DEBUG
LPCWSTR text;
ScopeText(LPCWSTR text) : text(text)
{
MY_TRACE("*** {/} を開始します ***\n", text);
}
~ScopeText()
{
MY_TRACE("*** {/} を終了します ***\n", text);
}
#else
ScopeText(LPCWSTR text) {}
~ScopeText() {}
#endif
};
switch (message)
{
case WM_NCACTIVATE:
{
ScopeText scope_text(L"WM_NCACTIVATE");
// アクティブ状態を取得しておきます。
is_active = !!w_param;
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
}
if (message == c_message.c_apply_config)
{
ScopeText scope_text(L"c_message.c_apply_config");
return apply_config();
}
// スリムバーを使用しない場合は以下のメッセージは処理しません。
if (!config.flag_use)
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
switch (message)
{
case WM_ACTIVATE:
{
ScopeText scope_text(L"WM_ACTIVATE");
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
case WM_NCPAINT:
{
ScopeText scope_text(L"WM_NCPAINT");
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
case WM_NCCALCSIZE:
{
ScopeText scope_text(L"WM_NCCALCSIZE");
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
case WM_NCHITTEST:
{
ScopeText scope_text(L"WM_NCHITTEST");
return on_nc_hittest(hwnd, message, w_param, l_param);
}
case WM_NCLBUTTONDOWN:
{
ScopeText scope_text(L"WM_NCLBUTTONDOWN");
return on_nc_l_button_down(hwnd, message, w_param, l_param);
}
case WM_NCRBUTTONDOWN:
{
ScopeText scope_text(L"WM_NCRBUTTONDOWN");
return on_nc_r_button_down(hwnd, message, w_param, l_param);
}
case WM_NCMOUSEMOVE:
{
ScopeText scope_text(L"WM_NCMOUSEMOVE");
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
case WM_NCMOUSELEAVE:
{
ScopeText scope_text(L"WM_NCMOUSELEAVE");
return on_nc_mouse_leave(hwnd, message, w_param, l_param);
}
case WM_STYLECHANGED:
{
ScopeText scope_text(L"WM_STYLECHANGED");
return on_style_changed(hwnd, message, w_param, l_param);
}
case WM_DPICHANGED:
{
ScopeText scope_text(L"WM_DPICHANGED");
return on_dpi_changed(hwnd, message, w_param, l_param);
}
case WM_WINDOWPOSCHANGING:
{
ScopeText scope_text(L"WM_WINDOWPOSCHANGING");
return on_window_pos_changing(hwnd, message, w_param, l_param);
}
case WM_SIZE:
{
ScopeText scope_text(L"WM_SIZE");
return on_size(hwnd, message, w_param, l_param);
}
case WM_SETTEXT:
{
ScopeText scope_text(L"WM_SETTEXT");
return on_set_text(hwnd, message, w_param, l_param);
}
case WM_UAHDRAWMENU:
{
ScopeText scope_text(L"WM_UAHDRAWMENU");
return on_uah_draw_menu(hwnd, message, w_param, l_param);
}
}
if (message == c_message.c_update_layout)
{
ScopeText scope_text(L"c_message.c_update_layout");
return update_layout();
}
return __super::on_wnd_proc(hwnd, message, w_param, l_param);
}
};
}