1 // CTabCtrlEx.cpp: 实现文件
2 //
3
4 #include "stdafx.h"
5 #include "MusicPlayer2.h"
6 #include "CTabCtrlEx.h"
7 #include "TabDlg.h"
8
9
10 // CTabCtrlEx
11
IMPLEMENT_DYNAMIC(CTabCtrlEx,CTabCtrl)12 IMPLEMENT_DYNAMIC(CTabCtrlEx, CTabCtrl)
13
14 CTabCtrlEx::CTabCtrlEx()
15 {
16
17 }
18
~CTabCtrlEx()19 CTabCtrlEx::~CTabCtrlEx()
20 {
21 }
22
AddWindow(CWnd * pWnd,LPCTSTR lable_text,IconMgr::IconType icon_type)23 void CTabCtrlEx::AddWindow(CWnd* pWnd, LPCTSTR lable_text, IconMgr::IconType icon_type)
24 {
25 if (pWnd == nullptr || pWnd->GetSafeHwnd() == NULL)
26 return;
27
28 InsertItem(m_tab_list.size(), lable_text, m_tab_list.size());
29
30 pWnd->SetParent(this);
31 pWnd->MoveWindow(m_tab_rect);
32
33 m_tab_list.push_back(pWnd);
34 if (icon_type != IconMgr::IconType::IT_NO_ICON)
35 m_icon_list.push_back(icon_type);
36 }
37
SetCurTab(int index)38 void CTabCtrlEx::SetCurTab(int index)
39 {
40 if (index < 0 || index >= static_cast<int>(m_tab_list.size()))
41 index = 0;
42 SetCurSel(index);
43
44 int tab_size = m_tab_list.size();
45 for (int i = 0; i < tab_size; i++)
46 {
47 if (i == index)
48 {
49 m_tab_list[i]->ShowWindow(SW_SHOW);
50 m_tab_list[i]->SetFocus();
51 }
52 else
53 {
54 m_tab_list[i]->ShowWindow(SW_HIDE);
55 }
56 }
57
58 CTabDlg* pTabWnd = dynamic_cast<CTabDlg*>(m_tab_list[index]);
59 if (pTabWnd != nullptr)
60 pTabWnd->OnTabEntered();
61
62 if (m_last_tab_index != index && m_last_tab_index >= 0 && m_last_tab_index < static_cast<int>(m_tab_list.size()))
63 {
64 CTabDlg* pLastTabWnd = dynamic_cast<CTabDlg*>(m_tab_list[m_last_tab_index]);
65 if (pLastTabWnd != nullptr)
66 pLastTabWnd->OnTabExited();
67 }
68
69 m_last_tab_index = index;
70 }
71
GetCurrentTab()72 CWnd* CTabCtrlEx::GetCurrentTab()
73 {
74 size_t cur_tab_index = GetCurSel();
75 if (cur_tab_index >= 0 && cur_tab_index < m_tab_list.size())
76 {
77 return m_tab_list[cur_tab_index];
78 }
79 return nullptr;
80 }
81
AdjustTabWindowSize()82 void CTabCtrlEx::AdjustTabWindowSize()
83 {
84 CalSubWindowSize();
85 for (size_t i{}; i < m_tab_list.size(); i++)
86 {
87 m_tab_list[i]->MoveWindow(m_tab_rect);
88 }
89 //为每个标签添加图标
90 if (m_icon_list.empty())
91 return;
92 CSize icon_size = IconMgr::GetIconSize(IconMgr::IconSize::IS_DPI_16);
93 CImageList ImageList;
94 ImageList.Create(icon_size.cx, icon_size.cy, ILC_COLOR32 | ILC_MASK, 2, 2);
95 for (auto icon_type : m_icon_list)
96 {
97 HICON hIcon = theApp.m_icon_mgr.GetHICON(icon_type, IconMgr::IconStyle::IS_OutlinedDark, IconMgr::IconSize::IS_DPI_16);
98 ImageList.Add(hIcon);
99 }
100 SetImageList(&ImageList);
101 ImageList.Detach();
102 }
103
Clear()104 void CTabCtrlEx::Clear()
105 {
106 DeleteAllItems();
107 m_tab_list.clear();
108 m_icon_list.clear();
109 }
110
CalSubWindowSize()111 void CTabCtrlEx::CalSubWindowSize()
112 {
113 GetClientRect(m_tab_rect);
114 CRect rc_temp = m_tab_rect;
115 AdjustRect(FALSE, rc_temp);
116 int margin = rc_temp.left - m_tab_rect.left;
117 CRect rcTabItem;
118 GetItemRect(0, rcTabItem);
119 m_tab_rect.top += rcTabItem.Height() + margin;
120 m_tab_rect.left += margin;
121 m_tab_rect.bottom -= margin;
122 m_tab_rect.right -= margin;
123 }
124
125
BEGIN_MESSAGE_MAP(CTabCtrlEx,CTabCtrl)126 BEGIN_MESSAGE_MAP(CTabCtrlEx, CTabCtrl)
127 ON_NOTIFY_REFLECT(TCN_SELCHANGE, &CTabCtrlEx::OnTcnSelchange)
128 ON_WM_SIZE()
129 END_MESSAGE_MAP()
130
131
132
133 // CTabCtrlEx 消息处理程序
134
135
136
137
138 void CTabCtrlEx::OnTcnSelchange(NMHDR *pNMHDR, LRESULT *pResult)
139 {
140 // TODO: 在此添加控件通知处理程序代码
141 int tab_selected = GetCurSel();
142 SetCurTab(tab_selected);
143
144 *pResult = 0;
145 }
146
147
PreSubclassWindow()148 void CTabCtrlEx::PreSubclassWindow()
149 {
150 // TODO: 在此添加专用代码和/或调用基类
151
152 //计算子窗口的位置
153 CalSubWindowSize();
154
155 CTabCtrl::PreSubclassWindow();
156 }
157
158
OnSize(UINT nType,int cx,int cy)159 void CTabCtrlEx::OnSize(UINT nType, int cx, int cy)
160 {
161 CTabCtrl::OnSize(nType, cx, cy);
162
163 // TODO: 在此处添加消息处理程序代码
164 AdjustTabWindowSize();
165 }
166