積累的VC編程小技巧之工具條和狀態條

1.工具條和狀態條中控件的添加:

方法⑴.只能在ToolBar裏建立控件:首先,在ToolBar中建立一個Button,其ID爲ID_TOOL_COMBO(咱們要將建立的控件放在該Button的位置上).app

其次,新建立一個類CMainToolBar,要從CToolBar繼承(建立過程大概以下:選擇工程/增長到工程/新的類;也能夠選擇工程的根,而後點擊右鍵,選擇新的類;或者CTL+W,選擇增長類/新的類 --- 而後在class type裏選擇Generic Class,在Name欄裏輸入新類的名字,Base class裏輸入CToolBar[u1]),建立成功後在該類裏建立要增長的控件的對象,如:ide

CComboBox        m_wndMyCombo;函數

CStatic          m_wndCategory, m_wndCategoryPath;工具

CButton          m_wndOpenButton;this

Cedit                  m_wndEdit;spa

而後在構造函數裏初始化如:指針

m_wndMyCombo.m_hWnd = NULL;code

m_wndCategory.m_hWnd = NULL;orm

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函數裏實現:

CRect rc;

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;

              m_wndStatusBar.SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),

SWP_NOZORDER);

       }

最後在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中的建立與此相同,只需更改一下句柄便可

 

2.使工具條上的按鈕點擊一次爲按下,再點擊才彈起

bCheck=m_RtfEditToolBar.GetToolBarCtrl().IsButtonChecked(ID_TB_BOLD);

m_RtfEditToolBar.GetToolBarCtrl().CheckButton(ID_TB_BOLD, !bCheck);

 

3.如何隱藏工具欄

添加以下兩個函數
隱藏:
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);
    SendMessage(WM_SIZE);
}

 

4.如何動態獲取工具條指針並給工具條加標題?

[問題提出]

工具條也是窗口,是窗口就有標題,如何給工具條加標題?
[程序實現]
不想動態改變工具條的標題就在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);

 

5.在狀態條中顯示鼠標的設備座標與邏輯座標

顯示器的設備座標系的原點在客戶區的左上角,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);
}

 

6.如何更新狀態條上的現實內容

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:void CMainFrame::OnUpdatePage(CCmdUI *pCmdUI){    pCmdUI->Enable();    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.

相關文章
相關標籤/搜索