• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

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

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

            方法⑴.只能在ToolBar里創建控件:首先,在ToolBar中創建一個Button,其IDID_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文件中將StatusBarIDOnViewStatusBar 函數綁定在一起,如下所示: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.hpublic這個數據成員
            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.

            posted on 2008-04-02 18:54 wrh 閱讀(737) 評論(0)  編輯 收藏 引用

            導航

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統計

            常用鏈接

            留言簿(19)

            隨筆檔案

            文章檔案

            收藏夾

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            精品多毛少妇人妻AV免费久久| 亚洲AV无码久久精品色欲| 亚洲精品tv久久久久久久久| 久久夜色精品国产| 伊人久久大香线蕉综合5g| 大香网伊人久久综合网2020| 久久亚洲中文字幕精品一区| 日本一区精品久久久久影院| 久久91精品国产91| 7777久久亚洲中文字幕| 亚洲国产成人精品久久久国产成人一区二区三区综 | 伊人久久综合无码成人网| 久久精品国产亚洲AV香蕉| 香蕉aa三级久久毛片| 国产精品久久自在自线观看| 93精91精品国产综合久久香蕉| 东京热TOKYO综合久久精品| 秋霞久久国产精品电影院| 久久精品国产亚洲5555| 无码AV中文字幕久久专区| 国产成人久久777777| 亚洲av成人无码久久精品| 无码任你躁久久久久久久| 久久亚洲高清观看| 久久久久久国产精品无码超碰| 久久精品99无色码中文字幕| 99久久99这里只有免费费精品| 婷婷久久五月天| 精品久久久久久久久免费影院| 成人亚洲欧美久久久久 | 情人伊人久久综合亚洲| 久久天天躁狠狠躁夜夜网站 | 精品国产日韩久久亚洲 | 五月丁香综合激情六月久久| 久久综合久久鬼色| 午夜视频久久久久一区 | 亚洲av日韩精品久久久久久a | 久久久精品视频免费观看| 久久精品一区二区三区中文字幕| 伊人色综合久久天天| 久久―日本道色综合久久|