Play Win32 development with me (11): use the control -- let's take a look at the button

Source: Internet
Author: User

Through controls and ApplicationsProgramInteraction: Before bragging, first introduce a tool, which is an official tool. You can use it to preview the appearance and style of commonly used controls, and what messages are received and sent during operations on the control. As follows:

Http://www.microsoft.com/en-us/download/details.aspx? Id = 4635

 

We can regard the control as a special type of window. Therefore, the createwindow or createmediawex function is used to create the control just like the window. However, remember to use the following two handsome guys in the window style:

A. ws_child: the control is placed on our window. Naturally, it must be used as a subwindow of the window. ws_childwindow is also the same. To save a few letters, use ws_child.

B. ws_visible: to use controls, make them visible to others. If you want others to praise your wife for being beautiful, you must first let others see your wife. Haha, don't think about it.

 

The theory is abstract. Let's say "What you see is what you get. So when is it appropriate to create controls? One way is to create it in the winmain method and set the hwndparent of the createwindow function as the handle of the window.

I use the second method here. We know that after the window is created, we will receive the wm_create message before it is displayed, that is, before the createwindow function returns, and we will respond to its call, work hard to create a button.

 
Case wm_create: {// create button hwnd hbutton = createwindow (L "button", l "can be used to click on me! ", Ws_visible | ws_child | bs_pushbutton, 35, 45,160, 65, hwnd, null, hg_app, null);} return 0;

Hg_app is a global variable defined by me, the handle of the current application (hinstance type ).

Then, run the command to check the effect.

 

Next, a new question arises. I created a button, but how can I respond to user clicks? In fact, this button is the same as the menu item. After you click "intimate" contact with the user, our windowproc will receive the wm_command message, just like the menu.

Wparam's low byte bit indicates the ID number and the high byte bit indicates the control notification. For example, if the user clicks a button and the notification code is bn_clicked, we can see what the user has done for the button.

The control handle is saved in lparam.

The question is, how to set the Control ID? Let's take a look at the document introduction of createwindow.

 
Hwnd winapi createwindow (_ in_opt _ maid, _ in_opt _ lpctstr lpwindowname, _ in _ DWORD dwstyle, _ in _ int X, _ in _ int y, _ in _ int nwidth, _ in _ int nheight, _ in_opt _ hwnd hwndparent, _ in_opt _ hmenu, _ in_opt _ hinstance, _ in_opt _ lpvoid lpparam );

Hmenu [IN, optional]
Type:Hmenu
A handle to a menu, or specifies a child-window identifier depending on the window style. for an overlapped or pop-up window, hmenu identifies the menu to be used with the window; it can be null if the class menu is to be used. for a child window, hmenu specifies
The child-window identifier, an integer value used by a dialog box control to define y its parent about events. the application determines the child-window identifier; it must be unique for all child windows with the same parent window.

To put it simply, a control usually does not need a menu. Therefore, you can use this parameter to set the Control ID. If hmenu is idle, you can give it an ID to play. The ID number is an integer. For readability, a macro is usually declared. In fact, the resource ID (such as idm_fuck) we use in the resource editor is in the resource. since the macro defined in H is called ID, you will know that its value should not be repeated.

We can also simulate that three macros are also declared before the file (# include...) to identify the three buttons respectively.

 
# Define idb_one3301 # define idb_two3302 # define idb_three3303

Create three buttons:

 
Case wm_create: {// create three buttons createwindow (L "button", l "button 1", ws_visible | ws_child | bs_pushbutton, 35, 10,120, 60, hwnd, (hmenu) idb_one, hg_app, null); createwindow (L "button", l "button 2", ws_visible | ws_child | bs_pushbutton, 35, 80,120, 60, hwnd, (hmenu) idb_two, hg_app, null); createwindow (L "button", l "button 3", ws_visible | ws_child | bs_pushbutton, 35,150,120, 60, hwnd, (hmenu) idb_three, hg_app, null);} return 0;

Then we will respond to the wm_command message.

Case wm_command: {Switch (loword (wparam) {Case idb_one: MessageBox (hwnd, l "you clicked the first button. ", L" prompt ", mb_ OK | mb_iconinformation); break; Case idb_two: MessageBox (hwnd, l" you clicked the second button. ", L" prompt ", mb_ OK | mb_iconinformation); break; Case idb_three: MessageBox (hwnd, l" you clicked the third button. ", L" prompt ", mb_ OK | mb_iconinformation); break; default: break;} return 0;

Check the effect.

 

At this time, I hope that after I click the button, the text on the button will change to "button X has been clicked". What should I do? The Windows system is based on the message mechanism. Therefore, the first thought of sending messages to the control should be sent to change the control-related text.Wm_settextMessage.

We put the aboveCodeChange it.

Case wm_command: {Switch (loword (wparam) {Case idb_one: // MessageBox (hwnd, l "you clicked the first button. ", L" prompt ", mb_ OK | mb_iconinformation); sendmessage (hwnd) lparam, wm_settext, (wparam) null, (lparam) l" the first pressed cursor has been clicked "); break; Case idb_two: // MessageBox (hwnd, l "you clicked the second button. ", L" prompt ", mb_ OK | mb_iconinformation); sendmessage (hwnd) lparam, wm_settext, (wparam) null, (lparam) l" the second worker has been clicked "); break; Case idb_three: // MessageBox (hwnd, l "you clicked the third button. ", L" prompt ", mb_ OK | mb_iconinformation); sendmessage (hwnd) lparam, wm_settext, (wparam) null, (lparam) l" the third one has been clicked "); break; default: break;} return 0;

As we know before, the lparam of the wm_command message stores the control handle. Therefore, the first parameter we pass to sendmessage is the handle of the operation target. Note that do not pass the parameters in the windowproc callback here, because the object we want to operate on now is a button, not a window. The handle passed by windowproc refers to the window we registered, because we have set this windowproc function in wndclass.

To operate the button, use the value contained in lparam of wm_command to forcibly convert it to hwnd.

Shows the running result.

 

The complete example is as follows:

# Include <windows. h> # include <winnt. h> // # include "resource. H "# define idb_one3301 # define idb_two3302 # define callback windowproc (hwnd, uint MSG, wparam, lparam); lpcwstr lps_cl = l" MyApp "; // class name lpcwstr wd_text = l "Super application"; // window title hinstance hg_app; // global instance handle int winapi wwinmain (hinstance hthisapp, hinstance hprevapp, lpwstr lpcmd, int nshow) {wndclassex WC ={}; WC. cbsize = s Izeof (wndclassex); WC. hbrbackground = (hbrush) color_window; WC. hinstance = hthisapp; WC. lpfnwndproc = (wndproc) windowproc; WC. lpszclassname = lps_cl; WC. style = cs_hredraw | cs_vredraw; registerclassex (& WC); hwnd = create1_wex (temperature, lps_cl, wd_text, temperature, 400,300, null, null, hthisapp, null ); if (hwnd = NULL) Return-1; showwindow (hwnd, nshow); updatewindow (hwnd); hg_app = hth Isapp; MSG; while (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG);} return 0 ;} lresult callback windowproc (hwnd, uint MSG, wparam, lparam) {Switch (MSG) {Case wm_destroy: postquitmessage (0); Return 0; Case wm_create: {// create three buttons createwindow (L "button", l "button 1", ws_visible | ws_child | bs_pushbutton, 35, 10,160, 60, hwnd, (hmenu) idb_one, hg_app, null); createwindow (L" Button ", l" button 2 ", ws_visible | ws_child | bs_pushbutton, 35, 80,160, 60, hwnd, (hmenu) idb_two, hg_app, null); createwindow (L" button ", L "button 3", ws_visible | ws_child | bs_pushbutton, 35,150,160, 60, hwnd, (hmenu) idb_three, hg_app, null);} return 0; Case wm_command: {Switch (loword (wparam) {Case idb_one: // MessageBox (hwnd, l "you have clicked the first button. ", L" prompt ", mb_ OK | mb_iconinformation); sendmessage (hwnd) lparam, wm_settext, (wparam) null, (lparam) l" the first pressed cursor has been clicked "); break; Case idb_two: // MessageBox (hwnd, l "you clicked the second button. ", L" prompt ", mb_ OK | mb_iconinformation); sendmessage (hwnd) lparam, wm_settext, (wparam) null, (lparam) l" the second worker has been clicked "); break; Case idb_three: // MessageBox (hwnd, l "you clicked the third button. ", L" prompt ", mb_ OK | mb_iconinformation); sendmessage (hwnd) lparam, wm_settext, (wparam) null, (lparam) l" the third one has been clicked "); break; default: break;} return 0; default: Return defwindowproc (hwnd, MSG, wparam, lparam);} return 0 ;}

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.