需求:對話框大小改變時,狀態欄各面板寬度自適應調整(如均分或按比例)。
CStatusBar m_wndStatusBar 成員。OnInitDialog 中創建並設置指示器:m_wndStatusBar.Create(this); m_wndStatusBar.SetIndicators(indicators, count);OnSize 消息處理中計算寬度並調用 SetPaneInfo:void CMyDialog::OnSize(UINT nType, int cx, int cy) {
CDialog::OnSize(nType, cx, cy);
if (m_wndStatusBar.GetSafeHwnd()) {
CRect rect;
m_wndStatusBar.GetWindowRect(&rect);
ScreenToClient(&rect);
int paneCount = 3; // 面板數
int paneWidth = rect.Width() / paneCount;
for (int i = 0; i < paneCount; ++i) {
UINT id = m_wndStatusBar.GetItemID(i);
// 最後一個面板用 SBPS_STRETCH 自動填充剩餘
if (i == paneCount - 1)
m_wndStatusBar.SetPaneInfo(i, id, SBPS_STRETCH, 0);
else
m_wndStatusBar.SetPaneInfo(i, id, SBPS_NORMAL, paneWidth);
}
m_wndStatusBar.Invalidate();
}
}
SetPaneInfo(index, id, style, width):style 可為 SBPS_NORMAL(固定寬度)、SBPS_STRETCH(拉伸佔滿剩餘)、SBPS_POPOUT(凹陷邊框)。SBPS_STRETCH 配合 width=0 讓最後一面板自動適應,無需手動計算餘寬。百度百科 MFC StatusBar 面板設置:https://baike.baidu.com/link?url=_ZZZCUxppVRm7zfGER8DM-RrTGru0s2KBw1rPRhbBCZQOYytK9B1qCIESlPDDAB9aYdNC30eVHjCiA0IL6dMzK