Windows API button learning notes

Source: Internet
Author: User
Document directory
  • Return Value

The following are some things about button controls in windows, which I hope will be useful to you.

 

 

Common buttons include common buttons, single-choice buttons, check boxes, and group boxes. Common buttons are used to help users trigger specified actions. Generally, single-choice buttons are mutually exclusive; check boxes are used to display a set of options for the user to choose from. There is no mutual exclusion between options. The group box is mainly used to divide controls into different groups and describe them.

Hwnd_check = createwindow (
"Button", // predefined class
"OK", // button text
Ws_visible | ws_child | bs_checkbox, // values for buttons.
100, // starting X position
100, // starting y position
100, // button width
20, // button height
Hwnd, // parent window
(Hmenu) id_check, // no menu
(Hinstance) getwindowlong (hwnd, gwl_hinstance ),
Null );

 

The following code selects and cancels bs_checkbox-style buttons. Check id_check in loword (wparam) of the wm_command message.

Bool flag = sendmessage (hwnd_check, bm_getcheck, 0, 0) = bst_checked? False: true;
Sendmessage (hwnd_check, bm_setcheck, flag, 0 );

I found this article later:

Button styles
  • Bs_3state As with the check box, this style button can be dimmed. The dimmed State is usually used to indicate that the buttons of this style are being disabled.

  • Bs_auto3state is the same as the three-state check box. When you select this button, the style status changes.

  • Bs_autocheckbox is the same as the check box. In addition to the user-clicked control, a selected flag is displayed. When the user clicks the next button, the flag disappears.

  • Bs_autoradiobutton is the same as that in a single-frame. The difference is that when you click it, It is highlighted and the other buttons in the same group are highlighted and transferred to you.

  • Bs_bitmap specifies a button to be displayed as a bitmap.

  • Bs_bottom place the button title at the bottom of the button rectangle.

  • The bs_center button title is displayed in the center of the button's rectangle.

  • Bs_checkbox creates a small square on the right of the button (this style must be used with bs_lefttext .)

  • Bs_defpushbutton creates a general default button with a heavy black border. You can click the selector button by pressing the Enter key. This button can be used to implement the common functions (that is, the default action function ).

  • Bs_flat specifies that the button is a 2D button without the shadow used by the 3D control.

  • Bs_groupbox creates a group box for the control group. If the title is used, the title appears in the upper left corner of the group box..

  • Bs_icon indicates that an icon is displayed on the button. 

  • Bs_left align the title Left in the rectangle of the control. If the button is a check box or single button with no bs_rightbutton style, the text is left aligned on the right of the check box or single button (this is redundant, the text is displayed on the right side of a checkbox or circle .).

  • Bs_lefttext when the button is a single choice or a check box, the title text will appear on the left side of the customer area of the single choice or check box (that is, the rectangular box of the check box, the circle box of a single choice. 

  • Bs_multiline if the title text is too long, the text will be broken in the drawing area.

  • Bs_notify enables you to send bn_dblclk, bn_killfocus, and bn_setfocus messages to the parent window. Note: whether this style is used or not, there is a bn_clicked button that can send messages.

  • Bs_ownerdraw creates a self-painted button. When the appearance of a button changes, the Framework calls the drawitem member function. This style must be set when the cbitmapbutton class is used.

  • Bs_pushbutton creates a button (the most common button). When you click this button, a wm_command message is sent to the parent window.

  • Bs_pushlike is displayed in the form of a button (multiple selection box, three-state multi-selection box, and single-choice). This button is floating when not selected, but in the selected status it is in the status.

  • Bs_radiobutton creates a single rectangle. This button has a circular customer area (if this style is not used together with the bs_lefttext style). The title text is on the right. A single sequence is usually used in multiple associated options, but there is only one choice between them. 

  • Bs_right align the text right in the drawing area of the button. However, if the button is a single choice or check box without the bs_rightbutton style, the title text will be right aligned on the right side of the single choice or check box. 

  • Bs_rightbutton specifies the circular optional area of a single button or the square open check area of the check box appears on the right side of the rectangle area of the button.AndBs_lefttext has the same effect.

  • Bs_text specifies the button to display the text title.

  • Bs_top displays the title text on the top of the button's painting area.

  • Bs_userbutton is no longer used. It is used only for Windows compatible with 16-bit systems. For 32-bit Windows systems, replace it with the bs_ownerdraw style.

  • The title of the bs_vcenter setting button is centered vertically in the drawing area.

 

Original article:

 

MFC Library referencebutton styles

 

  • Bs_3stateSame as a check box, counter t that the box can be dimmed as well as checked. The dimmed state typically is used to show that a check box has been disabled.

  • Bs_auto3stateSame as a three-state check box, counter t that the box changes its state when the user selects it.

  • Bs_autocheckboxSame as a check box, should t that a check mark appears in the check box when the user selects the box; the check mark disappears the next time the user selects the box.

  • Bs_autoradiobuttonSame as a radio button, should t that when the user selects it, the button automatically highlights itself and removes the selection from any other radio buttons with the same style in the same group.

  • Bs_bitmapSpecifies that the button displays a bitmap.

  • Bs_bottomPlaces text at the bottom of the button rectangle.

  • Bs_centerCenters text horizontally in the button rectangle.

  • Bs_checkboxCreates a small square that has text displayed to its right (unless this style is combined withBs_lefttextStyle ).

  • Bs_defpushbuttonCreates a button that has a heavy black border. The user can select this button by pressing the Enter key. This style enables the user to quickly select the most likely option (the default option ).

  • Bs_flatSpecifies that the button is two-dimenies; it does not use the default shading to create a 3-D image.

  • Bs_groupboxCreates a rectangle in which other buttons can be grouped. Any text associated with this style is displayed in the rectangle's upper-left corner.

  • Bs_iconSpecifies that the button displays an icon.

  • Bs_leftLeft aligns the text in the button rectangle. However, if the button is a check box or radio button that does not haveBs_rightbuttonStyle, the text is left aligned on the right side of the check box or radio button.

  • Bs_lefttextWhen combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.

  • Bs_multilineWraps the button text to multiple lines if the text string is too long to fit on a single line in the button rectangle.

  • Bs_notifyEnables a button to sendBn_dblclk,Bn_killfocus, AndBn_setfocusNotification messages to its parent window. Note that buttons sendBn_clickedNotification message regardless of whether it has this style.

  • Bs_ownerdrawCreates an owner-drawn button. The framework calltheDrawitemMember function when a visual aspect of the button has changed. This style must be set when usingCbitmapbuttonClass.

  • Bs_pushbuttonCreates a Pushbutton that postsWm_commandMessage to the owner window when the user selects the button.

  • Bs_pushlikeMakes a button (such as a check box, three-state check box, or radio button) look and act like a push button. the button looks raised when it isn' t pushed or checked, and sunken when it is pushed or checked.

  • Bs_radiobuttonCreates a small circle that has text displayed to its right (unless this style is combined withBs_lefttextStyle). radio buttons are usually used in groups of related, but mutually exclusive, choices.

  • Bs_rightRight aligns the text in the button rectangle. However, if the button is a check box or radio button that does not haveBs_rightbuttonStyle, the text is right aligned on the right side of the check box or radio button.

  • Bs_rightbuttonPositions a radio button's circle or a check box's square on the right side of the button rectangle. Same asBs_lefttextStyle.

  • Bs_textSpecifies that the button displays text.

  • Bs_topPlaces text at the top of the button rectangle.

  • Bs_userbuttonObsolete, but provided for compatibility with 16-bit versions of Windows. Win32-based applications shocould useBs_ownerdrawInstead.

  • Bs_vcenterPlaces text in the middle (vertically) of the button rectangle

 

 

Use bm_getcheck to check whether it is selected, as shown below:

Sendmessage (hwnd_check, bm_getcheck, 0, 0)

Bst_checked

Button is checked.

Bst_indeterminate

Button is grayed, indicating an indeterminate state (applies only if the button has the bs_3state or bs_auto3state style ).

Bst_unchecked

Button is cleared

 

However, you can also use bm_getstate. The corresponding macro isButton_getstateIn addition, the message returns more States and returns the following:

Return Value

The return value specifies the current state of the button. It is a combination of the following values.

Return code Description
Bst_checked

The button is checked.

Bst_dropdownpushed

Windows Vista. The button is in the drop-down state. applies only if the button has the tbstyle_dropdown style.

Bst_focus

The button has the keyboard focus.

Bst_hot

The button is hot; that is, the mouse is hovering over it.

Bst_indeterminate

The state of the button is indeterminate. applies only if the button has the bs_3state or bs_auto3state style.

Bst_pushed

The button is being shown in the pushed state.

Bst_unchecked

No special state. equivalent to zero.

 

When you click the mouse, the message "bn_clicked" is triggered. The loword (wparam) is the Control ID, and the hiword (wparam) is the bn_clicked event.

But I don't know why it is written in msdn as follows:

This message is sent when the user taps the pen on the touch screen. the parent window of the button has es this message through the wm_command message. unlike the other button messages, this message is intended for applications written for any version of Windows.

Bn_clicked idbutton = (INT) loword (wparam );
Hwndbutton = (hwnd) lparam;

I have verified it myself and wrote the following code to verify it. Let's take a look at it. I hope to discuss it with you and ask experts for advice. My personal English skills are poor.

Case id_check:
{
If (hiword (wparam) = bn_clicked)
{MessageBox (hwnd, "", "", mb_ OK); sendmessage (hwnd, wm_settext, 0, (lparam )"****");}
Bool flag = sendmessage (hwnd_check, bm_getcheck, 0, 0) = bst_checked? False: true;
Sendmessage (hwnd_check, bm_setcheck, flag, 0 );
}

When I click id_check, The MessageBox will pop up and the window title will change to ******. I think the msdn description is incorrect.

 

Maybe the logic I wrote is a little messy. I did not write this for anything. I just took notes while learning. At the same time, I posted these notes to learn from everyone.

If you see it, you can give me some advice as a beginner. Next we will discuss the other hand,

 

 

In fact, buttons can change the style. For example, you can use a bitmap to replace the ugly button. You can see some icon buttons on QQ. Of course, I don't know how to do those icons on QQ.

Pretty, but I think it works the same way.

A message can be generated when the created window style is bs_bitmap or bs_icon.Bm_setimageAdd Bitmap Using the button created by myself. The compiler is

Mingw + code: block. I don't know how to add resources to it, so I haven't verified it. Try using vc6 in a short time. See the following,

Wparam

The type of image to associate with the button. This parameter can be one of the following values:

  • Image_bitmap
  • Image_icon
Lparam

A handle (HiconOrHbitmap) To the image to associate with the button.

Bs_icon or bs_bitmap set? Bm_setimage called? Result
Yes Yes Show icon only.
No Yes Show icon and text.
Yes No Show text only.
No No Show text only

Bm_getimage returns the bitmap handle. In msdn,Bm_getimageMessage retrieves a handle to the bitmap or icon associated with a button

 

A button sends the bn_disable, bn_pushed, bn_killfocus, bn_paint, bn_setfocus, and bn_unpushed notification Codes

Only if it has the bs_policy Style

The preceding message is valid only when the button contains bs_notify.

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.