This presentation is the property of its rightful owner.
Sponsored Links
1 / 75

第六章 菜单、工具栏和状态栏 PowerPoint PPT Presentation


  • 134 Views
  • Uploaded on
  • Presentation posted in: General

第六章 菜单、工具栏和状态栏. 本章导读 掌握菜单的创建步骤和方法,如何进行命令消息的映射和编写相应的程序代码。 掌握 CMenu 类及其常用成员函数的功能和使用方法,掌握利用这些成员函数创建快捷菜单和实现动态增加或减少菜单项。 掌握工具栏的创建方法和步骤,如何将工具栏的功能与菜单的功能相对应。 掌握创建状态栏的方法和步骤。 学习和掌握 CMenu 类、 CStatusBar 类、 CToolBar 类、 CCmdUI 类及常用成员函数的功能和使用。 菜单、工具栏和状态栏是 Windows 应用程序中最重要的三个用户界面元素,是应用程序设计中不可缺少的步骤。.

Download Presentation

第六章 菜单、工具栏和状态栏

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


4416849

  • CMenu

  • CMenuCStatusBarCToolBarCCmdUI

  • Windows


4416849

6.1

  • Windows

  • Windows

  • 1.

  • IDIDIDID_FILE_OPEN


4416849

  • Visual C++ 6.0FileOpen WorkspaceDialog.dsw

  • Resource ViewDialog ResourcesMenu,IDR_MAINFRAME

  • IDR_MAINFRAME

  • Properties


4416849

  • Caption&P&PPAlt+PEnter

  • P

  • PropertiesCaption&D


4416849

  • Visual C++ 6.0IDIDIDMENUITEMxxxxx(xxxxx)Prompt

  • General

  • Windows


4416849

6.1 General


4416849

  • 2.

  • Windowsshortcut key

  • Resource ViewAcceleratorIDR_MAINFRAME

  • Ctrl+DCtrl+DAccel Properties


4416849

  • Accel Properties6.2

  • 6.2 Accel Properties


4416849

  • IDID_DISPLAY_DATAIDIDIDID_DISPLAY_DATA=32771

  • KeyDModifiersCtrlAltShiftNext Key TypedCtrl+DCtrl+DEnter

  • 6.1.1Caption(D)\tCtrl+D


4416849

  • 3

  • MFC

  • MFCIDMFCClassWizardMFC


4416849

  • COMMANDUPDATE_COMMAND_UI

  • 1COMMAND

  • 2UPDATE_COMMAND_UI

  • ClassWizardWM_COMMAND

  • &D


4416849

  • ViewClassWizardMFC ClassWizard

  • MFC ClassWizardMessage MapsClass nameCDialogView Object Ids ID_DISPLAY_DATA Messages COMMAND UPDATE_COMMAND_UI COMMAND

  • Add Function Add Member functionOnDisplayDataOK

  • Edit CodeMFC ClassWizard OnDisplayData


4416849

  • Visual C++ClassWizard

  • 1. CDialogViewDialogView.h

  • // Generated message map functions

  • protected:

  • //{{AFX_MSG(CDialogView)

  • afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);

  • afx_msg void OnDisplayData(); //ClassWizard

  • //}}AFX_MSG

  • DECLARE_MESSAGE_MAP()

  • };


4416849

  • //{{AFX_MSG(CDialogView)

  • //}}AFX_MSG

  • ClassWizardAFX_MSG(CDialogView)CDialogView

  • 2. CDialogViewDialogView.cpp

  • BEGIN_MESSAGE_MAP(CDialogView, CView)

  • //{{AFX_MSG_MAP(CDialogView)


4416849

  • ON_WM_LBUTTONDBLCLK()

  • ON_COMMAND(ID_DISPLAY_DATA,

  • OnDisplayData)//ClassWizard

  • //}}AFX_MSG_MAP

  • END_MESSAGE_MAP()

  • //{{AFX_MSG_MAP(CDialogView)

  • //}}AFX_MSG_MAP

  • ClassWizard


4416849

  • 3. DialogView.cppOnDisplayData

  • DialogView.cpp OnLButtonDBLCLK()OnDisplayData()

  • void CDialogView::OnDisplayData()

  • {// TODO: Add your command handler code here

  • CMydataDialog dlg;

  • dlg.m_radio_m=0;

  • dlg.m_edit_name="";

  • CString msg;

  • if(dlg.DoModal()==IDOK)

  • {msg="<>";}

  • Else {msg="<>";}


4416849

  • msg+="\r\n";

  • msg+="";

  • msg+=dlg.m_edit_name;

  • msg+="\r\n";

  • msg+="";

  • switch(dlg.m_radio_m)

  • {case 0:msg+="";

  • break;

  • case 1:msg+="";

  • break; }

  • msg+="\r\n";

  • msg+="";

  • msg+=dlg.m_combo_school;

  • msg+="\r\n";

  • msg+="";


4416849

  • if(dlg.m_check_art==TRUE)

  • msg+=" ";

  • if(dlg.m_check_music==TRUE)

  • msg+=" ";

  • if(dlg.m_check_sport==TRUE)

  • msg+=" ";

  • AfxMessageBox(msg);

  • }

  • Ctrl+D


4416849

  • 4. CMenu

  • Visual C++MFCCmenuWindows

  • CMenuCreateMenuCreatePopupMenu

  • BOOL CreateMenu(); //

  • BOOL CreatePopupMenu(); //


4416849

  • CMenuLoadMenu

  • BOOL LoadMenu(LPCTSTR lpszResourceName);

  • BOOL LoadMenu(UNIT nIDResource);

  • lpszResourceNamenIDResourceID

  • CMenuAppendMenuInsertMenuAppendMenuInsertMenu


4416849

  • BOOL AppendMenu(UNIT nFlags, UNIT nIDNewItem,const CBitmap * pBmp);

  • BOOL AppendMenu(UNIT nFlags, UNIT nIDNewItem=0,LPCTSTR lpszNewItem=NULL);

  • BOOL InsertMenu(UNIT nPosition, UNIT nFlags, UNIT nIDNewItem,const CBitmap *pBmp);

  • BOOL InsertMenu(UNIT nPosition, UNIT nFlags, UNIT nIDNewItem=0,LPCTSTR lpszNewItem=NULL);

  • nIDNewItemIDlpszNewItempBmpnPositionnFlags6.3


4416849

  • 1nFlagsMF_BYPOSITIONnPosition01-1

  • 2nFlags|MF_CHECKED|MF_STRINGMF_DISABLEDMF_ENABLEDMF_GRAYED

  • 3CWndCWnd::DrawMenuBarDrawMenuBar

  • void DrawMenuBar();


4416849

  • CMenuDeleteMenu

  • BOOL DeleteMenu(UNIT nPosition,UNIT nFlags);

  • nPositionnFlagsnFlagsMF_BYCOMMANDnPositionIDnFlagsMF_BYPOSITIONnPosition01

  • DeleteMenuCWndCWnd::DrawMenuBar


4416849

  • CMenuID

  • 1GetMenuItemCount

  • UNIT GetMenuItemCount() const;

  • -1

  • 2GetMenuItemID

  • UNIT GetMenuItemID(int nPos) const;

  • nPosIDnPos-1


4416849

  • 3GetSubMenu

  • CMenu *GetSubMenu(int nPos) const;

  • nPos0

  • CMainFrameCMainFrame::GetMenu

  • CMenu *GetMenu();


4416849

  • 5.

  • MFCCMenuTrackPopupMenu

  • BOOL TrackPopupMenu(UNIT nFlags,int x,int y,CWnd *pWnd,LPCRECT lpRect=0);

  • 1nFlags6.4|


4416849

  • 2xy

  • 3pWndWM_COMMAND

  • 4lpRectRECTCRectlpRectNULL

  • Visual C++ 6.0FileOpen WorkspaceDialog.dsw


4416849

  • InsertResourceCtrl+RInsert ResourceMenuNew

  • Resource ViewDialog ResourcesMenuIDR_MENU1IDIDR_ MENU1PropertiesMenu Item PropertiesCaption


4416849

  • PropertiesMenu Item PropertiesCaption&DIDID_MENU1_DATAMenu Item Properties&OID__

  • ViewClassWizard(Create a new class)Select an existing classSelect an existing classSelect ClassCDialogViewSelectMFC ClassWizard


4416849

  • MFC ClassWizardMessage MapsClass nameCDialogView Object Ids ID_MENU1_DATA Messages COMMAND

  • Add Function Add Member functionOnMenu1DataOK

  • Edit CodeMFC ClassWizard OnMenu1DataOnDisplayData()OnMenu1Data()


4416849

  • void CDialogView::OnMenu1Data()

  • {

  • // TODO: Add your command handler code here

  • CMydataDialog dlg;

  • dlg.m_radio_m=0;

  • dlg.m_edit_name="";

  • CString msg;

  • if(dlg.DoModal()==IDOK)

  • {msg="<>";}

  • else

  • {msg="<>";}


4416849

  • msg+="\r\n\n";

  • msg+=" ";

  • msg+=dlg.m_edit_name;

  • msg+="\r\n";

  • msg+=" ";

  • switch(dlg.m_radio_m)

  • {case 0:msg+="";

  • break;

  • case 1:msg+="";

  • break;}

  • msg+="\r\n";

  • msg+="";

  • msg+=dlg.m_combo_school;

  • msg+="\r\n";

  • msg+=" ";


4416849

  • if(dlg.m_check_art==TRUE)

  • msg+=" ";

  • if(dlg.m_check_music==TRUE)

  • msg+=" ";

  • if(dlg.m_check_sport==TRUE)

  • msg+=" ";

  • AfxMessageBox(msg);

  • }

  • ViewClassWizard.MFC ClassWizardMessage MapsClass nameCMainFrame Object IdsCMainFrame Messages WM_CONTEXTMENUAdd Function


4416849

  • Add Member functionOnContextMenuOKEdit CodeMFC ClassWizard OnContextMenu

  • void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point)

  • {// TODO: Add your message handler code here

  • CMenu menu;

  • menu.LoadMenu(IDR_MENU1);

  • menu.GetSubMenu(0)

  • ->TrackPopupMenu(TPM_LEFTALIGN|TPM_

  • RIGHTBUTTON,point.x,point.y,this);

  • }


4416849

  • 6.

  • Visual C++ 6.0FileOpen WorkspaceDialog.dsw

  • ViewResource SymbolsResource View


4416849

  • New New SymbolNameIDID_NEW_MENUITEMValueIDID10115~61440OK

  • Visual C++CMainFrameMainFrm.cppCMainFrame::OnCreate

  • int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

  • {//


4416849

  • CMenu *pMainMenu=GetMenu();

  • CMenu *pSubMenu=pMainMenu->GetSubMenu (4);

  • CString StrOfNewMenuItem("");

  • pSubMenu->AppendMenu (MF_SEPARATOR);

  • pSubMenu->AppendMenu

  • (MF_STRING,ID_NEW_MENUITEM,StrOfNewMenuItem);

  • m_bAutoMenuEnable=FALSE;

  • pMainMenu->EnableMenuItem

  • (ID_NEW_MENUITEM,MF_BYCOMMAND|MF_ENABLED);

  • DrawMenuBar();

  • return 0;

  • }


4416849

  • ViewClassWizardMFC ClassWizardMessage MapsClass nameCMainFrame Object IdsCMainFrame Messages OnCommandEdit Code CMainFrame::OnCommand

  • BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)

  • {// TODO: Add your specialized code here and/or call the base class

  • if (LOWORD(wParam)==ID_NEW_MENUITEM)

  • MessageBox("");

  • return CFrameWnd::OnCommand(wParam, lParam); }


4416849

6.2

  • 1 .

  • Dialog

  • Resource ViewDialog ResourcesAppWizardDialog ResourcesToolbar


4416849

  • InsertResourceInsertInsert ResourceToolbarNew

  • Dialog ResounrcesToolbarToolbarIDR_TOOLBAR1

  • Resource ViewToolbarIDR_TOOLBAR1PropertiesIDIDIDR_MYTOOLBAR


4416849

ID

Width

Height

Prompt

ID

  • GraphicColorR

  • Toolbar Button Properties6.5


4416849

  • IDID

  • 1

  • 2


4416849

  • 3

  • Ctrl

  • 4

  • 5


4416849

  • Class ViewCmainFrame,Add Member VariableAdd Member Variable

  • Variable TypeCToolBarVariable Namem_MyDataToolBarOK

  • Visual C++MainFrm.hCToolBar m_MyDataToolBar;


4416849

  • ViewClassWizardCtrl+RMFC ClassWizardClass nameCmainFrameObject IdsCMainFrameMessagesWM_CREATEWM_CREATEAdd FunctionMember FunctionOnCreate()

  • Edit CodeClassWizardMainFrm.cppOnCreate()


4416849

  • Int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

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

  • return -1;

  • // TODO: Add your specialized creation code here

  • if (!m_MyDataToolBar.Create(this)||

  • !m_MyDataToolBar.LoadToolBar(IDR_MYTOOLBAR))

  • { TRACE0("Failed to created toolbar\n");

  • return -1;

  • }

  • return 0;

  • }


4416849

  • 1CToolBarCToolBarCControlBarCreate

  • BOOL Create(Cwnd *pParentWnd,

  • DWORD wStyle=WS_CHILD|WS_VISIBLE|CBRS_TOP,

  • UNIT nID=AFX_IDW_TOOLBAR);

  • pParentWnddwStylenIDID


4416849

6.6


4416849

  • 2TRACE

  • TRACEprintfDumpDCTRACE512

  • int i = 1;

  • char sz[] = "one";

  • TRACE( "Integer = %d, String = %s\n", i, sz );

  • TRACE0~TRACE34~+3

  • TRACE0( exp )

  • TRACE1( exp, param1 )

  • TRACE2( exp, param1, param2 )

  • TRACE3( exp, param1, param2, param3 )


4416849

  • TRACE0( "Start Dump of MyClass members:" );

  • int i = 1;

  • TRACE1( "Integer = %d\n", i ); // Output: 'Integer = 1'

  • int j = 1;

  • char sz[] = "one";

  • TRACE2( "Integer = %d, String = %s\n",j, sz ); // Output: 'Integer = 1, String = one'

  • 2.


4416849

  • CControlBarEnableDocking()CFrameWndEnableDockingCFrameWndDockControlBar

  • OnCreate()

  • void CControlBar::EnableDocking( DWORD dwStyle );

  • void CFrameWnd::EnableDocking( DWORD dwDockStyle );

  • void CFrameWnd::DockControlBar( CControlBar * pBar, UINT nDockBarID = 0, LPCRECT lpRect = NULL );

  • dwStyledwDockStyle6.6pBar


4416849

  • int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

  • {

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

  • return -1;

  • // TODO: Add your specialized creation code here

  • if (!m_MyDataToolBar.Create(this)||

  • !m_MyDataToolBar.LoadToolBar(IDR_MYTOOLBAR))

  • { TRACE0("Failed to created toolbar\n");

  • return -1;

  • }


4416849

  • m_MyDataToolBar.EnableDocking(CBRS_ALIGN_ANY);

  • EnableDocking(CBRS_ALIGN_ANY);

  • DockControlBar(&m_MyDataToolBar);

  • return 0;

  • }


4416849

6.3

  • Caps LockNum Lock

  • 1 .

  • DialogNum Lock

  • ID

  • NewNew SymbolNameID_STATUSBAR_STR,ValueIDOK


4416849

  • IDResource SymbolsCloseResource SymbolsIDResource ViewString TableString TableNew String

  • String PropertiesIDID_STATUSBAR_STRCaptionEnter


4416849

  • 2.

  • Class ViewCMainFrameAdd Member VariableAdd Member Variable,Variable TypeCStatusBarVariable Namem_MyStatusBarOK

  • Visual C++MainFrm.h

  • CStatusBar m_MyStatusBar;

  • MainFrm.cppindicators


4416849

  • // CMainFrame

  • IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

  • BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

  • //{{AFX_MSG_MAP(CMainFrame)

  • ON_WM_CONTEXTMENU()

  • ON_WM_CREATE()

  • //}}AFX_MSG_MAP

  • END_MESSAGE_MAP()

  • static UINT indicators[]=

  • { ID_SEPARATOR,//

  • ID_STATUSBAR_STR,

  • ID_INDICATOR_NUM,

  • };


4416849

  • MainFrm.appOnCreate()

  • int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

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

  • return -1;

  • // TODO: Add your specialized creation code here

  • if (!m_MyDataToolBar.Create(this)||

  • !m_MyDataToolBar.LoadToolBar(IDR_MYTOOLBAR))

  • { TRACE0("Failed to created toolbar\n");

  • return -1;

  • }


4416849

  • m_MyDataToolBar.EnableDocking(CBRS_ALIGN_ANY);

  • EnableDocking(CBRS_ALIGN_ANY);

  • DockControlBar(&m_MyDataToolBar);

  • if(!m_MyStatusBar.Create(this)||

  • !m_MyStatusBar.SetIndicators(indicators,

  • sizeof(indicators)/sizeof(UINT)))

  • { TRACE0("Failed to created status bar \n");

  • return -1; //fail to create

  • }

  • return 0;

  • }


4416849

  • 1CStatusBarCStatusBarVisual C++MFC CStatusBarCControlBarCreate

  • BOOL Create( CWnd* pParentWnd,

  • DWORD dwStyle = WS_CHILD | WS_VISIBLE |CBRS_BOTTOM,

  • UINT nID = AFX_IDW_STATUS_BAR );

  • pParentWnddwStylenIDID6.7


4416849

  • 6.7 dwStyle

  • 2MainFrm.cppindicatorsstatic UINT indicators[]AppWizardSDIMDI4Initial status barMainFrm.cppindicatorsindicatorID_SEPARATORID_INDICATOR_CAPSID_INDICATOR_NUMID_INDICATOR_SCRL


4416849

  • 3

  • CWnd::SetWindowTextm_wndStatusBarm_wndStatusBar.SetWindowText()

  • ON_UPDATE_COMMAND_UICCmdUI::SetText

  • virtual void SetText( LPCTSTR lpszText );

  • lpszText


4416849

  • CStatusBar::SetPaneTextCStatusBar::GetPaneText

  • BOOL SetPaneText( int nIndex, LPCTSTR lpszNewText, BOOL bUpdate = TRUE );

  • CString GetPaneText( int nIndex ) const;

  • nIndex10lpszNewTextbUpdateTRUE

  • 4CStatusBar:: CommandToIndex ID

  • int CommandToIndex( UINT nIDFind ) const;

  • nIDFindID


4416849

  • 5CStatusBar::SetPaneInfoCStatusBar::GetPaneInfo

  • void SetPaneInfo( int nIndex, UINT nID, UINT nStyle, int cxWidth );

  • void GetPaneInfo( int nIndex, UINT& nID, UINT& nStyle, int& cxWidth ) const;

  • nIndexnIDIDcxWidthnStyleSBPS_POPOUT6.8


4416849

6.8


4416849

  • 6CStatusBar::SetPaneStyleCStatusBar::GetPaneStyle

  • UINT GetPaneStyle( int nIndex ) const;

  • void SetPaneStyle( int nIndex, UINT nStyle );

  • nIndexnStyle

  • 3.

  • !


4416849

  • ViewClassWizardMFC ClassWizardClass nameCMainFrameObject IDsID_APP_ABOUTMessagesCOMMANDUPDATE_COMMAND_UICOMMANDAdd Member Function

  • OnAppAboutOKMFC ClassWizardMFC ClassWizardMember functionsOnAppAbout

  • Edit CodeOnAppAboutMainFrm.cpp#include


4416849

  • // MainFrm.cpp : implementation of the CMainFrame class

  • //

  • #include "stdafx.h"

  • #include "Dialog.h"

  • #include "MainFrm.h"

  • #include "MydataDialog.h"

  • #ifdef _DEBUG

  • #define new DEBUG_NEW

  • #undef THIS_FILE

  • static char THIS_FILE[] = __FILE__;

  • #endif


4416849

  • OnAppAbout()

  • void CMainFrame::OnAppAbout()

  • {// TODO: Add your command handler code here

  • CMydataDialog dlg;

  • if(dlg.DoModal()==IDOK)

  • {

  • CClientDC dc(this);

  • SIZE size=dc.GetTextExtent("!");

  • int ndex=m_MyStatusBar.CommandToIndex(ID_STATUSBAR_STR);

  • m_MyStatusBar.SetPaneInfo(index,ID_STATUSBAR_STR,SBPS_POPOUT,size.cx);

  • m_MyStatusBar.SetPaneText(index,_T("!"),TRUE);

  • }

  • }


4416849

  • GetTextExtent()CommandToIndex()SetPaneInfo()SBPS_POPOUT

  • SetPaneText()


4416849

  • WM_COMMANDMFC

  • Visual C++MFCCmenuWindows

  • CMainFrameWM_CREATEMFCCToolBar


4416849

  • CreateCToolBarCControlBar

  • 0CMainFrameWM_CREATEMFCCStatusBarCreateCStatusBarCControlBarMainFrm.cppindicators


4416849

  • 6.1 AppWizard

  • 1

  • 2CMainFrameWM_COMMANDWM_COMMAND_UI

  • 3Ctrl+D


4416849

  • 4

  • 5XY

  • 6ClassWizardWM_MOUSEMOVEXY

  • 6.2 VCHelpCMenuCToolBarCStatusBarCCmdUI


  • Login