方法⑴.隻能在ToolBar裡建立控件:首先,在ToolBar中建立一個Button,其ID為ID_TOOL_COMBO(我們要将建立的控件放在該Button的位置上).
其次,新建立一個類CMainToolBar,要從CToolBar繼承(建立過程大概如下:選擇工程/增加到工程/新的類;也可以選擇工程的根,然後點選右鍵,選擇新的類;或者CTL+W,選擇增加類/新的類 --- 然後在class type裡選擇Generic Class,在Name欄裡輸入新類的名字,Base class裡輸入CToolBar[u1]),建立成功後在該類裡建立要增加的控件的對象,如:
CComboBox m_wndMyCombo;
CStatic m_wndCategory, m_wndCategoryPath;
CButton m_wndOpenButton;
Cedit m_wndEdit;
然後在構造函數裡初始化如:
m_wndMyCombo.m_hWnd = NULL;
m_wndCategory.m_hWnd = NULL;
m_wndCategoryPath.m_hWnd = NULL;
m_wndOpenButton.m_hWnd = NULL;
m_wndEdit.m_hWnd = NULL;
接着在CMainframe的頭檔案裡建立CMainToolBar的一個對象m_wndToolBar,最後在.cpp檔案的OnCreate函數的最後實作如下:
int index = 0;
CRect rect; // 可定義在頭檔案當中
// ComboBox
{
//找到指定的工具項
while(m_wndToolBar.GetItemID(index)!=ID_TOOL_COMBO)
index++;
//設定指定工具項的寬度并擷取新的區域 120是寬度
m_wndToolBar.SetButtonInfo(index, ID_TOOL_COMBO, TBBS_SEPARATOR, 120);
m_wndToolBar.GetItemRect(index, &rect);
//設定位置
rect.top+=1;
rect.bottom += 200;
// 建立并顯示控件
if(!m_wndToolBar.m_wndMyCombo.Create(WS_CHILD|WS_VISIBLE| CBS_AUTOHSCROLL|
CBS_DROPDOWNLIST | CBS_HASSTRINGS , rect, &m_wndToolBar, ID_TOOL_COMBO))
{
TRACE0("Failed to create combo-box\n");
return FALSE;
}
m_wndToolBar.m_wndMyCombo.ShowWindow(SW_SHOW);
//填充内容
m_wndToolBar.m_wndMyCombo.AddString("25%");
m_wndToolBar.m_wndMyCombo.AddString("50%");
m_wndToolBar.m_wndMyCombo.AddString("75%");
//選擇預設項
m_wndToolBar.m_wndMyCombo.SetCurSel(0);
//擷取到内容并MSGBOX顯示出來
CString strContent;
m_wndToolBar.m_wndMyCombo.GetWindowText(strContent);
index = 0;
}
其他控件都類似建立(隻需要注意一下各自的Create函數的參數即可)。
方法⑵.這種方法建立不太容易控制:直接在CMainframe的頭檔案中建立要增加的控件的對象,如CButton 的對象m_wndAboutButton,然後建立CToolBar或者CstatusBar的對象,如:CstatusBar的對象_wndStatusBar;再增加幾個函數如下:
Protected:
virtual void RecalcLayout(BOOL bNotify = TRUE);
afx_msg void CMainFrame::OnViewStatusBar();
接着在.cpp檔案中将StatusBar的ID和OnViewStatusBar 函數綁定在一起,如下所示:BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
// {{AFX_MSG_MAP(CMainFrame)
ON_COMMAND(ID_VIEW_STATUS_BAR, OnViewStatusBar)
ON_WM_CREATE()
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
然後Create函數的最後(傳回值之前)實作如下代碼:
CRect rc;
VERIFY(m_wndAboutButton.Create(_T("MyAbout"),
WS_VISIBLE,rc,this,ID_APP_ABOUT));
// TODO: Remove this if you don't want tool tips or a resizeable toolbar
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
再在RecalcLayout函數裡實作:
if (m_wndStatusBar.m_hWnd)
{
m_wndStatusBar.GetWindowRect(&rc);
ScreenToClient(&rc);
rc.right -= 50;
m_wndStatusBar.SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),
SWP_NOZORDER);
rc.left = rc.right;
rc.right += 50;
最後在OnViewStatusBar()裡實作:
BOOL bShow = m_wndStatusBar.GetStyle() & WS_VISIBLE;
m_wndAboutButton.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
(bShow ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
ToolBar中的建立與此相同,隻需更改一下句柄即可
bCheck=m_RtfEditToolBar.GetToolBarCtrl().IsButtonChecked(ID_TB_BOLD);
m_RtfEditToolBar.GetToolBarCtrl().CheckButton(ID_TB_BOLD, !bCheck);
添加如下兩個函數
隐藏:
void CMainFrame::OnHide()
if(m_wndToolBar.IsWindowVisible())
m_wndToolBar.ModifyStyle(WS_VISIBLE,0);
SendMessage(WM_SIZE);
}
顯示:
void CMainFrame::OnShow()
if(!m_wndToolBar.IsWindowVisible())
m_wndToolBar.ModifyStyle(0,WS_VISIBLE);
[問題提出]
工具條也是視窗,是視窗就有标題,如何給工具條加标題?
[程式實作]
不想動态改變工具條的标題就在CMainFrame::OnCreate()中:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
......
m_wndToolBar.SetWindowText(_T("Standdard"));
return 0;
}
若想動态改變工具條的标題,如下:
聲明一個菜單,并響應事件,如響應:OnMyToolBar()函數
void CMainFrame::OnMyToolBar()
{
// TODO: Add your command handler code here
CToolBar *pToolBar = (CToolBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);
pToolBar->SetWindowText (_T("Standdard"));
不要在TooBar懸浮時做OnMyToolBar()會出錯的.
順便提一下如何獲得狀态條的指針:
CStatusBar * pStatusBar =(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);
顯示器的裝置坐标系的原點在客戶區的左上角,x軸向右增長,y軸向下增長。我們要設定的邏輯坐标系的原點則在客戶區的中心,x軸向右增長,y軸向上增長,如一個笛卡爾坐标系一般。
為CChildView添加一個成員函數void OnPrepareDC(CDC * pDC, CPrintInfo * pInfo = NULL);
void OnPrepareDC(CDC * pDC, CPrintInfo * pInfo){
CRect rect;
// 設定映射模式為LOMETRIC (0.1mm),右上為增長方向
pDC->SetMapMode (MM_LOMETRIC);
// 将坐标原點定在客戶區的中心
GetClientRect(rect);
pDC->SetViewportOrg(rect.Width()/2, rect.Height()/2);
為CChildView響應滑鼠移動消息,并在狀态條中顯示滑鼠的坐标值。m_ptMouse資料成員是原打算做十字交叉線用的,在此使用沒有實際意義。
void CChildView::OnMouseMove(UINT nFlags, CPoint point){
CClientDC dc(this);
CString str;
OnPrepareDC(&dc);
//要通路類CMainFrame,需要将mainfrm.h檔案引入
CMainFrame * pFrame = (CMainFrame *) AfxGetApp()->m_pMainWnd;
//要通路CMainFrame的資料成員m_wndStatusBar,需要手工修改mainfrm.h,public這個資料成員
CStatusBar * pStatus = (CStatusBar *) &pFrame->m_wndStatusBar;
m_ptMouse = point;
str.Format ("裝置坐标 X=%i pixel, Y=%i pixel", m_ptMouse.x, m_ptMouse.y);
pStatus->SetPaneText(1, str);
dc.DPtoLP(&m_ptMouse);
str.Format ("邏輯坐标 X=%i * 0.1mm, Y=%i * 0.1mm", m_ptMouse.x, m_ptMouse.y);
pStatus->SetPaneText(2, str);
By default, a CStatusBar pane is not enabled when the pane is created. To activate a pane, you must call the ON_UPDATE_COMMAND_UI() macro for each pane on the status bar and update the panes. Because panes do not send WM_COMMAND messages, you cannot use ClassWizard to activate panes; you must type the code manually. For example, suppose one pane has ID_INDICATOR_PAGE as its identifier and that it contains the current page number in a document. To make the ID_INDICATOR_PAGE pane display text, add the following to a header file (probably the MAINFRM.H file):
afx_msg void OnUpdatePage(CCmdUI *pCmdUI);
Add the following to the application message map:
ON_UPDATE_COMMAND_UI(ID_INDICATOR_PAGE, OnUpdatePage)
Add the following to a source code file (probably MAINFRM.CPP):
void CMainFrame::OnUpdatePage(CCmdUI *pCmdUI)
pCmdUI->Enable();
To display text in the panes, either call SetPaneText() or call CCmdUI::SetText() in the OnUpdate() function. For example, you might want to set up an integer variable m_nPage that contains the current page number. Then, the OnUpdatePage() function might read as follows:
char szPage[16];
wsprintf((LPSTR)szPage, "Page %d", m_nPage);
pCmdUI->SetText((LPSTR)szPage);
This technique causes the page number to appear in the pane during idle processing in the same manner that the application updates other indicators.