1 / 13

CITA 342 Section 6

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.

Télécharger la présentation

CITA 342 Section 6

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CITA 342 Section 6 Message Maps, Menus, and Toolbars

  2. 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.

  3. 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.

  4. 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.

  5. 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 };

  6. Message Map Example BEGIN_MESSAGE_MAP(CGreetingView, CView) … ON_WM_LBUTTONDOWN() END_MESSAGE_MAP()

  7. Message Map Example void CGreetingView::OnLButtonDown(UINT nFlags, CPoint point) { MessageBox(_T("Hello, World!")); CView::OnLButtonDown(nFlags, point); }

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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.

More Related