• <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編程小技巧之工具條和狀態(tài)條

            1.工具條和狀態(tài)條中控件的添加:

            方法⑴.只能在ToolBar里創(chuàng)建控件:首先,在ToolBar中創(chuàng)建一個Button,其IDID_TOOL_COMBO(我們要將創(chuàng)建的控件放在該Button的位置上).

            其次,新創(chuàng)建一個類CMainToolBar,要從CToolBar繼續(xù)(創(chuàng)建過程大概如下:選擇工程/增加到工程/新的類;也可以選擇工程的根,然后點擊右鍵,選擇新的類;或者CTL W選擇增加類/新的類 --- 然后在class type里選擇Generic Class,在Name欄里輸入新類的名字,Base class里輸入CToolBar[u1] ),創(chuàng)建成功后在該類里創(chuàng)建要增加的控件的對象,如:

            CComboBox m_wndMyCombo;

            CStatic m_wndCategory, m_wndCategoryPath;

            CButton m_wndOpenButton;

            Cedit m_wndEdit;

            然后在構(gòu)造函數(shù)里初始化如:

            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的頭文件里創(chuàng)建CMainToolBar的一個對象m_wndToolBar,最后在.cpp文件的OnCreate函數(shù)的最后實現(xiàn)如下:

            int index = 0;

            CRect rect; // 可定義在頭文件當中

            // ComboBox

            {

            //找到指定的工具項

            while(m_wndToolBar.GetItemID(index)!=ID_TOOL_COMBO)

            index ;

            //設(shè)置指定工具項的寬度并獲取新的區(qū)域 120是寬度

            m_wndToolBar.SetButtonInfo(index, ID_TOOL_COMBO, TBBS_SEPARATOR, 120);

            m_wndToolBar.GetItemRect(index, &rect);

            //設(shè)置位置

            rect.top =1;

            rect.bottom = 200;

            // 創(chuàng)建并顯示控件

            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);

            //填充內(nèi)容

            m_wndToolBar.m_wndMyCombo.AddString("25%");

            m_wndToolBar.m_wndMyCombo.AddString("50%");

            m_wndToolBar.m_wndMyCombo.AddString("75%");

            //選擇默認項

            m_wndToolBar.m_wndMyCombo.SetCurSel(0);

            //獲取到內(nèi)容并MSGBOX顯示出來

            CString strContent;

            m_wndToolBar.m_wndMyCombo.GetWindowText(strContent);

            index = 0;

            }

            其他控件都類似創(chuàng)建(只需要注重一下各自的Create函數(shù)的參數(shù)即可)

            方法⑵.這種方法創(chuàng)建不太輕易控制:直接在CMainframe的頭文件中創(chuàng)建要增加的控件的對象,如CButton 的對象m_wndAboutButton,然后創(chuàng)建CToolBar或者CstatusBar的對象,如:CstatusBar的對象_wndStatusBar;再增加幾個函數(shù)如下:

            Protected:

            virtual void RecalcLayout(BOOL bNotify = TRUE);

            afx_msg void CMainFrame::OnViewStatusBar();

            接著在.cpp文件中將StatusBarIDOnViewStatusBar 函數(shù)綁定在一起,如下所示: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函數(shù)的最后(返回值之前)實現(xiàn)如下代碼:

            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函數(shù)里實現(xiàn):

            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()里實現(xiàn):

            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中的創(chuàng)建與此相同,只需更改一下句柄即可

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

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

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

            3.如何隱藏工具欄

            添加如下兩個函數(shù)
            隱藏:
            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.如何動態(tài)獲取工具條指針并給工具條加標題?

            [問題提出]

            工具條也是窗口,是窗口就有標題,如何給工具條加標題?
            [程序?qū)崿F(xiàn)]
            不想動態(tài)改變工具條的標題就在CMainFrame::OnCreate():
            int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
            {
            ......
            m_wndToolBar.SetWindowText(_T("Standdard"));

            return 0;
            }
            若想動態(tài)改變工具條的標題,如下:
            聲明一個菜單,并響應(yīng)事件,如響應(yīng):OnMyToolBar()函數(shù)

            void CMainFrame::OnMyToolBar()
            {
            // TODO: Add your command handler code here
            CToolBar *pToolBar = (CToolBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);
            pToolBar->SetWindowText (_T("Standdard"));
            }
            不要在TooBar懸浮時做OnMyToolBar()會出錯的.
            順便提一下如何獲得狀態(tài)條的指針:
            CStatusBar * pStatusBar =(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);

            5.在狀態(tài)條中顯示鼠標的設(shè)備坐標與邏輯坐標

            顯示器的設(shè)備坐標系的原點在客戶區(qū)的左上角,x軸向右增長,y軸向下增長。我們要設(shè)置的邏輯坐標系的原點則在客戶區(qū)的中心,x軸向右增長,y軸向上增長,如一個笛卡爾坐標系一般。

            CChildView添加一個成員函數(shù)void OnPrepareDC(CDC * pDC, CPrintInfo * pInfo = NULL);

            void OnPrepareDC(CDC * pDC, CPrintInfo * pInfo){
            CRect rect;

            // 設(shè)置映射模式為LOMETRIC (0.1mm),右上為增長方向
            pDC->SetMapMode (MM_LOMETRIC);

            // 將坐標原點定在客戶區(qū)的中心
            GetClientRect(rect);
            pDC->SetViewportOrg(rect.Width()/2, rect.Height()/2);
            }
            CChildView響應(yīng)鼠標移動消息,并在狀態(tài)條中顯示鼠標的坐標值。m_ptMouse數(shù)據(jù)成員是原打算做十字交叉線用的,在此使用沒有實際意義。

            void CChildView::OnMouseMove(UINT nFlags, CPoint point){
            CClientDC dc(this);
            CString str;

            OnPrepareDC(&dc);

            //要訪問類CMainFrame,需要將mainfrm.h文件引入
            CMainFrame * pFrame = (CMainFrame *) AfxGetApp()->m_pMainWnd;

            //要訪問CMainFrame的數(shù)據(jù)成員m_wndStatusBar,需要手工修改mainfrm.hpublic這個數(shù)據(jù)成員
            CStatusBar * pStatus = (CStatusBar *) &pFrame->m_wndStatusBar;

            m_ptMouse = point;
            str.Format ("設(shè)備坐標 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.如何更新狀態(tài)條上的現(xiàn)實內(nèi)容

            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 閱讀(745) 評論(0)  編輯 收藏 引用

            導航

            <2008年4月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            統(tǒng)計

            常用鏈接

            留言簿(19)

            隨筆檔案

            文章檔案

            收藏夾

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            人妻无码中文久久久久专区| 亚洲日本va午夜中文字幕久久 | 久久精品青青草原伊人| 青青草国产精品久久| 久久无码人妻一区二区三区午夜 | 国产精品亚洲综合专区片高清久久久| 精品久久久久久国产| 中文字幕久久久久人妻| 99国产精品久久久久久久成人热| 久久人人爽爽爽人久久久| 91久久精品91久久性色| 久久国产精品-久久精品| 久久婷婷色综合一区二区| 久久综合五月丁香久久激情| 国内精品久久久久影院薰衣草| 久久综合九色综合网站| 久久久国产精品福利免费| 综合久久精品色| 亚洲国产成人久久精品动漫| 亚洲欧洲久久av| 青青热久久综合网伊人| 久久综合九色综合网站| 久久精品免费一区二区| 亚洲精品无码专区久久久| 久久国产成人精品国产成人亚洲| 精品乱码久久久久久久| 思思久久99热只有频精品66| 久久免费香蕉视频| 久久精品国产WWW456C0M| 久久久久免费视频| 国内精品久久久久久久亚洲| 99久久无码一区人妻| 97精品伊人久久久大香线蕉| 久久综合给久久狠狠97色| 久久乐国产综合亚洲精品| 99久久香蕉国产线看观香| 久久久女人与动物群交毛片| 久久精品二区| 99久久99久久久精品齐齐 | 久久99这里只有精品国产| 久久99久久99精品免视看动漫 |