xref: /MusicPlayer2/MusicPlayer2/CTabCtrlEx.cpp (revision 6d629c9dd07271cb88f39e336efcd77d92dbaaae)
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 
12 IMPLEMENT_DYNAMIC(CTabCtrlEx, CTabCtrl)
13 
14 CTabCtrlEx::CTabCtrlEx()
15 {
16 
17 }
18 
19 CTabCtrlEx::~CTabCtrlEx()
20 {
21 }
22 
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 
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 
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 
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 
104 void CTabCtrlEx::CalSubWindowSize()
105 {
106     GetClientRect(m_tab_rect);
107     CRect rc_temp = m_tab_rect;
108     AdjustRect(FALSE, rc_temp);
109     int margin = rc_temp.left - m_tab_rect.left;
110     CRect rcTabItem;
111     GetItemRect(0, rcTabItem);
112     m_tab_rect.top += rcTabItem.Height() + margin;
113     m_tab_rect.left += margin;
114     m_tab_rect.bottom -= margin;
115     m_tab_rect.right -= margin;
116 }
117 
118 
119 BEGIN_MESSAGE_MAP(CTabCtrlEx, CTabCtrl)
120 	ON_NOTIFY_REFLECT(TCN_SELCHANGE, &CTabCtrlEx::OnTcnSelchange)
121     ON_WM_SIZE()
122 END_MESSAGE_MAP()
123 
124 
125 
126 // CTabCtrlEx 消息处理程序
127 
128 
129 
130 
131 void CTabCtrlEx::OnTcnSelchange(NMHDR *pNMHDR, LRESULT *pResult)
132 {
133 	// TODO: 在此添加控件通知处理程序代码
134 	int tab_selected = GetCurSel();
135 	SetCurTab(tab_selected);
136 
137 	*pResult = 0;
138 }
139 
140 
141 void CTabCtrlEx::PreSubclassWindow()
142 {
143 	// TODO: 在此添加专用代码和/或调用基类
144 
145 	//计算子窗口的位置
146     CalSubWindowSize();
147 
148 	CTabCtrl::PreSubclassWindow();
149 }
150 
151 
152 void CTabCtrlEx::OnSize(UINT nType, int cx, int cy)
153 {
154     CTabCtrl::OnSize(nType, cx, cy);
155 
156     // TODO: 在此处添加消息处理程序代码
157     AdjustTabWindowSize();
158 }
159