工具欄(ToolBar)是一種非常方便的控件,能大大增加用戶操作的效率,但是基于對話框的程序,卻不能像使用編輯框(Edit Box)和列表框(List Box)一樣,方便地增加工具欄控件。本文將介紹一種在對話框中加入工具欄的方法。
一、 技術(shù)要點分析
所有的Windows控件(包括工具欄、編輯框等)都派生自CWnd類,這就意味著,我們可以用窗口類的Create()函數(shù)把它們“創(chuàng)建”并顯示到另一個窗口(例如對話框)上。把工具欄加入到對話框中正是使用了這樣的一種方法。
通常,我們使用CToolBarCtrl類(派生自CWnd類)來創(chuàng)建并管理工具欄控件。使用這個類創(chuàng)建一條工具欄的一般步驟如下:
1.派生一個CToolBarCtrl的對象;
2.調(diào)用CToolBarCtrl::Create函數(shù)創(chuàng)建工具欄對象;
3.調(diào)用CToolBarCtrl::AddBitmap()和CToolBarCtrl::AddString()為工具欄對象加入位圖和提示信息;
4.派生一個TBUTTON數(shù)組對象進行工具欄中各按鈕的具體設(shè)置;
5.修改主窗口的OnNotify()函數(shù),以顯示工具欄上的提示信息。
以上步驟在下面的范例代碼中會有具體體現(xiàn)。
二、 范例程序的建立與主要代碼分析
利用Visual C++ 的向?qū)梢粋€基于對話框的程序,命名為ToolBarInDial。修改主對話框樣式如圖1。繪出一條工具欄的位圖并建立一選單,設(shè)置幾個子選單項,然后建立一組工具欄的提示信息串(String Table),一旦鼠標(biāo)在工具欄某項上停留,就會顯示提示信息。下面給出程序中的主要代碼。
在主對話框CToolBarInDialDlg的類定義中有如下的變量說明:
CToolBarCtrl ToolBar;
int ButtonCount;
int ButtonBitmap;
BOOL DoFlag;
TBBUTTON m_Button[5];
//設(shè)置工具欄上具體信息的變量數(shù)組
//主對話框的初始化函數(shù)
BOOL CToolBarInDialDlg::OnInitDialog()
{
RECT rect;
//設(shè)置工具欄的顯示范圍
rect.top=0; rect.left=0; rect.right=48; rect.bottom=16;
ToolBar.Create(WS_CHILD|WS_VISIBLE|CCS_TOP|TBSTYLE_TOOLTIPS|CCS_ADJUSTABLE,rect,this,0);
//建立工具欄并設(shè)置工具欄的樣式
ButtonBitmap=ToolBar.AddBitmap(5,IDB_PLAY); //加入工具欄的位圖
ButtonString=ToolBar.AddString(IDS_FIRST);//加入工具欄的提示信息
//以下代碼開始設(shè)置各具體的按鈕
m_Buttons[ButtonCount].iBitmap=
ButtonBitmap+ButtonCount; //ButtonCount初值為0
m_Buttons[ButtonCount].idCommand=ID_PLAY; //工具欄與選單上某子項對應(yīng)
m_Buttons[ButtonCount].fsState=TBSTATE_ENABLED;
//設(shè)置工具欄按鈕為可選
m_Buttons[ButtonCount].fsStyle=TBSTYLE_BUTTON;
//設(shè)置工具欄按鈕為普通按鈕
m_Buttons[ButtonCount].dwData=0;
m_Buttons[ButtonCount].iString=IDS_LAST;
++ButtonCount;
//類似地設(shè)置第二個按鈕
m_Buttons[ButtonCount].iBitmap=ButtonBitmap+ButtonCount;
m_Buttons[ButtonCount].idCommand=ID_STOP;
m_Buttons[ButtonCount].fsState=TBSTATE_ENABLED;
m_Buttons[ButtonCount].fsStyle=TBSTYLE_BUTTON;
m_Buttons[ButtonCount].dwData=0;
m_Buttons[ButtonCount].iString=IDS_NEXT;
++ButtonCount;
……//省略設(shè)置剩下的按鈕的代碼
ToolBar.AddButtons(ButtonCount,m_Buttons);
//為工具欄加入按鈕并顯示在對話框中
return TRUE;
}
//當(dāng)鼠標(biāo)在工具欄上停留時,調(diào)用這個函數(shù)來顯示提示信息
BOOL CToolBarInDialDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
TOOLTIPTEXT* tt;
tt=(TOOLTIPTEXT*)lParam;
CString Tip;
switch(tt->hdr.code)
{
case TTN_NEEDTEXT:
//該信息表明要求顯示工具欄上的提示
switch(tt->hdr.idFrom)
{
case ID_PLAY:
Tip.LoadString(IDS_FIRST); //設(shè)置對應(yīng)于工具欄上ID_PLAY的按鈕的提示信息
break;
case ID_STOP:
Tip.LoadString(IDS_NEXT);
//IDS_FIRST,IDS_NEXT等為一系列CString串
break;
……//類似地設(shè)置剩下按鈕的提示信息
}
strcpy(tt->szText,(LPCSTR)Tip);
//顯示提示信息
break;
}
return CDialog::OnNotify(wParam, lParam, pResult);
}
//該演示程序的工具欄能由用戶定制,隨時增加或刪除工具欄中的某一項
void CToolBarInDialDlg::OnApply()
{
switch(DoFlag) //用戶選擇了增加或刪除工具欄中的“退出”按鈕
{
case TRUE: //增加工具欄上的“退出”按鈕
m_Buttons[ButtonCount].iBitmap=ButtonBitmap+ButtonCount;
m_Buttons[ButtonCount].idCommand=ID_QUIT;
m_Buttons[ButtonCount].fsState=TBSTATE_ENABLED;
m_Buttons[ButtonCount].fsStyle=TBSTYLE_BUTTON;
m_Buttons[ButtonCount].dwData=0;
m_Buttons[ButtonCount].iString=IDS_FIRST;
ToolBar.InsertButton(ButtonCount,&&m_Buttons[ButtonCount]);
//根據(jù)m_Buttons的信息在工具欄的尾部加上一個按鈕
break;
case FALSE:
if(ToolBar.GetButtonCount()==4) //刪除工具欄上某一特定位置的按鈕
{
ToolBar.DeleteButton(3);
//刪除工具欄上某一按鈕
}
break;
}
}
void CToolBarInDialDlg::OnPlay() //響應(yīng)函數(shù)舉例
{
……
//對應(yīng)選單項的響應(yīng)函數(shù)
}