<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

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

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

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

    方法⑴.只能在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函數里實現:

    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.

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全