Windows core Programming (2)

Source: Internet
Author: User

One, create a control or window

    1. The API to create a control or window is CreateWindow

    2. Button control is created, the button is also a window, he registers the window class is button

HWND hwndbtn = CreateWindow (_t ("button"), _t ("I am the button"), Ws_child,0,0,100,30,hwndparent, (HMENU) 1,hinst,null);

A button control is created in the hWndParent rect (0,0,100,300) area of the window, and the ID of this control is 1, which means that the button is

A WM_COMMAND message is sent to the Wwndparent window when clicked, and the wparam parameter is 1 low.

3. check box creation Modify the style of the button itself to turn the button into a check box, such as Bs_autocheckbox the button

becomes a checkbox, and when the mouse click, its selected state will automatically change, if it is selected, click on the

becomes unchecked, create the code as follows

HWND Hwndcheckbox = CreateWindow (_t ("button"), _t ("I am a check box"), ws_child|

Bs_autocheckbox,0,0,100,30,hwndparent, (HMENU) 2,hinst,null);


4.SendMessage LRESULT SendMessage (HWND hwnd,uint msg,wparam wparam,lparam LPARAM)

Sends a specific message ID to the specified window, with the parameters used for the message,

SendMessage the message is sent, the window that received the message must finish processing the message and return, SendMessage will not return.

Depending on the message type. The meaning of the return value is different


5. Obtain the checkbox's selected state,

A bm_getcheck message is sent to the CheckBox control window by SendMessage, which is selected,

SendMessage (hwndchkbox,bm_getcheck,0,0)

Return value: If the Bst_unchecked representative is not selected, if the Bst_checked representative is selected


6. How to manually set the checkbox state

Sends a BM_SETCHECK message to the CheckBox control window via SendMessage.

SendMessage (hwndchkbox,bm_setcheck,bst_checked,0)

The checkbox can be set unchecked by the following code

SendMessage (hwndchkbox,bm_setcheck,bst_unchecked,0)


7. Radio control

The radio control Ws_group style, when a radio control is created with this style,

The radio controls created after that are all in this group until there is another right with this style, and he is a new group



8. Implementation of window centering

The upper and lower left and right distance between the window and the desktop window is centered

Rect rect; GetWindowRect (hwnd,&rect); Gets the upper-left corner of the window to XY in the upper-right corner of the window to the upper-left corner of the desktop

To get a high-width method is to lower the right minus the top left, Rect.right-rect.left. Rect.bottom-rect.top;

Subtract half the width of the screen by half the width of the window to get to the left of the window to set the position.

Simply subtract the width of the window by the width of the screen/2.

Then MoveWindow (); On the line.



9.MFC composition

A. The derived class of CWinApp must be estimated, and this class defines a global object Theapp

When this object is initialized. Because it is static, so he is more than the WinMain function,

Was executed early, that is, he first executes the constructor of the class derived from CWinApp.

B. Derived classes must override the InitInstance virtual function as an entry function for MFC


10.MFC Message-Mapping mechanism

A. A dialog resource to be established and an association derived from the CDialog class,

When this derived class constructs, it passes the resource of this dialog box to the parent class,

B. This class can display this window when calling DoModal

The C.Vs Class Wizard provides quick-build, message-corresponding handler functions,

This will not require you to manually bind the message.

D. If you want to receive the binding message, first define the handler function, in this derived class

Begin_message_map (CMFC01Dlg, Cbcgpdialog)

End_message_map ()

In these two macro definition is actually the message processing, for example you want to add the button to be clicked

on_bn_clicked (Button ID, message processing function).







Windows core Programming (2)

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.