Windows Form Bai 4

download Windows Form Bai 4

of 86

Transcript of Windows Form Bai 4

  • 7/29/2019 Windows Form Bai 4

    1/86

    i Hc S Phm Tp. H Ch MinhKhoa Ton Tin Hc LP TRNH WINDOWS

    Toolbar - MenubarDialogBar

    To v xl Toolbar

    To v xl Menubar

    Trn Ngc Bo

    Email: [email protected]

    To v xl Dialogbar

    mailto:[email protected]:[email protected]
  • 7/29/2019 Windows Form Bai 4

    2/86

    i Hc S Phm Tp. H Ch MinhKhoa Ton Tin Hc LP TRNH WINDOWS

    Lp trnh vi Toolbar

    Toolbar - MenubarDialogBar

  • 7/29/2019 Windows Form Bai 4

    3/86

    Tran Ngoc Bao 3 Dai hoc Su Pham TP.HCM

    NI DUNG

    Gii thiu v Tool bar v Dialog bar

    Thm combo box vo Tool bar

    Thm Flyby v ToolTip

    To v x l tool bar

    Radio & Check Box buttons trn tool bar

    Dialog barResizable dialog bar

  • 7/29/2019 Windows Form Bai 4

    4/86

    Tran Ngoc Bao 4 Dai hoc Su Pham TP.HCM

    Tool bar Dialog barTool bar

    Tool bar

    Tool bar

    Tool bar

    Tree

    control

    Dialog bar

    Tool bar

  • 7/29/2019 Windows Form Bai 4

    5/86

    Tran Ngoc Bao 5 Dai hoc Su Pham TP.HCM

    Tool bar Dialog barTool bar (Docked to Top)

    Tool bar

    (Floated)

    Tree control

    Dialog bar

    (docked to Left)

    Tool bar (Docked to Left)

    Tool bar (Docked to bottom)

    Tool Bar

    (Docked To Right)

  • 7/29/2019 Windows Form Bai 4

    6/86Tran Ngoc Bao 6 Dai hoc Su Pham TP.HCM

    Cc thnh phn trn Tool barBitmap button

    Separator

    Tool bar Caption

    Close buttonRadio button

    Combo box

    Push button

    Radio button

    Check box button

    Check box button

    Separator

    Separator

  • 7/29/2019 Windows Form Bai 4

    7/86Tran Ngoc Bao 7 Dai hoc Su Pham TP.HCM

    To v x l Tool bar

    To mt ng dng c Tool bar mc nh bngAppWizard

    To v x l Tool bar bng lp CToolBar

  • 7/29/2019 Windows Form Bai 4

    8/86Tran Ngoc Bao 8 Dai hoc Su Pham TP.HCM

    Tool bar - To ng dng bng AppWizard

    Chn menu File New

    Chn Tab Projects

    Chn loi projectMFC AppWizard (exe)

    t tn project trong Project name

    Xc nh ng dn thmc trongLocation

    Step 1: Chn loi ng dngSingle Document, b

    option Document/View architecture support NhnNextnStep 4 of 6

  • 7/29/2019 Windows Form Bai 4

    9/86Tran Ngoc Bao 9 Dai hoc Su Pham TP.HCM

    Tool bar - To ng dng bng AppWizard

    Chn optionDocking toolbarAppWizard tng tora tool bar mc nh

  • 7/29/2019 Windows Form Bai 4

    10/86Tran Ngoc Bao 10 Dai hoc Su Pham TP.HCM

    Tool bar - To ng dng bng AppWizard

    Kt qu chy chng trnh to tool bar bng AppWizard

    Tool bar

    mcnh

  • 7/29/2019 Windows Form Bai 4

    11/86Tran Ngoc Bao 11 Dai hoc Su Pham TP.HCM

    Khai bo bin CToolBar

    Tool bar - Cc xl trong lp CMainFrame

    class CMainFrame : public CFrameWnd

    {

    .

    protected: //khai bo bin x l default tool barCToolBar m_wndToolBar;

    .

    }

    Cc x l trong hm OnCreate Bc 1: Gi hm to lp ca s tool barCToolBar::Create()

    Bc 2: Load resource tool barCToolBar::LoadToolBar() Bc 3: Thit lp thuc tnh tool barCToolBar::SetBarStyle()

    Bc 4: Gi hm CToolBar::EnableDocking()

    Bc 5: Gi hm CToolBar::EnableDocking() gn tool bar

  • 7/29/2019 Windows Form Bai 4

    12/86Tran Ngoc Bao 12 Dai hoc Su Pham TP.HCM

    Tool bar - Cc xl trong hm OnCreate

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){

    ...

    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE |

    CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY |CBRS_SIZE_DYNAMIC) ||

    !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))

    {

    TRACE0("Failed to create toolbar\n");

    return -1; // fail to create

    }

    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

    EnableDocking(CBRS_ALIGN_ANY);

    DockControlBar(&m_wndToolBar);

    ..

    }

  • 7/29/2019 Windows Form Bai 4

    13/86Tran Ngoc Bao 13 Dai hoc Su Pham TP.HCM

    Thm Tool bar vo ng dng

    Thm mi Tool bar resource

    Khai bo bin CToolBar To tool bar

    Thit lp thuc tnh tool bar

    Gn tool bar

    X l message

  • 7/29/2019 Windows Form Bai 4

    14/86Tran Ngoc Bao 14 Dai hoc Su Pham TP.HCM

    Thm mi tool bar resource

    Tool bar resource

  • 7/29/2019 Windows Form Bai 4

    15/86Tran Ngoc Bao 15 Dai hoc Su Pham TP.HCM

    Thm mi tool bar resource (tt)

    Chn mc Insert Toolbarthm mi tool barresource vo ng dng

  • 7/29/2019 Windows Form Bai 4

    16/86Tran Ngoc Bao 16 Dai hoc Su Pham TP.HCM

    Thm mi tool bar resource (tt)

    New tool bar

    resource

  • 7/29/2019 Windows Form Bai 4

    17/86Tran Ngoc Bao 17 Dai hoc Su Pham TP.HCM

    Thm mi tool bar resource (tt)

  • 7/29/2019 Windows Form Bai 4

    18/86Tran Ngoc Bao 18 Dai hoc Su Pham TP.HCM

    Thm mi tool bar resource (tt)

    Resource editor

  • 7/29/2019 Windows Form Bai 4

    19/86Tran Ngoc Bao 19 Dai hoc Su Pham TP.HCM

    Thm mi tool bar resource (tt)

    ID Tool barBitmap

    button

    Separator

    Current item

  • 7/29/2019 Windows Form Bai 4

    20/86Tran Ngoc Bao 20 Dai hoc Su Pham TP.HCM

    Khai bo bin qun l tool bar

    class CMainFrame : public CFrameWnd

    {

    protected: // control bar embedded members

    CStatusBar m_wndStatusBar;

    CToolBar m_wndToolBar;

    CToolBar m_wndDrawBox;

    };

  • 7/29/2019 Windows Form Bai 4

    21/86Tran Ngoc Bao 21 Dai hoc Su Pham TP.HCM

    To & xc lp thuc tnh cho Toolbar

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){

    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)

    return -1;

    // To Toolbarif (!m_wndDrawBox.CreateEx(this) ||

    !m_wndDrawBox.LoadToolBar(IDR_DRAW_TOOLBAR))

    {

    return -1;}

    // Xc lp thuc tnh Toolbar

    m_wndDrawBox.SetBarStyle(m_wndDrawBox.GetBarStyle()|CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

    }

  • 7/29/2019 Windows Form Bai 4

    22/86Tran Ngoc Bao 22 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo ca s chnh

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    .

    // To Toolbar

    // Xc lp thuc tnh Toolbar

    // Gn Toolbar vo ca s chnh

    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);

    DockControlBar(&m_wndToolBar);

    }

  • 7/29/2019 Windows Form Bai 4

    23/86

    Tran Ngoc Bao 23 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn trn ca s chnh

    Toolbar gn bn trn

  • 7/29/2019 Windows Form Bai 4

    24/86

    Tran Ngoc Bao 24 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn trn ca s chnh

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    .// To Toolbar

    // Xc lp thuc tnh Toolbar

    // Gn Toolbar vo ca s chnh

    m_wndDrawBox.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_TOP);

    DockControlBar(&m_wndDrawBox,AFX_IDW_DOCKBAR_TOP);

    }

  • 7/29/2019 Windows Form Bai 4

    25/86

    Tran Ngoc Bao 25 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn tri ca s chnh

    Toolbar gn bn tri

  • 7/29/2019 Windows Form Bai 4

    26/86

    Tran Ngoc Bao 26 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn tri ca s chnh

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    .// To Toolbar

    // Xc lp thuc tnh Toolbar

    // Gn Toolbar vo ca s chnh

    m_wndDrawBox.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_LEFT);

    DockControlBar(&m_wndDrawBox,AFX_IDW_DOCKBAR_LEFT);

    }

  • 7/29/2019 Windows Form Bai 4

    27/86

    Tran Ngoc Bao 27 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn di ca s chnh

    Toolbar gn bn di

  • 7/29/2019 Windows Form Bai 4

    28/86

    Tran Ngoc Bao 28 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn di ca s chnh

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    .// To Toolbar

    // Xc lp thuc tnh Toolbar

    // Gn Toolbar vo ca s chnhm_wndDrawBox.EnableDocking(CBRS_ALIGN_ANY);

    EnableDocking(CBRS_ALIGN_BOTTOM);

    DockControlBar(&m_wndDrawBox,AFX_IDW_DOCKBAR_BOTTOM);

    }

  • 7/29/2019 Windows Form Bai 4

    29/86

    Tran Ngoc Bao 29 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn phi ca s chnh

    Toolbar gn bn phi

  • 7/29/2019 Windows Form Bai 4

    30/86

    Tran Ngoc Bao 30 Dai hoc Su Pham TP.HCM

    Gn Toolbar vo bn di ca s chnh

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    .

    // To Toolbar

    // Xc lp thuc tnh Toolbar

    // Gn Toolbar vo ca s chnh

    m_wndDrawBox.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_RIGHT);

    DockControlBar(&m_wndDrawBox,AFX_IDW_DOCKBAR_RIGHT);

    }

  • 7/29/2019 Windows Form Bai 4

    31/86

    Tran Ngoc Bao 31 Dai hoc Su Pham TP.HCM

    Hin th ToolBar N ct

    ToolBar 2 ct

  • 7/29/2019 Windows Form Bai 4

    32/86

    Tran Ngoc Bao 32 Dai hoc Su Pham TP.HCM

    Hin th ToolBar N ctvoid CMainFrame::SetColumns(int nCols,CToolBar tbToolBox)

    { int nColumns = nCols;

    int nCount = tbToolBox.GetCount();

    for(int i = 0; i < nCount; i++)

    {UINT nStyle = tbToolBox.GetButtonStyle(i);

    BOOL bWrap = (((i + 1) % nColumns) == 0);

    if (bWrap)

    nStyle |= TBBS_WRAPPED;

    else

    nStyle &= ~TBBS_WRAPPED;

    tbToolBox.SetButtonStyle(i, nStyle);

    }

    RecalcLayout();

    Invalidate();

    }

  • 7/29/2019 Windows Form Bai 4

    33/86

    Tran Ngoc Bao 33 Dai hoc Su Pham TP.HCM

    Xl skin click ln cc button trong tool bar

    Chn button cn x l Chn COMMAND

    Chn Add Function

  • 7/29/2019 Windows Form Bai 4

    34/86

    Tran Ngoc Bao 34 Dai hoc Su Pham TP.HCM

    Xl skin click ln cc button trong tool bar//tp tin *.h

    class CAddNewTooBarView : public CView

    {

    afx_msg void OnButtonBold();

    };//tp tin *.cpp

    BEGIN_MESSAGE_MAP(CAddNewTooBarView, CView)

    //{{AFX_MSG_MAP(CAddNewTooBarView)

    ON_COMMAND(ID_BUTTON_BOLD, OnButtonBold)//}}AFX_MSG_MAP

    END_MESSAGE_MAP()

    void CAddNewTooBarView::OnButtonBold()

    {

    MessageBox(Nhan nut B (Bold");

    }

  • 7/29/2019 Windows Form Bai 4

    35/86

    Tran Ngoc Bao 35 Dai hoc Su Pham TP.HCM

    Xl button dng CheckBox v Radio trn Toolbar

    Toolbar dng Radio

    Toolbar dng CheckBox

  • 7/29/2019 Windows Form Bai 4

    36/86

    Tran Ngoc Bao 36 Dai hoc Su Pham TP.HCM

    class CAddNewTooBarView : public CView{

    //

    public:

    BOOL m_bBold; //Lu tr trng thi button B

    BOOL m_bItalic;//Lu tr trng thi button I

    BOOL m_bUnderline;//Lu tr trng thi button U

    };

    Xl CheckBox button (tt)

    CAddNewTooBarView ::CAddNewTooBarView

    {

    m_bBold = TRUE; //Trng thi button B ban u c Check

    m_bItalic=FALSE;//Trng thi button I ban u khng c Checkm_bUnderline = FALSE;//Trng thi button U ban u khng c Check

    };

  • 7/29/2019 Windows Form Bai 4

    37/86

    Tran Ngoc Bao 37 Dai hoc Su Pham TP.HCM

    Xl CheckBox button (tt)void CAddNewTooBarView::OnButtonBold()

    {

    m_bBold = !m_bBold;

    }

    void CAddNewTooBarView::OnUpdateButtonBold(CCmdUI* pCmdUI){

    pCmdUI->SetCheck(m_bBold );

    }

    void CAddNewTooBarView::OnButtonItalic(){

    m_bItalic = !m_bItalic;

    }

    void CAddNewTooBarView::OnUpdateButtonItalic(CCmdUI* pCmdUI)

    {

    pCmdUI->SetCheck(m_bItalic);

    }

    i ( )

  • 7/29/2019 Windows Form Bai 4

    38/86

    Tran Ngoc Bao 38 Dai hoc Su Pham TP.HCM

    class CAddNewTooBarView : public CView{

    //

    public:

    int m_nToolSelected; //Lu tr trng thi button ang chn

    };

    Xl Radio button (tt)

    CAddNewTooBarView ::CAddNewTooBarView

    {

    m_nToolSelected = -1; //Trng thi ban u cha c button no cchn

    };

  • 7/29/2019 Windows Form Bai 4

    39/86

    Tran Ngoc Bao 39 Dai hoc Su Pham TP.HCM

    Xl Radio button (tt)

    void CAddNewTooBarView::OnButtonLine()

    {

    m_nToolSelected = 1;

    }

    void CAddNewTooBarView::OnUpdateButtonLine(CCmdUI* pCmdUI)

    {

    if (m_nToolSelected == 1)pCmdUI->SetCheck(1);

    else

    pCmdUI->SetCheck(0);}

  • 7/29/2019 Windows Form Bai 4

    40/86

    Tran Ngoc Bao 40 Dai hoc Su Pham TP.HCM

    Xl Radio button (tt)

    void CAddNewTooBarView::OnButtonCircle()

    {

    m_nToolSelected = 2;

    }

    void CAddNewTooBarView::OnUpdateButtonItalic(CCmdUI* pCmdUI)

    {pCmdUI->SetCheck(m_nToolSelected == 2);

    }

    E bl /Di bl b tt (tt)

  • 7/29/2019 Windows Form Bai 4

    41/86

    Tran Ngoc Bao 41 Dai hoc Su Pham TP.HCM

    Enable/Disable button (tt)

    Nu chn vng thng hoc ng trn rng

    th khng cho php chn kiu tDisable cc button chn kiu t

    Nu chn vng trn cth cho php chn kiu t

    Enable cc button chn kiu t

  • 7/29/2019 Windows Form Bai 4

    42/86

    Tran Ngoc Bao 42 Dai hoc Su Pham TP.HCM

    Enable/Disable button (tt)

    void CAddNewTooBarView::OnUpdateButtonFillStyle(CCmdUI* pCmdUI){

    switch (m_nToolSelected)

    {

    case 1: //Vng thng

    pCmdUI->Enable(0);

    break;

    case 2: //Vng trn cpCmdUI->Enable(1);

    break;

    }

    }

  • 7/29/2019 Windows Form Bai 4

    43/86

    Tran Ngoc Bao 43 Dai hoc Su Pham TP.HCM

    Gn ComboBox vo Toolbar

    ComboBox gn vo Toolbar

  • 7/29/2019 Windows Form Bai 4

    44/86

    Tran Ngoc Bao 44 Dai hoc Su Pham TP.HCM

    Khai bo bin qun l ComboBox

    class CMainFrame : public CFrameWnd{

    protected:// control bar embedded members

    CStatusBar m_wndStatusBar;CToolBar m_wndToolBar;

    CToolBar m_wndDrawBox;

    CComboBox m_wndFontCombox;

    };

    G C b B T lb ( )

  • 7/29/2019 Windows Form Bai 4

    45/86

    Tran Ngoc Bao 45 Dai hoc Su Pham TP.HCM

    Gn ComboBox vo Toolbar (tt)int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    m_wndDrawBox.SetButtonInfo(0,ID_BUTTON_RECT,TBBS_SEPARATOR,150);

    CRect rect;

    m_wndDrawBox.GetItemRect(0,rect);

    rect.bottom = rect.top+150;

    if (!m_wndFontCombox.Create(WS_CHILD

    |CBS_DROPDOWN|CBS_AUTOHSCROLL|WS_VSCROLL|CBS_HASSTRINGS,

    rect,&m_wndDrawBox,ID_BUTTON_RECT))

    {return -1;

    }

    m_wndFontCombox.AddString("Time New Roman");

    m_wndFontCombox.AddString("Tahoma");m_wndFontCombox.SetCurSel(1);

    m_wndFontCombox.ShowWindow(SW_SHOW);

    }

    X l d b tt t T lb (tt)

  • 7/29/2019 Windows Form Bai 4

    46/86

    Tran Ngoc Bao 46 Dai hoc Su Pham TP.HCM

    class CAddNewTooBarView : public CView

    {

    .

    //Khai boafx_msg void OnButtons(UINT uID);

    afx_msg void OnUpdateButtons(CCmdUI* pCmdUI);

    };//Message Map

    ON_COMMAND_RANGE(ID_BUTTON_RED,ID_BUTTON_YELLOW,

    OnButtons)

    ON_UPDATE_COMMAND_UI_RANGE(ID_BUTTON_RED,ID_BUTTO

    N_YELLOW,OnUpdateButtons)

    Xl dy buttons trn Toolbar (tt)

    X l d b tt t T lb (tt)

  • 7/29/2019 Windows Form Bai 4

    47/86

    Tran Ngoc Bao 47 Dai hoc Su Pham TP.HCM

    void CAddNewTooBarView::OnUpdateButtons(CCmdUI* pCmdUI)

    {

    pCmdUI->SetRadio(pCmdUI->m_nID==m_uColor);

    }

    void CAddNewTooBarView::OnButtons(UINT uID)

    {

    CString s;

    if (uID == ID_BUTTON_YELLOW)

    s = "Yellow";if (uID == ID_BUTTON_RED)

    s = "Red";

    if (uID == ID_BUTTON_BLUE)

    s = "Blue";MessageBox(s);

    m_uColor = uID;

    }

    Xl dy buttons trn Toolbar (tt)

    B ( /hi ) T lb

  • 7/29/2019 Windows Form Bai 4

    48/86

    Tran Ngoc Bao 48 Dai hoc Su Pham TP.HCM

    Bt tt (n/hin) Toolbar

    void CMainFrame::OnViewDrawBox()

    {

    BOOL bShow;m_wndDrawBox.IsWindowVisible()?FALSE: TRUE;

    m_wndDrawBox.ShowWindow(bShow);

    }

    i Hc S Phm Tp. H Ch MinhKh T Ti H

    LP TRNH WINDOWS

  • 7/29/2019 Windows Form Bai 4

    49/86

    Khoa Ton Tin Hc LP TRNH WINDOWS

    Lp trnh vi Menubar

    Toolbar - MenubarDialogBar

    NI DUNG

  • 7/29/2019 Windows Form Bai 4

    50/86

    Tran Ngoc Bao 50 Dai hoc Su Pham TP.HCM

    NI DUNG

    Gii thiu v Menu

    Gn Menu vo dialog

    Right click popup menu (Context Menu)

    To v x l Menu

    X l thay i Menu trong ng dng

    Thm v hy Menu Bitmap Item

    Gii thi M

  • 7/29/2019 Windows Form Bai 4

    51/86

    Tran Ngoc Bao 51 Dai hoc Su Pham TP.HCM

    Gii thiu MenuMenu Bar

    Context menu

    C th h h t M b

  • 7/29/2019 Windows Form Bai 4

    52/86

    Tran Ngoc Bao 52 Dai hoc Su Pham TP.HCM

    Cc thnh phn trn Menu bar

    Popup Menu

    Popup Menu

    Menu Bar

    Menu Item

    Separator

    Popup Menu

    Popup Menu

    T l M b

  • 7/29/2019 Windows Form Bai 4

    53/86

    Tran Ngoc Bao 53 Dai hoc Su Pham TP.HCM

    To v xl Menu bar

    To mt ng dng c Menubar mc nh bngAppWizard

    To v x l Menu bar bng lp CMenu

    M b T d b A Wi d

  • 7/29/2019 Windows Form Bai 4

    54/86

    Tran Ngoc Bao 54 Dai hoc Su Pham TP.HCM

    Menubar - To ng dng bng AppWizard

    Chn menu File New

    Chn Tab Projects

    Chn loi projectMFC AppWizard (exe)

    t tn project trong Project name

    Xc nh ng dn thmc trongLocation

    Step 1: Chn loi ng dngMultiple Document, b

    option Document/View architecture support NhnNextnStep 4 of 6

    Menubar To ng dng bng AppWizard

  • 7/29/2019 Windows Form Bai 4

    55/86

    Tran Ngoc Bao 55 Dai hoc Su Pham TP.HCM

    Menubar - To ng dng bng AppWizard

    Kt qu chy chng trnh to menu bar bng AppWizard

    Menubar

    mcnh

    (Child Menu)

    Menubar To ng dng bng AppWizard

  • 7/29/2019 Windows Form Bai 4

    56/86

    Tran Ngoc Bao 56 Dai hoc Su Pham TP.HCM

    Menubar - To ng dng bng AppWizard

    Kt qu chy chng trnh to menu bar bng AppWizard

    Menubar

    mcnh(Main Menu)

    Cc x l trong lp CxxxApp

  • 7/29/2019 Windows Form Bai 4

    57/86

    Tran Ngoc Bao 57 Dai hoc Su Pham TP.HCM

    Cc xl trong lp CxxxAppBOOL CMyFirstMenuApp::InitInstance()

    {

    AfxEnableControlContainer();

    .

    CSingleDocTemplate* pDocTemplate;

    pDocTemplate = new CSingleDocTemplate(

    IDR_MENU_TOOL,//Menu o cho nay

    RUNTIME_CLASS(CMyFirstMenuDoc),

    RUNTIME_CLASS(CMainFrame), // main SDI frame window

    RUNTIME_CLASS(CMyFirstMenuView));

    AddDocTemplate(pDocTemplate);

    // Parse command line for standard shell commands, DDE, file open

    m_pMainWnd->ShowWindow(SW_SHOW);

    m_pMainWnd->UpdateWindow();

    return TRUE;

    }

    Th i M

  • 7/29/2019 Windows Form Bai 4

    58/86

    Tran Ngoc Bao 58 Dai hoc Su Pham TP.HCM

    Thm mi Menu resource

    Menu resource

    Chn mc Insert Menu thm mi Menu resourcevo ng dng

    Th i M (tt)

  • 7/29/2019 Windows Form Bai 4

    59/86

    Tran Ngoc Bao 59 Dai hoc Su Pham TP.HCM

    Thm mi Menu resource (tt)

    New Menu

    resource

    Th i M (tt)

  • 7/29/2019 Windows Form Bai 4

    60/86

    Tran Ngoc Bao 60 Dai hoc Su Pham TP.HCM

    Thm mi Menu resource (tt)

    Thm mi Men reso rce (tt)

  • 7/29/2019 Windows Form Bai 4

    61/86

    Tran Ngoc Bao 61 Dai hoc Su Pham TP.HCM

    Thm mi Menu resource (tt)

    Resource editor

    Thm mi Menu resource (tt)

  • 7/29/2019 Windows Form Bai 4

    62/86

    Tran Ngoc Bao 62 Dai hoc Su Pham TP.HCM

    Thm mi Menu resource (tt)

    ID Menu

    Popup

    Separator

    Menu Item

    X l s kin click ln cc button trong Menu

  • 7/29/2019 Windows Form Bai 4

    63/86

    Tran Ngoc Bao 63 Dai hoc Su Pham TP.HCM

    Xl skin click ln cc button trong Menu

    Thng ip WM_COMMAND

    Thng ip UPDATE_COMMAND_UI

    Bt/tt MenuItem (Enable/Disable)

    Thay i Caption ca MenuItem

    Checking MenuItem

    X l s kin click ln cc button trong Menu

  • 7/29/2019 Windows Form Bai 4

    64/86

    Tran Ngoc Bao 64 Dai hoc Su Pham TP.HCM

    Xl skin click ln cc button trong Menu

    Chn Item cn x l Chn COMMAND

    Chn Add Function

    X l s kin click ln cc button trong tool bar

  • 7/29/2019 Windows Form Bai 4

    65/86

    Tran Ngoc Bao 65 Dai hoc Su Pham TP.HCM

    Xl skin click ln cc button trong tool bar

    class CAddNewTooBarView : public CView

    {

    afx_msg void OnButtonBold();

    };BEGIN_MESSAGE_MAP(CAddNewTooBarView, CView)

    //{{AFX_MSG_MAP(CAddNewTooBarView)

    ON_COMMAND(ID_BUTTON_BOLD, OnButtonBold)

    //}}AFX_MSG_MAP

    END_MESSAGE_MAP()

    void CAddNewTooBarView::OnButtonBold()

    {MessageBox(Nhan nut B (Bold");

    }

    X l Item dng CheckBox v Radio trn Menu

  • 7/29/2019 Windows Form Bai 4

    66/86

    Tran Ngoc Bao 66 Dai hoc Su Pham TP.HCM

    Xl Item dng CheckBox v Radio trn Menu

    X l tng t nh button dngcheckbox v Radio button trong

    Toolbar Gn ID ca MenuItem c cng ID vi

    cc Item trong Toolbar Click lnMenuItem v Item trn Toolbar s thchin cng mt chc nng trong

    chng trnh

    X l CheckBox button (tt)

  • 7/29/2019 Windows Form Bai 4

    67/86

    Tran Ngoc Bao 67 Dai hoc Su Pham TP.HCM

    class CAddNewTooBarView : public CView

    {

    //

    public:

    BOOL m_bBold; //Lu tr trng thi button BBOOL m_bItalic;//Lu tr trng thi button I

    BOOL m_bUnderline;//Lu tr trng thi button U

    };

    Xl CheckBox button (tt)

    CAddNewTooBarView ::CAddNewTooBarView

    {

    m_bBold = TRUE; //Trng thi button B ban u c Check

    m_bItalic=FALSE;//Trng thi button I ban u khng c Checkm_bUnderline = FALSE;//Trng thi button U ban u khng cCheck

    };

    X l CheckBox button (tt)

  • 7/29/2019 Windows Form Bai 4

    68/86

    Tran Ngoc Bao 68 Dai hoc Su Pham TP.HCM

    Xl CheckBox button (tt)void CAddNewTooBarView::OnButtonBold()

    {

    m_bBold = !m_bBold;

    }

    void CAddNewTooBarView::OnUpdateButtonBold(CCmdUI* pCmdUI)

    {

    pCmdUI->SetCheck(m_bBold );

    }

    void CAddNewTooBarView::OnButtonItalic(){

    m_bItalic = !m_bItalic;

    }

    void CAddNewTooBarView::OnUpdateButtonItalic(CCmdUI* pCmdUI)

    {

    pCmdUI->SetCheck(m_bItalic);

    }

    X l Radio button (tt)

  • 7/29/2019 Windows Form Bai 4

    69/86

    Tran Ngoc Bao 69 Dai hoc Su Pham TP.HCM

    class CAddNewTooBarView : public CView

    {

    //

    public:int m_nToolSelected; //Lu tr trng thi button ang chn

    };

    Xl Radio button (tt)

    CAddNewTooBarView ::CAddNewTooBarView

    {

    m_nToolSelected = -1; //Trng thi ban u cha c button no cchn

    };

    Thay i Menu trong ng dng

  • 7/29/2019 Windows Form Bai 4

    70/86

    Tran Ngoc Bao 70 Dai hoc Su Pham TP.HCM

    Thay i Menu trong ng dng

    Yu cuChng trnh cho php ngi dng chn hin th menuTing Anh/Ting Vit nhhnh

    Menu ting Anh Menu ti ng Vit

    Thay i Menu trong ng dng (tt)

  • 7/29/2019 Windows Form Bai 4

    71/86

    Tran Ngoc Bao 71 Dai hoc Su Pham TP.HCM

    void CMyFirstMenuView::OnUpdateToolsCircle(CCmdUI*

    pCmdUI)

    {

    CString s;

    if (m_LanguageSel==VN)s.LoadString(ID_TOOLS_CIRCLE_VN);

    else

    s.LoadString(ID_TOOLS_CIRCLE);pCmdUI->SetText(s);

    }

    Thay i Menu trong ng dng (tt)

    Cch 1:Thay i thuc tnh Text trong mi MenuItem trong Menu

    Thay i Menu trong ng dng (tt)

  • 7/29/2019 Windows Form Bai 4

    72/86

    Tran Ngoc Bao 72 Dai hoc Su Pham TP.HCM

    void CMyFirstMenuView::OnUpdateToolsLine(CCmdUI*pCmdUI)

    {

    CString s;

    if (m_LanguageSel==VN)

    s.LoadString(ID_TOOLS_LINE_VN);

    else

    s.LoadString(ID_TOOLS_LINE);pCmdUI->SetText(s);

    }

    Thay i Menu trong ng dng (tt)

    Cch 1:

    Thay i thuc tnh Text trong mi MenuItem trong Menu

    Thay i Menu trong ng dng (tt)

  • 7/29/2019 Windows Form Bai 4

    73/86

    Tran Ngoc Bao 73 Dai hoc Su Pham TP.HCM

    Thay i Menu trong ng dng (tt)

    Cch 2:

    Thit k 2 Menu trong Menu Resource c cng

    cu trc: cng s Item v cc Item trong 2Menu resource i 1 c cng ID

    Menu ting Anh Menu ting Vit

    Thay i Menu trong ng dng (tt)

  • 7/29/2019 Windows Form Bai 4

    74/86

    Tran Ngoc Bao 74 Dai hoc Su Pham TP.HCM

    Thay i Menu trong ng dng (tt)void CMainFrame::OnEnglish()

    {

    // TODO: Add your command handler code here

    m_myMenu.DestroyMenu();

    m_NN =1;NewMyMenu();

    }

    void CMainFrame::OnVietnamese()

    {

    // TODO: Add your command handler code here

    m_myMenu.DestroyMenu();

    m_NN =2;NewMyMenu();

    }

    Thay i Menu trong ng dng (tt)

  • 7/29/2019 Windows Form Bai 4

    75/86

    Tran Ngoc Bao 75 Dai hoc Su Pham TP.HCM

    Thay i Menu trong ng dng (tt)

    void CMainFrame::NewMyMenu()

    {

    if (m_NN==1)

    {m_myMenu.LoadMenu(IDR_MENU_EN);

    }

    elsem_myMenu.LoadMenu(IDR_MENU_VN);

    SetMenu(&m_myMenu);

    }

    Gn menu vo dialog

  • 7/29/2019 Windows Form Bai 4

    76/86

    Tran Ngoc Bao 76 Dai hoc Su Pham TP.HCM

    Gn menu vo dialog

    i.To menu resourceii.To dialog reourceiii.Gn menu vo dialog

    -Chn dialog resource trong ResourceView-Click phi chut chn Properties

    -Trong Tab General, chn Menu ID tng ng trongcombo Menu

    Context Menu - Right Click Popup Menu

  • 7/29/2019 Windows Form Bai 4

    77/86

    Tran Ngoc Bao 77 Dai hoc Su Pham TP.HCM

    Context Menu Right Click Popup Menu

    Thm Resource MenuThc hin tng t nh thm menu bnh thng

    Ci t Right Click menu

    Nhn chut phi ln vng Client hin th popup menu

    void CMyFirstMenuView::OnRButtonDown(UINT nFlags, CPointpoint)

    {

    CMenu menu;

    menu.LoadMenu(IDR_MENU1);

    ClientToScreen(&point);//Hin th popup ti v tr click chut

    menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this,NULL);

    }

    Xc lp trng thi ca Item trong Popup Menu

  • 7/29/2019 Windows Form Bai 4

    78/86

    Tran Ngoc Bao 78 Dai hoc Su Pham TP.HCM

    Xc lp trng thi ca Item trong Popup Menuvoid CMyFirstMenuView::OnRButtonDown(UINT nFlags, CPoint point)

    {// TODO: Add your message handler code here and/or call default

    CMenu menu;

    CMenu *ptrMenu;

    //Load Menumenu.LoadMenu(IDR_MENU1);

    ptrMenu = menu.GetSubMenu(0);

    //Xac lap trang thai cua cac Item

    ptrMenu->EnableMenuItem(ID_POPUP_POPUPITEM01,MF_GRAYED);

    ptrMenu-EnableMenuItem(ID_POPUP_POPUPITEM02,MF_ENABLED);

    ptrMenu-EnableMenuItem(ID_POPUP_POPUPITEM03,MF_CHECKED);

    //Di chuyen vao dung vi tri cua con chuot

    ClientToScreen(&point);

    //Hien thi menuptrMenu->TrackPopupMenu(

    TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this,NULL);

    CView::OnRButtonDown(nFlags, point);

    }

    Thm v Hu MenuItem

  • 7/29/2019 Windows Form Bai 4

    79/86

    Tran Ngoc Bao 79 Dai hoc Su Pham TP.HCM

    Thm v Hu MenuItemC php

    InsertMenu(nPosition, nFlags, NewID, Caption)

    MF_BYPOSITION: da vo v tr

    MF_BYCOMMAND: da vo ID, SEPARATOR khng c ID

    Separator:nFlags = MF_SEPARATOR

    NewID = ANYCaption = ANY

    MenuItemnFlags = None

    NewID = NEWID

    Caption

    Thm MenuItem

  • 7/29/2019 Windows Form Bai 4

    80/86

    Tran Ngoc Bao 80 Dai hoc Su Pham TP.HCM

    Thm MenuItem

    void CMyFirstMenuView::OnPopupInsert()

    {

    // TODO: Add your command handler code here

    CMenu *topMenu = AfxGetMainWnd()->GetMenu();

    CMenu *ptrMenu = topMenu->GetSubMenu(0);

    UINT nNewID = ID_USER_MENUITEM+m_nItem;

    m_nItem = m_nItem+1;ptrMenu->InsertMenu(1,MF_BYPOSITION,nNewID,"&Menu

    Caption");

    AfxGetMainWnd()->DrawMenuBar();}

    Thm PopupMenu

  • 7/29/2019 Windows Form Bai 4

    81/86

    Tran Ngoc Bao 81 Dai hoc Su Pham TP.HCM

    Thm PopupMenu

    void CMyFirstMenuView::OnPopupInsert()

    {

    // TODO: Add your command handler code hereCMenu *topMenu = AfxGetMainWnd()->GetMenu();

    CMenu *ptrMenu = topMenu->GetSubMenu(0);

    //Handle ca Sub Menu(0) c lu vo *ptrMenu

    topMenu->InsertMenu(1,MF_BYPOSITION,(UINT)ptrMenu->GetSafeHmenu(),"&Menu Caption");

    AfxGetMainWnd()->DrawMenuBar();}

    Xa/Hy menuItem

  • 7/29/2019 Windows Form Bai 4

    82/86

    Tran Ngoc Bao 82 Dai hoc Su Pham TP.HCM

    Xa/Hy menuItem

    void CMyFirstMenuView::OnPopupDelete()

    {

    CMenu *topMenu = AfxGetMainWnd()->GetMenu();

    topMenu->RemoveMenu(1,MF_BYPOSITION);

    AfxGetMainWnd()->DrawMenuBar();

    }

    Remove 1 Popup trong MainMenu

    Xa/Hy menuItem

  • 7/29/2019 Windows Form Bai 4

    83/86

    Tran Ngoc Bao 83 Dai hoc Su Pham TP.HCM

    y

    void CMyMenuView::OnLButtonDown(UINT nFlags, CPoint point){

    CMenu *topMenu = AfxGetMainWnd()->GetMenu();

    CMenu *ptrMenu = topMenu->GetSubMenu(0);

    ptrMenu->RemoveMenu(1,MF_BYPOSITION);

    AfxGetMainWnd()->DrawMenuBar();

    CView::OnLButtonDown(nFlags, point);

    }

    Remove 1 Item trong Popup th nht trong MainMenu

    i Hc S Phm Tp. H Ch MinhKhoa Ton Tin Hc LP TRNH WINDOWS

  • 7/29/2019 Windows Form Bai 4

    84/86

    Lp trnh vi Dialogbar

    Toolbar - MenubarDialogBar

    To v xl Dialog Bar

  • 7/29/2019 Windows Form Bai 4

    85/86

    Tran Ngoc Bao 85 Dai hoc Su Pham TP.HCM

    gclass CMainFrame : public CFrameWnd

    {

    .

    CDialogBar m_wndDialogBar;

    };int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    if (!m_wndDialogBar.Create(this,IDD_DIALOG_BAR,CBRS_TOOLTIPS |

    CBRS_FLYBY,IDD_DIALOG_BAR)){

    TRACE0("Failed to create toolbar\n");

    return -1; // fail to create

    }m_wndDialogBar.EnableDocking(CBRS_ALIGN_ANY);

    EnableDocking(CBRS_ALIGN_TOP);

    DockControlBar(&m_wndDialogBar,AFX_IDW_DOCKBAR_TOP);

    }

  • 7/29/2019 Windows Form Bai 4

    86/86