-
Notifications
You must be signed in to change notification settings - Fork 3
/
CTabWnd.cpp
1267 lines (1112 loc) · 33.2 KB
/
CTabWnd.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
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "StdAfx.h"
#include "Sazabi.h"
#include "MainFrm.h"
#include "BroFrame.h"
#include "BroView.h"
#include <limits.h>
#include "CWnd.h"
#include "CTabWnd.h"
#ifndef SPI_GETFOREGROUNDLOCKTIMEOUT
#define SPI_GETFOREGROUNDLOCKTIMEOUT 0x2000
#endif
#ifndef SPI_SETFOREGROUNDLOCKTIMEOUT
#define SPI_SETFOREGROUNDLOCKTIMEOUT 0x2001
#endif
#define TAB_MARGIN_TOP 1
#define TAB_MARGIN_LEFT 1
#define TAB_MARGIN_RIGHT 1
//#define TAB_FONT_HEIGHT 12
#define TAB_ITEM_HEIGHT 25
#define TAB_WINDOW_HEIGHT 35
#define MAX_TABITEM_WIDTH 180
#define MIN_TABITEM_WIDTH 26
#define CX_SMICON 16
#define CY_SMICON 16
static const RECT rcBtnBase = {0, 0, 16, 16};
WNDPROC gm_pOldWndProc = NULL;
inline LRESULT CALLBACK DefTabWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (gm_pOldWndProc)
return ::CallWindowProc(gm_pOldWndProc, hwnd, uMsg, wParam, lParam);
else
return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK TabWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CTabWnd* pcTabWnd = NULL;
pcTabWnd = (CTabWnd*)::GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (pcTabWnd)
{
if (0L == pcTabWnd->TabWndDispatchEvent(hwnd, uMsg, wParam, lParam))
return 0L;
}
return DefTabWndProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CTabWnd::TabWndDispatchEvent(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_LBUTTONDOWN:
return OnTabLButtonDown(wParam, lParam);
case WM_LBUTTONUP:
return OnTabLButtonUp(wParam, lParam);
case WM_MOUSEMOVE:
return OnTabMouseMove(wParam, lParam);
case WM_TIMER:
return OnTabTimer(wParam, lParam);
case WM_CAPTURECHANGED:
return OnTabCaptureChanged(wParam, lParam);
case WM_RBUTTONDOWN:
return OnTabRButtonDown(wParam, lParam);
case WM_RBUTTONUP:
return OnTabRButtonUp(wParam, lParam);
case WM_MBUTTONDOWN:
return OnTabMButtonDown(wParam, lParam);
case WM_MBUTTONUP:
return OnTabMButtonUp(wParam, lParam);
case WM_NOTIFY:
return OnTabNotify(wParam, lParam);
case WM_HSCROLL:
{
::InvalidateRect(GetHwnd(), NULL, TRUE);
break;
}
}
return 1L;
}
LRESULT CTabWnd::OnTabLButtonDown(WPARAM wParam, LPARAM lParam)
{
// ボタンが押された位置を確認する
TCHITTESTINFO hitinfo = {0};
hitinfo.pt.x = LOWORD((DWORD)lParam);
hitinfo.pt.y = HIWORD((DWORD)lParam);
int nSrcTab = TabCtrl_HitTest(m_hwndTab, (LPARAM)&hitinfo);
if (0 > nSrcTab)
{
return 1L;
}
int nCurSel = TabCtrl_GetCurSel(m_hwndTab);
if (nCurSel != nSrcTab)
{
if (theApp.IsWnd(theApp.m_wndpClose))
{
POINT pt = {0};
::GetCursorPos(&pt);
m_pwndFrame->ScreenToClient(&pt);
if (theApp.m_wndpClose->m_Rc.PtInRect(pt))
{
HWND hCloseWnd = NULL;
TCITEM tcitem = {0};
tcitem.mask = TCIF_PARAM;
tcitem.lParam = 0;
TabCtrl_GetItem(m_hwndTab, nSrcTab, &tcitem);
hCloseWnd = (HWND)tcitem.lParam;
if (IsWindow(hCloseWnd))
{
::PostMessage(hCloseWnd, WM_COMMAND, ID_W_CLOSE, 0);
}
return 0L;
}
}
}
else
{
if (theApp.IsWnd(theApp.m_wndpClose))
{
POINT pt = {0};
::GetCursorPos(&pt);
m_pwndFrame->ScreenToClient(&pt);
if (theApp.m_wndpClose->m_Rc.PtInRect(pt))
{
HWND hCloseWnd = NULL;
TCITEM tcitem = {0};
tcitem.mask = TCIF_PARAM;
tcitem.lParam = 0;
TabCtrl_GetItem(m_hwndTab, nSrcTab, &tcitem);
hCloseWnd = (HWND)tcitem.lParam;
if (IsWindow(hCloseWnd))
{
::PostMessage(hCloseWnd, WM_COMMAND, ID_W_CLOSE, 0);
}
return 0L;
}
}
}
// マウスドラッグ開始処理
m_eDragState = DragState::CHECK; // ドラッグのチェックを開始
// ドラッグ元タブを記憶する
m_nSrcTab = nSrcTab;
::GetCursorPos(&m_ptSrcCursor);
::SetCapture(m_hwndTab);
return 0L;
}
LRESULT CTabWnd::OnTabLButtonUp(WPARAM wParam, LPARAM lParam)
{
TCHITTESTINFO hitinfo = {0};
hitinfo.pt.x = LOWORD((DWORD)lParam);
hitinfo.pt.y = HIWORD((DWORD)lParam);
int nDstTab = TabCtrl_HitTest(m_hwndTab, (LPARAM)&hitinfo);
int nSelfTab = FindTabIndexByHWND(m_pwndFrame->m_hWnd);
// マウスドロップ処理
switch (m_eDragState)
{
case DragState::CHECK:
{
int nCurSel = TabCtrl_GetCurSel(m_hwndTab);
if (nCurSel != nDstTab)
{
BOOL bActive = TRUE;
if (theApp.IsWnd(theApp.m_wndpClose))
{
POINT pt = {0};
::GetCursorPos(&pt);
m_pwndFrame->ScreenToClient(&pt);
if (theApp.m_wndpClose->m_Rc.PtInRect(pt))
{
bActive = FALSE;
BreakDrag();
}
}
if (bActive)
{
//指定のウインドウをアクティブに
TCITEM tcitem = {0};
tcitem.mask = TCIF_PARAM;
tcitem.lParam = 0;
TabCtrl_GetItem(m_hwndTab, nDstTab, &tcitem);
if (tcitem.lParam)
{
ShowTabWindow((HWND)tcitem.lParam);
SendMessage((HWND)tcitem.lParam, WM_SETREDRAW, true, 0);
}
BreakDrag();
return 1;
}
}
break;
}
case DragState::DRAG:
m_pwndMainFrame->TabWindowMsgBSend(TWNT_REFRESH_COMMAND, NULL);
if (m_nTabBorderArray)
{
delete[] m_nTabBorderArray;
m_nTabBorderArray = NULL;
}
break;
default:
break;
}
BreakDrag();
return 0L;
}
LRESULT CTabWnd::OnTabMouseMove(WPARAM wParam, LPARAM lParam)
{
TCHITTESTINFO hitinfo = {0};
int i = 0;
int nTabCount = 0;
hitinfo.pt.x = LOWORD((DWORD)lParam);
hitinfo.pt.y = HIWORD((DWORD)lParam);
int nDstTab = TabCtrl_HitTest(m_hwndTab, (LPARAM)&hitinfo);
if (::GetCapture() != m_hwndTab)
{
int nTabHoverPrev = m_nTabHover;
int nTabHoverCur = nDstTab;
RECT rcPrev = {0};
RECT rcCur = {0};
TabCtrl_GetItemRect(m_hwndTab, nTabHoverPrev, &rcPrev);
TabCtrl_GetItemRect(m_hwndTab, nTabHoverCur, &rcCur);
m_nTabHover = nTabHoverCur;
if (nTabHoverCur >= 0)
{
if (nTabHoverPrev < 0)
{
::SetTimer(m_hwndTab, 1, 200, NULL);
}
}
else
{
::KillTimer(m_hwndTab, 1);
}
}
// マウスドラッグ中の処理
switch (m_eDragState)
{
case DragState::CHECK:
// 元のタブから離れたらドラッグ開始
if (m_nSrcTab == nDstTab)
break;
m_eDragState = DragState::DRAG;
m_hDefaultCursor = ::GetCursor();
// 現在のタブ境界位置を記憶する
nTabCount = TabCtrl_GetItemCount(m_hwndTab);
if (m_nTabBorderArray)
{
delete[] m_nTabBorderArray;
}
m_nTabBorderArray = new LONG[nTabCount];
memset(m_nTabBorderArray, 0x00, sizeof(LONG) * nTabCount);
for (i = 0; i < nTabCount - 1; i++)
{
RECT rc = {0};
TabCtrl_GetItemRect(m_hwndTab, i, &rc);
m_nTabBorderArray[i] = rc.right;
}
m_nTabBorderArray[i] = 0; // 最後の要素は番兵
[[fallthrough]]; // ここに来たらドラッグ開始なので break しないでそのまま DRAG_DRAG 処理に入る
case DragState::DRAG:
// ドラッグ中のマウスカーソルを表示する
HINSTANCE hInstance;
LPCTSTR lpCursorName;
lpCursorName = IDC_NO; // 禁止カーソル
if (0 <= nDstTab) // タブの上にカーソルがある
{
lpCursorName = NULL; // 開始時カーソル指定
// ドラッグ開始時のタブ位置で移動先タブを再計算
for (nDstTab = 0; m_nTabBorderArray[nDstTab] != 0; nDstTab++)
{
if (hitinfo.pt.x < m_nTabBorderArray[nDstTab])
{
break;
}
}
// ドラッグ中に即時移動
if (m_nSrcTab != nDstTab)
{
RECT rc = {0};
TabCtrl_GetItemRect(m_hwndTab, nDstTab, &rc);
if (rc.left > 0)
{
ReorderTab(m_nSrcTab, nDstTab);
Refresh(FALSE);
m_nSrcTab = nDstTab;
::InvalidateRect(GetHwnd(), NULL, TRUE);
}
}
}
if (lpCursorName)
{
hInstance = (lpCursorName == IDC_NO) ? NULL : ::GetModuleHandle(NULL);
::SetCursor(::LoadCursor(hInstance, lpCursorName));
}
else
{
::SetCursor(m_hDefaultCursor);
}
break;
default:
return 1L;
}
return 0L;
}
LRESULT CTabWnd::OnTabTimer(WPARAM wParam, LPARAM lParam)
{
if (wParam == 1)
{
// カーソルがタブ外にある場合にも WM_MOUSEMOVE を送る
TCHITTESTINFO hitinfo = {0};
::GetCursorPos(&hitinfo.pt);
::ScreenToClient(m_hwndTab, &hitinfo.pt);
int nDstTab = TabCtrl_HitTest(m_hwndTab, (LPARAM)&hitinfo);
if (nDstTab < 0)
::SendMessage(m_hwndTab, WM_MOUSEMOVE, 0, MAKELONG(hitinfo.pt.x, hitinfo.pt.y));
}
return 0L;
}
LRESULT CTabWnd::OnTabCaptureChanged(WPARAM wParam, LPARAM lParam)
{
if (m_eDragState != DragState::NONE)
m_eDragState = DragState::NONE;
return 0L;
}
LRESULT CTabWnd::OnTabRButtonDown(WPARAM wParam, LPARAM lParam)
{
BreakDrag();
return 0L;
}
LRESULT CTabWnd::OnTabRButtonUp(WPARAM wParam, LPARAM lParam)
{
TCHITTESTINFO hitinfo = {0};
int i = 0;
int nTabCount = 0;
hitinfo.pt.x = LOWORD((DWORD)lParam);
hitinfo.pt.y = HIWORD((DWORD)lParam);
int nDstTab = TabCtrl_HitTest(m_hwndTab, (LPARAM)&hitinfo);
int nCurSel = TabCtrl_GetCurSel(m_hwndTab);
if (nDstTab == nCurSel)
{
CRect rcOver(0, 0, 0, 0);
::SendMessage(m_hwndTab, TCM_GETITEMRECT, nDstTab, (LPARAM)&rcOver);
::ClientToScreen(m_hwndTab, (LPPOINT)&rcOver.left);
::ClientToScreen(m_hwndTab, (LPPOINT)&rcOver.right);
TCITEM tcitem = {0};
tcitem.mask = TCIF_PARAM;
tcitem.lParam = 0;
TabCtrl_GetItem(m_hwndTab, nDstTab, &tcitem);
CMenu menu;
menu.LoadMenu(IDR_MENU_TAB);
CMenu* menuSub = NULL;
menuSub = menu.GetSubMenu(0);
if (tcitem.lParam)
{
int lResult = TrackPopupMenuEx(menu.GetSubMenu(0)->m_hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | TPM_RIGHTBUTTON,
rcOver.left, rcOver.bottom - 4,
(HWND)tcitem.lParam, NULL);
if (lResult > 0)
{
SendMessage((HWND)tcitem.lParam, WM_COMMAND, MAKEWPARAM(LOWORD(lResult), 0x0), 0);
}
}
return 1;
}
return 0;
}
LRESULT CTabWnd::OnTabMButtonDown(WPARAM wParam, LPARAM lParam)
{
BreakDrag();
return 0L;
}
LRESULT CTabWnd::OnTabMButtonUp(WPARAM wParam, LPARAM lParam)
{
TCHITTESTINFO hitinfo = {0};
int i = 0;
int nTabCount = 0;
hitinfo.pt.x = LOWORD((DWORD)lParam);
hitinfo.pt.y = HIWORD((DWORD)lParam);
int nDstTab = TabCtrl_HitTest(m_hwndTab, (LPARAM)&hitinfo);
if (nDstTab > -1)
{
CRect rcOver(0, 0, 0, 0);
::SendMessage(m_hwndTab, TCM_GETITEMRECT, nDstTab, (LPARAM)&rcOver);
::ClientToScreen(m_hwndTab, (LPPOINT)&rcOver.left);
::ClientToScreen(m_hwndTab, (LPPOINT)&rcOver.right);
TCITEM tcitem = {0};
tcitem.mask = TCIF_PARAM;
tcitem.lParam = 0;
TabCtrl_GetItem(m_hwndTab, nDstTab, &tcitem);
HWND hCloseWnd = NULL;
hCloseWnd = (HWND)tcitem.lParam;
if (IsWindow(hCloseWnd))
{
::PostMessage(hCloseWnd, WM_COMMAND, ID_W_CLOSE, 0);
return 1;
}
return 1;
}
return 1;
}
LRESULT CTabWnd::OnTabNotify(WPARAM wParam, LPARAM lParam)
{
return 1L;
}
BOOL CTabWnd::ReorderTab(int nSrcTab, int nDstTab)
{
TCITEM tcitem = {0};
if (0 > nSrcTab || 0 > nDstTab || nSrcTab == nDstTab)
return FALSE;
if (m_pwndMainFrame->ReorderTab(nSrcTab, nDstTab))
return FALSE;
return TRUE;
}
CTabWnd::CTabWnd()
: CWndSkr(_T("::CTabWnd")),
m_eDragState(DragState::NONE),
m_bHovering(FALSE),
m_eCaptureSrc(CaptureSrc::NONE),
m_nTabBorderArray(NULL),
m_nSrcTab(0)
{
m_hwndTab = NULL;
gm_pOldWndProc = NULL;
memset(m_szTextTip, 0x00, _countof(m_szTextTip));
m_pwndMainFrame = NULL;
m_pwndFrame = NULL;
m_pwndView = NULL;
m_WheelDCnt = 0;
m_TAB_ITEM_HEIGHT = TAB_ITEM_HEIGHT;
m_TAB_WINDOW_HEIGHT = TAB_WINDOW_HEIGHT;
m_MAX_TABITEM_WIDTH = MAX_TABITEM_WIDTH;
m_MIN_TABITEM_WIDTH = MIN_TABITEM_WIDTH;
m_nTabHover = 0;
m_hDefaultCursor = NULL;
m_ptSrcCursor = {0};
return;
}
CTabWnd::~CTabWnd()
{
if (m_nTabBorderArray)
{
delete[] m_nTabBorderArray;
m_nTabBorderArray = NULL;
}
return;
}
HWND CTabWnd::CreateTab(HINSTANCE hInstance, CWnd* pFrame, CWnd* pView, HWND hwndParent)
{
m_pwndMainFrame = ((CMainFrame*)theApp.m_pMainWnd);
m_pwndFrame = ((CBrowserFrame*)pFrame);
m_pwndView = ((CChildView*)pView);
LPCTSTR pszClassName = _T("CTabWnd");
m_hwndTab = NULL;
gm_pOldWndProc = NULL;
m_eDragState = DragState::NONE;
m_bHovering = FALSE;
m_eCaptureSrc = CaptureSrc::NONE;
RegisterWC(
hInstance,
NULL, // Handle to the class icon.
NULL, // Handle to a small icon
::LoadCursor(NULL, IDC_ARROW), // Handle to the class cursor.
NULL, // Handle to the class background brush.
NULL, // Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file.
pszClassName // Pointer to a null-terminated string or is an atom.
);
RECT rcParent = {0};
::GetWindowRect(hwndParent, &rcParent);
if (theApp.m_ScaleDPI > 1)
{
double dScale = 0.0;
dScale = TAB_ITEM_HEIGHT * theApp.m_ScaleDPI;
m_TAB_ITEM_HEIGHT = (int)dScale;
dScale = TAB_WINDOW_HEIGHT * theApp.m_ScaleDPI;
m_TAB_WINDOW_HEIGHT = (int)dScale;
dScale = MAX_TABITEM_WIDTH * theApp.m_ScaleDPI;
m_MAX_TABITEM_WIDTH = (int)dScale;
dScale = MIN_TABITEM_WIDTH * theApp.m_ScaleDPI;
m_MIN_TABITEM_WIDTH = (int)dScale;
}
CWndSkr::Create(
hwndParent,
0, // extended window style
pszClassName, // Pointer to a null-terminated string or is an atom.
pszClassName, // pointer to window name
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, // window style
CW_USEDEFAULT, // horizontal position of window
0, // vertical position of window
rcParent.right - rcParent.left, // window width
m_TAB_WINDOW_HEIGHT, // window height
NULL // handle to menu, or child-window identifier
);
//タブウインドウを作成する。
m_hwndTab = ::CreateWindow(
WC_TABCONTROL,
_T(""),
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | TCS_TOOLTIPS | TCS_SINGLELINE | TCS_TABS | TCS_FOCUSNEVER | TCS_FORCELABELLEFT | TCS_FIXEDWIDTH,
TAB_MARGIN_LEFT,
TAB_MARGIN_TOP,
rcParent.right - rcParent.left - (TAB_MARGIN_LEFT + TAB_MARGIN_RIGHT),
m_TAB_WINDOW_HEIGHT,
GetHwnd(),
(HMENU)NULL,
GetAppInstance(),
(LPVOID)NULL);
if (m_hwndTab)
{
::SetWindowLongPtr(m_hwndTab, GWLP_USERDATA, (LONG_PTR)this);
gm_pOldWndProc = (WNDPROC)::SetWindowLongPtr(m_hwndTab, GWLP_WNDPROC, (LONG_PTR)TabWndProc);
//スタイルを変更する。
UINT lngStyle;
lngStyle = (UINT)::GetWindowLongPtr(m_hwndTab, GWL_STYLE);
lngStyle &= ~(TCS_BUTTONS | TCS_MULTILINE);
lngStyle |= TCS_SINGLELINE;
lngStyle |= TCS_TABS | TCS_FOCUSNEVER | TCS_FIXEDWIDTH | TCS_FORCELABELLEFT;
::SetWindowLongPtr(m_hwndTab, GWL_STYLE, lngStyle);
TabCtrl_SetItemSize(m_hwndTab, m_MAX_TABITEM_WIDTH, m_TAB_ITEM_HEIGHT);
HWND hwndToolTips;
hwndToolTips = TabCtrl_GetToolTips(m_hwndTab);
lngStyle = (UINT)::GetWindowLongPtr(hwndToolTips, GWL_STYLE);
lngStyle |= TTS_ALWAYSTIP | TTS_NOPREFIX;
::SetWindowLongPtr(hwndToolTips, GWL_STYLE, lngStyle);
LOGFONT lf = {0};
SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &lf, 0);
m_hFont.CreateFontIndirect(&lf);
::SendMessage(m_hwndTab, WM_SETFONT, (WPARAM)m_hFont.GetSafeHandle(), MAKELPARAM(TRUE, 0));
//Tab作成が完了したので、Listに追加。
this->m_pwndMainFrame->Add_TabWindow(m_pwndFrame);
TabCtrl_SetImageList(m_hwndTab, theApp.m_imgFavIcons);
Refresh();
}
return GetHwnd();
}
void CTabWnd::Close(void)
{
if (GetHwnd())
{
if (gm_pOldWndProc)
{
::SetWindowLongPtr(m_hwndTab, GWLP_WNDPROC, (LONG_PTR)gm_pOldWndProc);
gm_pOldWndProc = NULL;
}
::SetWindowLongPtr(m_hwndTab, GWLP_USERDATA, (LONG_PTR)NULL);
this->DestroyWindow();
}
}
LRESULT CTabWnd::OnSize(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (NULL == GetHwnd() || NULL == m_hwndTab) return 0L;
LayoutTab();
::InvalidateRect(GetHwnd(), NULL, FALSE);
return 0L;
}
LRESULT CTabWnd::OnDestroy(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (m_hwndTab)
{
::DestroyWindow(m_hwndTab);
m_hwndTab = NULL;
}
::KillTimer(hwnd, 1);
_SetHwnd(NULL);
return 0L;
}
LRESULT CTabWnd::OnLButtonDblClk(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return 0L;
}
LRESULT CTabWnd::OnCaptureChanged(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (m_eCaptureSrc != CaptureSrc::NONE)
m_eCaptureSrc = CaptureSrc::NONE;
return 0L;
}
LRESULT CTabWnd::OnLButtonDown(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return 0L;
}
LRESULT CTabWnd::OnLButtonUp(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
POINT pt = {0};
RECT rc = {0};
RECT rcBtn = {0};
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
::GetClientRect(GetHwnd(), &rc);
if (::GetCapture() == GetHwnd()) // 自ウィンドウがマウスキャプチャーしている?
{
// キャプチャー解除
m_eCaptureSrc = CaptureSrc::NONE;
::ReleaseCapture();
}
return 0L;
}
LRESULT CTabWnd::OnRButtonDown(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return 0L;
}
LRESULT CTabWnd::OnMouseMove(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// カーソルがウィンドウ内に入ったらタイマー起動
// ウィンドウ外に出たらタイマー削除
POINT pt = {0};
RECT rc = {0};
BOOL bHovering = {0};
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
::GetClientRect(hwnd, &rc);
bHovering = ::PtInRect(&rc, pt);
if (bHovering != m_bHovering)
{
m_bHovering = bHovering;
if (m_bHovering)
::SetTimer(hwnd, 1, 200, NULL);
else
::KillTimer(hwnd, 1);
}
return 0L;
}
LRESULT CTabWnd::OnTimer(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (wParam == 1)
{
// カーソルがウィンドウ外にある場合にも WM_MOUSEMOVE を送る
POINT pt = {0};
RECT rc = {0};
::GetCursorPos(&pt);
::ScreenToClient(hwnd, &pt);
::GetClientRect(hwnd, &rc);
if (!::PtInRect(&rc, pt))
::SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELONG(pt.x, pt.y));
}
return 0L;
}
LRESULT CTabWnd::OnMouseWheel(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int zDelta = (SHORT)HIWORD(wParam);
m_WheelDCnt += zDelta;
if (m_WheelDCnt > 120 * 100)
m_WheelDCnt = 0;
else if (m_WheelDCnt < -120 * 100)
m_WheelDCnt = 0;
if (m_WheelDCnt > 0)
{
//120単位なので2回転に1回
//if(m_WheelDCnt >= 120*2)
if (m_WheelDCnt >= 120)
{
m_pwndFrame->OnPrevWnd();
m_WheelDCnt = 0;
}
}
else
{
//if (m_WheelDCnt <= -120*2)
if (m_WheelDCnt <= -120)
{
m_pwndFrame->OnNextWnd();
m_WheelDCnt = 0;
}
}
return 1;
}
LRESULT CTabWnd::OnEraseBkgnd(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc = {0};
PAINTSTRUCT ps = {0};
RECT rc = {0};
//描画対象
hdc = ::BeginPaint(hwnd, &ps);
// 背景を描画する
::GetClientRect(hwnd, &rc);
::FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW));
::EndPaint(hwnd, &ps);
return 1;
}
LRESULT CTabWnd::OnPaint(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc = {0};
PAINTSTRUCT ps = {0};
RECT rc = {0};
//描画対象
hdc = ::BeginPaint(hwnd, &ps);
int nCurSel = TabCtrl_GetCurSel(m_hwndTab);
nCurSel = FindTabIndexByHWND(this->m_pwndFrame->GetSafeHwnd());
if (nCurSel >= 0)
{
POINT pt = {0};
RECT rcCurSel = {0};
TabCtrl_GetItemRect(m_hwndTab, nCurSel, &rcCurSel);
pt.x = rcCurSel.left;
pt.y = 0;
::ClientToScreen(m_hwndTab, &pt);
::ScreenToClient(GetHwnd(), &pt);
rcCurSel.right = pt.x + (rcCurSel.right - rcCurSel.left); // - 1;
rcCurSel.left = pt.x; // + 1;
rcCurSel.top = rc.top + TAB_MARGIN_TOP - 4;
rcCurSel.bottom = rc.top + TAB_MARGIN_TOP;
if (rcCurSel.left < rc.left + TAB_MARGIN_LEFT)
rcCurSel.left = rc.left + TAB_MARGIN_LEFT; // 左端限界値
RECT rcTabTop = {0};
::GetClientRect(this->GetHwnd(), &rcTabTop);
rcTabTop.bottom = rcTabTop.top + 8;
HBRUSH hBrb = ::CreateSolidBrush(RGB(207, 214, 229));
::FillRect(hdc, &rcTabTop, hBrb);
::DeleteObject(hBrb);
if (rcCurSel.left < rcCurSel.right)
{
HBRUSH hBr = ::CreateSolidBrush(RGB(10, 132, 255)); //( RGB( 255, 128, 0 ) );
::FillRect(hdc, &rcCurSel, hBr);
::DeleteObject(hBr);
}
}
::EndPaint(hwnd, &ps);
return 0L;
}
LRESULT CTabWnd::OnNotify(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
NMHDR* pnmh = (NMHDR*)lParam;
if (pnmh->hwndFrom == TabCtrl_GetToolTips(m_hwndTab))
{
switch (pnmh->code)
{
case TTN_GETDISPINFO:
// ツールチップ表示情報を設定する
TCITEM tcitem = {0};
tcitem.mask = TCIF_PARAM;
tcitem.lParam = (LPARAM)NULL;
if (TabCtrl_GetItem(m_hwndTab, pnmh->idFrom, &tcitem))
{
CString strRet;
strRet = this->m_pwndMainFrame->Get_TabWindowInfo_Title((HWND)tcitem.lParam);
StringCchCopy(m_szTextTip, _countof(m_szTextTip), strRet);
((NMTTDISPINFO*)pnmh)->lpszText = m_szTextTip;
((NMTTDISPINFO*)pnmh)->hinst = NULL;
}
return 0L;
}
}
return 0L;
}
void CTabWnd::TabWindowNotify(WPARAM wParam, LPARAM lParam)
{
if (NULL == m_hwndTab) return;
bool bFlag = false; //前回何もタブがなかったか?
int nCount = {0};
int nIndex = {0};
HWND hwndUpDown = {0};
DWORD nScrollPos = {0};
BreakDrag();
nCount = TabCtrl_GetItemCount(m_hwndTab);
if (nCount <= 0)
{
bFlag = true;
//最初のときはすでに存在するウインドウの情報も登録する必要がある。
// 起動時、CTabWnd::Open()内のRefresh()ではまだグループ入り前のため既に別ウィンドウがあってもタブは空
if (wParam == TWNT_ADD_COMMAND)
Refresh(); // 続けてTWNT_ADD処理で自分以外のウィンドウを隠す
}
switch (wParam)
{
case TWNT_ADD_COMMAND: //ウインドウ登録
nIndex = FindTabIndexByHWND((HWND)lParam);
if (-1 == nIndex)
{
TCITEM tcitem = {0};
TCHAR szName[1024] = {0};
_tcscpy_s(szName, _T("blank"));
tcitem.mask = TCIF_TEXT | TCIF_PARAM | TCIF_IMAGE;
tcitem.pszText = szName;
tcitem.lParam = (LPARAM)lParam;
tcitem.iImage = nCount;
TabCtrl_InsertItem(m_hwndTab, nCount, &tcitem);
nIndex = nCount;
}
//自分ならアクティブに
if (!theApp.IsWndVisible(m_pwndFrame->m_hWnd))
{
ShowTabWindow(m_pwndFrame->m_hWnd);
//ここに来たということはすでにアクティブ
//コマンド実行時のアウトプットで問題があるのでアクティブにする
}
TabCtrl_SetCurSel(m_hwndTab, nIndex);
// 自分以外を隠す
HideOtherWindows(m_pwndFrame->m_hWnd);
break;
case TWNT_DEL_COMMAND: //ウインドウ削除
nIndex = FindTabIndexByHWND((HWND)lParam);
if (-1 != nIndex)
{
if (!theApp.IsWndVisible(m_pwndFrame->m_hWnd))
{
ShowTabWindow(m_pwndFrame->m_hWnd);
ForceActiveWindow(m_pwndFrame->m_hWnd);
::InvalidateRect(GetHwnd(), NULL, FALSE);
}
// (右端のほうのタブアイテムを削除したとき、スクロール可能なのに右に余白ができることへの対策)
hwndUpDown = ::FindWindowEx(m_hwndTab, NULL, UPDOWN_CLASS, 0); // タブ内の Up-Down コントロール
if (hwndUpDown != NULL && ::IsWindowVisible(hwndUpDown))
{
nScrollPos = LOWORD(UpDown_GetPos(hwndUpDown));
// 現在位置 nScrollPos と画面表示とを一致させる
::SendMessage(m_hwndTab, WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, LOWORD(nScrollPos)), (LPARAM)NULL); // 設定位置にタブをスクロール
}
}
break;
case TWNT_ORDER_COMMAND: //ウインドウ順序変更
nIndex = FindTabIndexByHWND((HWND)lParam);
if (-1 != nIndex)
{
//自分ならアクティブに
if (!theApp.IsWndVisible(m_pwndFrame->m_hWnd))
{
ShowTabWindow(m_pwndFrame->m_hWnd);
}
RECT rcTab = {0};
nCount = 0;
int cx = 0;
::GetClientRect(m_hwndTab, &rcTab);
nCount = TabCtrl_GetItemCount(m_hwndTab);
if (0 < nCount)
{
cx = (rcTab.right - rcTab.left - 8) / nCount;
int min = m_MIN_TABITEM_WIDTH;
if (m_MAX_TABITEM_WIDTH < cx)
cx = m_MAX_TABITEM_WIDTH;
else if (m_MIN_TABITEM_WIDTH > cx)
cx = m_MIN_TABITEM_WIDTH;
TabCtrl_SetItemSize(m_hwndTab, cx, m_TAB_ITEM_HEIGHT);
}
// 自タブアイテムを強制的に可視位置にするために、
// 自タブアイテム選択前に一時的に画面左端のタブアイテムを選択する
nScrollPos = (hwndUpDown != NULL && ::IsWindowVisible(hwndUpDown)) ? LOWORD(UpDown_GetPos(hwndUpDown)) : 0;
TabCtrl_SetCurSel(m_hwndTab, nScrollPos);
TabCtrl_SetCurSel(m_hwndTab, nIndex);
// 自分以外を隠す
// (連続切替時に TWNT_ORDER が大量発生・交錯して?画面がすべて消えてしまったりするのを防ぐ)
HideOtherWindows(m_pwndFrame->m_hWnd);
}
break;
case TWNT_REFRESH_COMMAND: //再表示
Refresh((BOOL)lParam);
if (lParam)
::InvalidateRect(this->GetHwnd(), NULL, FALSE);
break;
case TWNT_WNDPL_ADJUST_COMMAND: // ウィンドウ位置合わせ
AdjustWindowPlacement();
LayoutTab();
::InvalidateRect(this->GetHwnd(), NULL, FALSE);
return;
default:
break;
}
return;
}
int CTabWnd::FindTabIndexByHWND(HWND hWnd)
{
int i = {0};
int nCount = {0};
TCITEM tcitem = {0};
if (NULL == m_hwndTab) return -1;
nCount = TabCtrl_GetItemCount(m_hwndTab);
for (i = 0; i < nCount; i++)
{
tcitem.mask = TCIF_PARAM;
tcitem.lParam = (LPARAM)0;
TabCtrl_GetItem(m_hwndTab, i, &tcitem);
if ((HWND)tcitem.lParam == hWnd) return i;
}
return -1;
}
void CTabWnd::Refresh(BOOL bEnsureVisible /* = TRUE*/, BOOL bRebuild /* = FALSE*/)
{
TCITEM tcitem = {0};
int nCount = 0;
int nGroup = 0;
int nTab = 0;
int nSel = 0;
int nCurTab = 0;
int nCurSel = 0;
int i = 0;
int j = 0;
if (NULL == m_hwndTab) return;
if (!m_pwndMainFrame)