狀態欄的編程
VS2010提供了CMFCStatusBar用於控制狀態欄的顯示,在狀態欄中,可以顯示圖標、進度條、圖形動畫、更改文本的顏色和背景色、雙機相應狀態欄產生消息。
首先利用AppWizard建立工程文件,注意建立的工程文件視類的基類選擇爲CFormView,在CMainFrame中定義狀態欄並設置狀態欄。
依據狀態欄中出現的次序定義常量,確定修改哪一個狀態欄。
const int nStatusIcon = 0;
const int nStatusInfo = 1;
const int nStatusProgress = 2;
const int nStatusLabel = 3;
const int nStatusAnimation =4;
重新修改狀態欄的標識函數
static UINT indicators[] =
{
ID_INDICATOR_ICON, //status icon
ID_SEPARATOR, // statusline indicator
ID_INDICATOR_PROGRESS, // progress bar
ID_INDICATOR_LABEL, // textlabel
ID_INDICATOR_ANIMATION, // animation pane
};
設置狀態欄的格式
m_wndStatusBar.SetPaneStyle(nStatusIcon, SBPS_NOBORDERS);
m_wndStatusBar.SetPaneStyle(nStatusAnimation, SBPS_NOBORDERS);
m_wndStatusBar.SetPaneStyle(nStatusInfo, SBPS_STRETCH | SBPS_NOBORDERS);
m_wndStatusBar.SetPaneWidth(nStatusProgress, 80);
m_wndStatusBar.EnablePaneDoubleClick();
編輯Form資源,加入需要的控件並編程。
要在視類中訪問狀態欄控件,需要編寫函數GetStatusBar()
CMFCStatusBar&GetStatusBar () const
{
return ((CMainFrame*) AfxGetMainWnd ())->GetStatusBar();
}
另外,在CMainFrame中加入GetStatusBar代碼
CMFCStatusBar&GetStatusBar ()
{
return m_wndStatusBar;
}
視類的初始化,修改OnInitialUpdate函數並加入狀態欄相關的控件初始化代碼
//動畫圖標的載入
if(m_imlStatusAnimation.GetSafeHandle()==NULL)
{
m_imlStatusAnimation.Create(IDB_ANIMATE,16,0,RGB(255,0,255));
}
//位圖圖標的載入
if(m_bmpIcon1.GetSafeHandle() == NULL)
{
m_bmpIcon1.LoadBitmapW(IDB_ICON1);
}
//位圖圖標的載入
if(m_bmpIcon2.GetSafeHandle() == NULL)
{
m_bmpIcon2.LoadBitmapW(IDB_ICON2);
}
//載入圖標
GetStatusBar().SetPaneIcon(nStatusIcon,m_bmpIcon1);
GetStatusBar().SetTipText(nStatusIcon,_T("this is tooltip"));
進度條的啓動和停止
if(m_ShowProgress)
{
KillTimer(ID_PROGRESS_TIMER);
m_ProgressBut.SetWindowTextW(_T("Show Progress"));
GetStatusBar().EnablePaneProgressBar (nStatusProgress, -1);
m_ShowProgress= FALSE;
return;
}
SetTimer(ID_PROGRESS_TIMER,100,NULL);
GetStatusBar().EnablePaneProgressBar (nStatusProgress, PROGRESS_MAX);
m_ShowProgress= TRUE;
m_ProgressBut.SetWindowTextW(_T("Stop Progress"));
在WM_TIMER中加入動態進度條的控制代碼
if(nIDEvent == ID_PROGRESS_TIMER)
{
nProgressPos+=5;
if(nProgressPos > PROGRESS_MAX)
{
nProgressPos= 0;
}
GetStatusBar().SetPaneProgress(nStatusProgress, nProgressPos);
}
動畫演示的啓動和停止
if(m_ShowAnimation)
{
m_ShowAnimation= FALSE;
m_AnimateBut.SetWindowTextW(_T("Start Animation"));
GetStatusBar().SetPaneAnimation (nStatusAnimation, NULL);
}
else
{
m_ShowAnimation= TRUE;
m_AnimateBut.SetWindowTextW(_T("Stop Animation"));
GetStatusBar().SetPaneAnimation (nStatusAnimation, m_imlStatusAnimation);
GetStatusBar().SetPaneText (nStatusAnimation, _T(""));
GetStatusBar().SetPaneWidth (nStatusAnimation, 16);
}
圖標的變換
If(m_nIcon==0)
{
return;
}
m_nIcon= 0;
GetStatusBar().SetPaneIcon (nStatusIcon, m_bmpIcon1);
背景色和顯示文本顏色的改變
m_backColor= m_BackColorBut.GetColor();
GetStatusBar().SetPaneBackgroundColor(nStatusLabel,m_backColor);
m_textColor= m_TextColorBut.GetColor();
GetStatusBar().SetPaneTextColor(nStatusLabel,m_textColor);
鼠標雙擊狀態欄消息的建立
可以利用狀態欄的ID建立ON_COMMAND消息並建立處理函數,在其中相應該函數即可
最終軟件界面如下: