topic1:浮動工具條。
在前面說到浮動工具條,實際上是新建了一個工具欄,可以拖放到菜單中的。而此處是,初始化創(chuàng)建程序的時候,工具欄上面不是固定在菜單下面的,而是在浮動的,最終,如果拖放的話,可以固定在原來的位置上面。代碼很簡單。
int?CMainFrame::OnCreate(LPCREATESTRUCT?lpCreateStruct)


{
????if?(CFrameWnd::OnCreate(lpCreateStruct)?==?-1)
????????return?-1;
????
????if?(!m_wndToolBar.CreateEx(this,?TBSTYLE_FLAT,?WS_CHILD?|?WS_VISIBLE?|?CBRS_TOP
????????|?CBRS_GRIPPER?|?CBRS_TOOLTIPS?|?CBRS_FLYBY?|?CBRS_SIZE_DYNAMIC)?||
????????!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))

????
{
????????TRACE0("Failed?to?create?toolbar\n");
????????return?-1;??????//?fail?to?create
????}

????if?(!m_wndStatusBar.Create(this)?||
????????!m_wndStatusBar.SetIndicators(indicators,
??????????sizeof(indicators)/sizeof(UINT)))

????
{
????????TRACE0("Failed?to?create?status?bar\n");
????????return?-1;??????//?fail?to?create
????}

????//?TODO:?Delete?these?three?lines?if?you?don't?want?the?toolbar?to
????//??be?dockable
????m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
????EnableDocking(CBRS_ALIGN_ANY);
????DockControlBar(&m_wndToolBar);
????

????CRect?rect;
????GetWindowRect(&rect);
????CPoint?point(rect.left,rect.top+250);
????FloatControlBar(&m_wndToolBar,point,CBRS_ALIGN_LEFT);


????return?0;
}添加的代碼如上,這樣,只是改變了初始化的工具欄,用到的是默認(rèn)的那個m_wndToolBar,自己并沒有新建一個。
Topic 2:? 調(diào)整工具欄按鈕的位置。
工具欄上面有很多按鈕實現(xiàn)不同的功能,如何調(diào)整他們的順序呢?比如將新建和保存兩個按鈕的順序?qū)φ{(diào),當(dāng)然,按鈕的外觀和功能也要對調(diào)啊。
通過SetButtonInfo函數(shù)來實現(xiàn)。
void?CMainFrame::OnChange()?


{
????//?TODO:?Add?your?command?handler?code?here
????UINT?firstID,secondID;
????firstID?=?m_wndToolBar.GetItemID(0);
????secondID?=?m_wndToolBar.GetItemID(1);
????m_wndToolBar.SetButtonInfo(2,firstID,0,0);
????m_wndToolBar.SetButtonInfo(0,secondID,0,2);
}

此處添加了一個菜單,對應(yīng)的函數(shù)來響應(yīng),OK。相關(guān)的函數(shù)就是那個,不需要多說明。
Topic 3: 狀態(tài)欄中顯示鼠標(biāo)的位置。
如何在移動鼠標(biāo)后,馬上顯示出鼠標(biāo)的位置呢?
添加消息WM_MOUSEMOVE的響應(yīng)函數(shù)OnMouseMove函數(shù)。
void?CTest23View::OnMouseMove(UINT?nFlags,?CPoint?point)?


{
????//?TODO:?Add?your?message?handler?code?here?and/or?call?default
????CString?str;
????CMainFrame?*pMainFrame?=?(CMainFrame?*)AfxGetApp()->m_pMainWnd;????//獲取主窗口的指針
????CStatusBar?*pState?=?&pMainFrame->m_wndStatusBar;????
????str.Format("X=%d,Y=%d",point.x,point.y);????//文本格式化
????pState->SetPaneText(1,str,TRUE);????????//更新狀態(tài)欄第二項內(nèi)容
????pState->SetPaneText?(1,?"My?New?Status?Bar?Text",?TRUE);
//????((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);
????CView::OnMouseMove(nFlags,?point);
}注意的幾點是:1,添加字符串資源IDS_MOUSE到MainFrm.cpp源代碼中的indicators數(shù)組中。位置在第二個。
????????????????????????? 2.添加響應(yīng)消息OnMouseMove函數(shù),其中如上代碼。
????????????????????????? 3.要添加對于的頭文件#include "MainFrm.h"文件。
????????????????????????? 4.將CMainFrame中成員變?yōu)閜ublic,否則的話訪問權(quán)限有誤!
(一個疑問就是:為何我的狀態(tài)欄很窄,顯示出來的信息就只要前面的X=xx,而例子中的代碼卻可以使得狀態(tài)很寬,顯示的夠多呢?應(yīng)該是其他方面的設(shè)置問題,待弄清!)
修改后的代碼如下(使得可以正確的顯示出來,長度合適):
void?CTest23View::OnMouseMove(UINT?nFlags,?CPoint?point)?


{
????//?TODO:?Add?your?message?handler?code?here?and/or?call?default
????CString?str;
????CMainFrame?*pMainFrame?=?(CMainFrame?*)AfxGetApp()->m_pMainWnd;????//獲取主窗口的指針
????CStatusBar?*pState?=?&pMainFrame->m_wndStatusBar;????
????str.Format("X=%d,Y=%d",point.x,point.y);????//文本格式化
????CClientDC?dc(this);
????CSize?sz?=?dc.GetTextExtent(str);????//用這個得到要顯示的字符串的長度
????pState->SetPaneInfo(1,IDS_MOUSE,SBPS_NORMAL,sz.cx);
????pState->SetPaneText(1,str,TRUE);????????//更新狀態(tài)欄第二項內(nèi)容
//????((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);
????CView::OnMouseMove(nFlags,?point);
}

上面就是用到了CClientDC的一個函數(shù)特征,得到長度和寬度。然后調(diào)用SetPaneInfo函數(shù)設(shè)置寬度。
參考:
http://www.shnenglu.com/deercoder/archive/2010/02/17/107985.html
posted on 2010-02-17 21:30
deercoder 閱讀(1060)
評論(0) 編輯 收藏 引用