130 likes | 146 Views
CITA 342 Section 6. Message Maps, Menus, and Toolbars. Message Maps. MFC programs do not contain a main function or event loop. All of the event handling happens “behind the scenes” in C++ code that is part of the CWinApp class.
E N D
CITA 342 Section 6 Message Maps, Menus, and Toolbars
Message Maps • MFC programs do not contain a main function or event loop. All of the event handling happens “behind the scenes” in C++ code that is part of the CWinApp class. • Because the event handling is hidden, we need a way to tell the invisible event loop to notify us about events of interest to the application. This is done with a mechanism called a message map. • The message map identifies interesting events and then indicates functions to call in response to those events.
Message Map Macros • DECLARE_MESSAGE_MAP() • Makes the creation of a message map possible. • BEGIN_MESSAGE_MAP(theClass, baseClass) • Accepts two parameters. The first is the name of the specific class to which the message map applies. The second is the base class from which the specific class is derived. • Initializes the message map structure. • END_MESSAGE_MAP() • Closes the implementation of the message map.
Message Map Macros • ON_COMMAND(id, memberFxn) • ON_WM_XX() • Handles a Windows message and invokes the class member function that should be called for that message.
Message Map Example class CGreetingView : public CView { … // Generated message map functions protected: DECLARE_MESSAGE_MAP() public: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); // "afx_msg" indicates that the function is part // of the MFC library message dispatch system };
Message Map Example BEGIN_MESSAGE_MAP(CGreetingView, CView) … ON_WM_LBUTTONDOWN() END_MESSAGE_MAP()
Message Map Example void CGreetingView::OnLButtonDown(UINT nFlags, CPoint point) { MessageBox(_T("Hello, World!")); CView::OnLButtonDown(nFlags, point); }
Menus and Commands • In Visual C++, you use the Menu Editor to design and edit menus. • If you open Resource View and expand the Menu folder, you will see the IDR_MAINFRAME resource that MFC Application Wizard automatically creates for you. • By default, the menu resource includes File, Edit, View, and Help menus, with prewritten commands such as the Copy and Paste commands, on the Edit menu.
Menus and Commands • Menus and commands usually have an accelerator key associated with them. • An accelerator key is an underlined character in a menu or command caption that defines a keystroke sequence that you can use to open a menu or select a command without using the mouse.
Menus and Commands • For menus themselves, you open the menu by holding down the Alt key and pressing the underlined character on your keyboard. • To define an accelerator key in your menus and commands, you place an ampersand (&) in the caption before the letter you want to use as the accelerator key.
Toolbars and Buttons • Menus and toolbars are similar in that they both execute commands. • Instead of having text commands as menus do, however, toolbars execute commands using graphical icons known as buttons. • When you work with toolbars, you assign resource IDs to toolbar buttons, and then use COMMAND messages to map each button to a handler function.
Toolbars and Buttons • You design and edit toolbars in Visual C++ using the Toolbar Editor. • If you expand the Toolbar folder in Resource View, you will see the IDR_MAINFRAME resource. • This is the same resource ID used by the menu resource. • If you open the IDR_MAINFRAME resource in the Toolbar Editor, you will see several buttons already created for you, such as the Edit buttons.
Toolbars and Buttons • When you design a button, you color in the individual pixels that make up the button. • You can see the individual pixels for the New toolbar button in the preview window. • You create a ToolTip by appending to the status bar text (in the Prompt property box) a \n and the text you want displayed in the ToolTip.