"Go" VS2010/MFC Programming Primer 20 (Common controls: Static text boxes)

Source: Internet
Author: User

This article turns from chicken Peck rice: http://www.jizhuomi.com/software/177.html Thanks to chicken peck Rice for the contribution of this article.

Last chicken peck Rice after the Color dialog box, the introduction of the use of dialogs and the various common dialog boxes is finished. Starting from this section Chicken Peck rice will explain the usage of various common controls. Common controls include: Static text boxes, edit boxes, radio buttons, check boxes, group boxes, list boxes, combo boxes, picture controls, list controls, tree controls, progress bar controls, and so on. This tutorial first explains the use of static text boxes.

Notification message for the control

Before you use a static text box, you'll probably tell the control's notification message.

When an event occurs for a control, it sends a notification message to the parent window. The most common event is a mouse click, when the control sends a BN_CLICKED message to the parent window, which in effect sends a WM_COMMAND message to the parent window, with a notification message code in the WPARAM parameter (the notification message code when the mouse click is Bn_ CLICKED) and the control Id,lparam parameter contains a handle to the control. In an overview of the MFC message mapping mechanism, the chicken pecked at Rice, the message is made up of three parts: message value, wparam parameter, and lparam parameter.

Adding a message-map and message-handling function to a control notification message has been done more than once. Now, for the details, the format of the control's message-map macro is roughly the following:

ON_ notification Message code (NID, Memberfun)

The NID parameter is the Id,memberfun parameter of the control that is the name of the message handler function. For example, on_bn_clicked (Idc_button1, &cdlg::onbnclickedbutton1). This message map macro should be added between Begin_message_map and End_message_map.

The syntax for a message-handling function declaration is:

afx_msg void Memberfun ();

Use of static text boxes

In front of the chicken peck Rice example, you should also know the general role of static text box, is used to display text description. MFC provides a CStatic class that encapsulates all operations on a static text box.

If we want to create static text boxes dynamically in a program instead of dragging directly from Toolbox to the dialog template as before, we need to use the member function of the CStatic class create. The prototype of the CREATE function is as follows:

Virtual BOOL Create (
LPCTSTR LpszText,
DWORD Dwstyle,
Const rect& RECT,
cwnd* pParentWnd,
UINT NID = 0xFFFF
);

Parameter description:

LpszText: Specifies the text to display in the control. If null, no text is displayed.

Dwstyle: Specifies the style of the static control. A static text box is typically a subwindow of a dialog box or other window, and is visible, so it should contain the ws_child and ws_visible styles, and, as illustrated in MSDN, you can also set any combination of styles in static control styles. Here are some of the styles that you might be explaining:

Ss_bitmap A bitmap is displayed in a static control, and the Lpsztext parameter string for the CREATE function is the bitmap name defined in the resource file. This style ignores the width and height parameters, and the static control automatically adjusts its size to fit the bitmap
Ss_blackframe Specifies a box with the same color as the bounds of the window, which is black by default
Ss_blackrect Specifies a real rectangle with the same color as the bounds of the window, which is black by default
Ss_center Aligns the displayed body to the center, and the body can be wrapped
Ss_grayframe Specifies a border with the same color as the screen background
Ss_grayrect Specifies a real rectangle with the same color as the screen background
Ss_icon Causes the control to display an icon defined in the resource, whose name is specified by the Lpsztext parameter of the CREATE function, and the icon automatically adjusts its size
Ss_left Left-justified body, body can wrap around
Ss_leftnowordwrap Left-justified body, body cannot wrap around
Ss_notify Enables the control to send mouse event messages to the parent window
Ss_right Right-align the body to wrap around
Ss_simple Make the static body cannot be changed at run time and the body is displayed in a single line
Ss_whiteframe Specifies a box with the same color as the background of the window, which is white by default
Ss_whiterect Specifies a solid rectangle with the same color as the background of the window, which is white by default

When we add a static text box to a dialog template, we can set its style in the static text box's property page, many of which correspond to the style above, for example, the simple attribute is equivalent to the ss_simple style.

Rect: Specifies the position and size of the static control, which can be a rect struct type or an object of the CRect class.

pParentWnd: Specifies the parent window of a static control, usually a CDialog object, and cannot be null.

NID: Specifies the ID of the static control.

Introduction to member functions of the CStatic class

The following is a brief introduction to the main member functions of the CStatic class, and here is a list of member functions.

Getbitmap Gets the handle to the bitmap set by the SetBitmap function
GetCursor Gets the handle of the cursor set by setcursor
GetEnhMetaFile Gets the handle to the enhanced metafile set by Setenhmetafile
GetIcon Gets the handle to the icon set by SetIcon
SetBitmap Set the bitmap to display in a static control
SetCursor Set the cursor picture to display in a static control
Setenhmetafile To set the enhanced metafile to display in a static control
SetIcon Set the icon to display in a static control

In addition to the above member functions, since CStatic is a derived class of CWnd, many member functions of CWnd can also be used, such as GetWindowText, GetWindowRect, SetWindowText, and so on.

The basic application method of static text box in the previous has been said, chicken peck rice is no longer an example, you can according to this section of the experiment, to the static text box more understanding. Welcome you to Chicken peck Rice Exchange study.

Go to VS2010/MFC Programming 20 (Common controls: Static text boxes)

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.