Use MFC to create menu buttons

Source: Internet
Author: User

 

 

Nowadays, many software have such an interface effect: when a user clicks a button

Is to simply execute a function or pop up a dialog box, but a menu is displayed next to the button, let

To some extent, this replaces a simple dialog box, and

Is"User friendly ". There are basically two types of buttons: display text on the button and

Show the arrows on, show the arrows are common to the right and down two, there are up and left.

The icon shows a common style, that is, the down arrow and the menu displayed in the lower left corner of the button. Then, we compile

How does one implement this function?

---- We know that the CButton class in MFC has a virtual function named DrawItem ().

The control is specified in the template.BS_OWNERDRAW style, this function will be called at runtime to draw a button,

WhileThe member function TrackPopupMenu () of the CMenu class can pop up a menu at any position on the screen. By

As long as we use these two functions properly, we can create"Menu button.

 

---- The following CMenuButton class encapsulates all of these functions. Let's take a look at its production.

Principle.

 

---- After obtaining the rectangle of the button, pass the value of a corner

The TrackPopupMenu () function can be used to implement the pop-up menu, Which is used inside TrackPopupMenu.

The TPM_RETURNCMD flag can obtain the command ID of the menu selected for further processing;

LoadAfter the DrawItem () function, we can use it internally.

CDC: DrawFrameControl () function to draw the basic button appearance, and then draw an arrow in the middle

Header. Arrows can be usedMarlett font. Some may worry that if other people's machines do not

InstallationWhat should I do with the Marlett font? In fact, any machine that installs Windows leaves the Marlett font.

Cannot work normally. Please refer to it first. This isWindows "system tool" comes with the "character ing table ".

 

 

---- Do you see the arrows in the top row? They are to be painted on the buttons. Wait,

How are the other symbols so familiar? This is not what almost every window has."Minimize ",

"Restore", "close", and "maximize" buttons? Yes. Windows uses these characters in the title.

It is drawn on the bar. In fact,The most "standard" arrow in Windows is to use the Marlett word.

Body, whether it is the arrow on the toolbar or the arrow in the combo box, are drawn in this way. Sometimes, in disorder

After the font is deleted, the drop-down arrow of the combo box or toolbar is changed to 6 or 9. Why? See

On the Status Bar"Strike key value? -- "6", number to the right, the smaller down arrow is exactly -- "9 ".

 

 

---- The following is the specific production process.

 

---- First, generate an MFC AppWizard EXE project, preferably a dialog box-based project. When

However, you can also use existing projects. GenerateThe CButton is a new class for the base class named

CMenuButton, and then use ClassWizard to add two member functions for it: DrawItem ()

AndPreSubclassWidnow (); manually add the BOOL type m_bDrawFocusRect to the CMenuButton class

Member variable, used to determine whether to draw a focus rectangle on the button, addSetDrawFocusRect () function

Set this flag. By default, the focus rectangle is drawn. Two enumerated variables are added.M_ArrowType and

M_PopupPos is used to determine the arrow type and menu pop-up position. The arrow can be the right arrow,

Down Arrow, right arrow, down arrow, up arrow, and left arrow (see the figure at the beginning of this article); menu

In the upper left corner, upper right corner, lower left corner, and lower right corner of the button. Finally, manually add two

Functions,SetArrowType () and SetMenuPopupPos () are used to set the preceding styles.

The values are the right arrow and the pop-up in the lower left corner. If you only need a menu and do not need to draw arrows, you only need to leave it empty.

BS_OWNERDRAW: Add a SetStyle () function to set whether to draw arrows or display

Text. The default value is an arrow. For convenient processingBN_CLICKED notification message, which is

Create a public member function OnClick () in the CMenuButton class so that it can be located in the message of BN_CLICKED.

It is called in the handler. It has two parameters. The first one is the menu resourceID. The second parameter is the sub-menu's

ID. The default value is 0. If there is only one group of sub-menus, you can use its default value 0. OnClick () function

Command that returns the selected menu itemID. If no valid selection is made, 0 is returned.

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.