VS2010-MFC (Common Controls: Button control buttons, Radio button, and check Box)

Source: Internet
Author: User

Transferred from: http://www.jizhuomi.com/software/182.html

Introduction to Button controls

A button control includes a command button (button), a radio button (Radio button), and a checkbox (check box) . The command button is the narrow button control we mentioned many times before, which responds to the user's mouse click and handles it so that it can display text or embed bitmaps. When a radio button is used, it is generally composed of more than one group, and the selected state of each radio button in the group is mutually exclusive, that is, only one of the radio buttons of the same group is selected.

command buttons are the most familiar and most commonly used button controls, while radio buttons and check boxes are a special kind of button controls. Radio buttons have checked and unchecked two states , and a blue dot appears in the center of the radio button to identify the selected state when selected. The General check box is also checked and unchecked two states , when checked box will be added a "√", while the three-state check box (set Bs_3state style) has a selected, unchecked and indeterminate three states, the indeterminate state when the check box appears in a gray "√".

The button control sends a notification message to the parent window, and the most common notification message is bn_clicked and bn_doubleclicked. When the user clicks the mouse button, the bn_clicked message is sent to the parent window, and the bn_doubleclicked message is sent when the mouse is double-clicked.

The creation of a button control

MFC provides all the actions of the CButton class to encapsulate a button control.

In the previous tutorial, we were adding the button control resources directly on the dialog template, but in some special cases we needed to create the button control dynamically, that is, by creating the member function of the CButton class. The following is the prototype of the CREATE function:

Virtual BOOL Create (
LPCTSTR Lpszcaption,
DWORD Dwstyle,
Const rect& RECT,
cwnd* pParentWnd,
UINT NID
);

Parameter description:

Lpszcaption: Specifies the text that the button control displays.

Dwstyle: Specifies the style of the button control, which can be set to any combination of the following button styles.

Bs_autocheckbox: Same as Bs_checkbox, but the button automatically reverses when you click the mouse
Bs_autoradiobutton: Same as Bs_radiobutton, but the button automatically reverses when you click the mouse
bs_auto3state: Same as bs_3state, but changes state when button is clicked
Bs_checkbox: Specifies a selection box with a caption to the right of the rectangle button
Bs_defpushbutton: Specifies the default command button, which has a black box around the button, and the user can press ENTER to quickly select the button
Bs_groupbox: Specify a group box
Bs_lefttext: Make the caption of the control appear to the left of the button
Bs_ownerdraw: Specifies a custom-painted button
Bs_pushbutton: Specify a command button
Bs_radiobutton: Specify a radio button to display the body on the right side of the circle button
bs_3state: Same as Bs_checkbox, but controls have 3 states-Select, not selected, and dimmed

Of course, in addition to the styles listed above, the Ws_child, ws_visible, and ws_tabstop styles are usually set for the buttons, and the Ws_tabstop style makes the button control have the tab stop property, which is the ability to stop the focus control when the TAB key is toggled on the button control. When you create a group of radio buttons, the style of the first button should be set to ws_child| ws_visible| ws_tabstop| Ws_group|bs_autoradiobutton, the style of the other radio buttons should be ws_child| Ws_visible|bs_autoradiobutton, not including Ws_tabstop and Ws_group.

When you add a button control directly on a dialog box template, its properties include the above style, for example, the Tri_state property of a check box actually represents the Bs_3state style.

The remaining three parameters are similar to the corresponding parameters in the CREATE function of the static text box, so you can refer to the previous static text box or review MSDN.

main member functions of the CButton class

Here are some of the main member functions of the CButton class, as you can see in MSDN for other functions.

Hbitmap SetBitmap (Hbitmap hbitmap);

Sets the bitmap to display in the button. The parameter hbitmap is a handle to the bitmap. The return value is the handle to the original bitmap of the button.

HBITMAP getbitmap () const;

Gets a handle to the button bitmap that was previously set by the SetBitmap function.

void SetButtonStyle (UINT nstyle,bool bredraw = TRUE);

Sets the style of the button. The Nstyle parameter specifies the style of the button, Bredraw specifies whether the button is redrawn, true to redraw, otherwise not redrawn, which is redrawn by default.

UINT Getbuttonstyle () const;

Gets the style of the button control.

void SetCheck (int ncheck);

Sets the selection state of the button. A parameter ncheck of 0 indicates unchecked, 1 is selected, and 2 indicates an indeterminate state (check box only).

int Getcheck () const;

Gets the selection state of the button. The meaning of the return value is the same as the Ncheck parameter of the SetCheck function.

Hcursor setcursor (Hcursor hcursor);

Sets the cursor graph to be displayed on the button. The parameter hcursor specifies the handle of the cursor. The return value is the handle to the original cursor of the button.

Hcursor getcursor ();

Gets the handle of the cursor previously set by SetCursor.

Hicon SetIcon (Hicon hicon);

Sets the icon to be displayed on the button. The parameter hicon specifies the handle to the icon. The return value is the handle to the original icon of the button.

Hicon GetIcon () const;

Gets the handle to the icon that was previously set by SetIcon.

void SetState (BOOL bhighlight);

Sets the highlight state of the button. Parameter bhighlight Specifies whether the button is highlighted, not 0 is highlighted, otherwise the highlighted state is deselected.

UINT GetState () const;

Gets the selection state, highlight state, and focus state of the button control. We can obtain various status values by returning the values to each mask, and the mask and the corresponding phase result are described as follows:

Mask 0x0003: Used to get the state of a radio button or check box. The phase result is 0 unchecked, 1 is selected, and 2 indicates an indeterminate state (check box only).
Mask 0x0004: Used to determine whether a button is highlighted. The phase result is a non-0 value indicating that the button is highlighted. When you click the button and hold down the left mouse button, the buttons appear highlighted.
Mask 0x0008: The phase result is a non-0 value indicating that the button has input focus.

The following is a list of several member functions that inherit from the CWnd class , which makes it easy to get or set the state of a button control by simply knowing the button's ID.

void CheckDlgButton (int nidbutton,uint ncheck);

Used to set the selection state of the button. The parameter Nidbutton specifies the ID of the button. A value of Ncheck of 0 means that the button is not selected, 1 means that the button is selected, and 2 indicates that the button is in an indeterminate state (check box only).

UINT isdlgbuttonchecked (int nidbutton) const;

Returns the selection state of a check box or radio button. A return value of 0 indicates that the button is not selected, 1 indicates that the button is selected, and 2 indicates that the button is in an indeterminate state (check box only).

void Checkradiobutton (int nidfirstbutton,int nidlastbutton,int Nidcheckbutton);

Use to select a radio button in the group. Parameter Nidfirstbutton Specifies the Id,nidlastbutton of the first button in the group specifying the Id,nidcheckbutton of the last button in the group specifies the ID of the button to select.

int Getcheckedradiobutton (int nidfirstbutton, int nidlastbutton);

Used to get the ID of the selected button in a group of radio buttons. The parameter Nidfirstbutton describes the Id,nidlastbutton of the first button in the group that describes the ID of the last button in the group.

In addition, the member functions of the CWnd Class GetWindowText (), SetWindowText (), etc. can also be used to get or set the text displayed in the button.

The basics of using button control buttons, Radio button, and check box are described here, and the following section shows examples of how various button controls are used.

VS2010-MFC (Common Controls: Button control buttons, Radio button, and check Box)

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.