Play Win32 development with me (13): handshake dialog box

Source: Internet
Author: User

When talking about the dialog box, I believe there are not many people familiar with it, let alone the coders. You may ask, what is the difference between the dialog box and the window? There is no difference in nature, and a dialog box is also a window (as mentioned earlier, controls can also be seen as subwindows ).

The simplest dialog box is the dialog box popped up by MessageBox, right? I am confident that this function will be used by everyone. After all, it is very simple.

Okay, you don't need to talk about it. Let's start the first thing in this article and create a dialog box.

A dialog box is a resource that is stored in a resource file (. RC). If the project does not contain an RC file, the first method is to right-click the resource file node in Solution Explorer, select "add"-"new item" from the menu to add an RC file. The second method is to open "resource view" from the "View" menu of Vs, right-click the Project Name node in the resource view, select "add"-"resource" from the menu ".

Then, select the dialog box and click Create.

In the Properties window, name an ID for this dialog box. If you like it, set it to idd_mydlg.

OK, now we can use the visual designer to play it. It's good, although it's not as fierce as the winform designer.

Delete the two buttons by default. we drag and drop some controls from the toolbox. Remember to name the Control ID, just like setting the name attribute in winform.

In this case, drag a static text and button control, and then we try to implement a function: click the button to display the text in the static text box. I won't talk about how to use this editor.

 

Save the resource file. Let's start writingCode.

1. Message Processing in the Main WindowProgramIn response to the wm_create message, use the createdialog function to create and display the non-modal dialog box.

 
Lresult callback winmainproc (hwnd, uint MSG, wparam, lparam) {hwnd hdlg; Switch (MSG) {Case wm_create: hdlg = createdialog (hgapp, makeintresource (idd_mydlg ), hwnd, (dlgproc); If (hdlg) {// display dialog box showwindow (hdlg, sw_normal);} return 0; Case wm_destroy: postquitmessage (0); Return 0; default: Return defwindowproc (hwnd, MSG, wparam, lparam);} return 0 ;}

The last parameter of createdialog is a callback, which is similar to our windowproc. Note that when defining this function, you must first declare it before the header file or source file, otherwise, we won't be able to find it here. We usually put all these functions behind the winmain function, but this is usually the case. It doesn't mean we must do this.

Dlgproc is as follows:

// Int_ptr callback dlgproc (hwnd hdlg, uint MSG, wparam, lparam) {return (int_ptr) false ;}

Like windowproc, there is also a defdlgproc, but it is best not to call it here. Pay attention to the last section about this function description on msdn.

TheDefdlgprocFunction must not be called by a dialog box procedure; doing so results in Recursive Execution.

If defdlgproc is called in dialogproc, it will lead to an endless loop. In fact, the message loop we write for the window is also an endless loop, and getmessage is continuously executed, unless the wm_quit message is received to let it return false (0), it will jump out of the loop, and for the dialog box, we didn't write getmessage for it or post quitmessage to it. It may not jump out of the loop.

Now, the program can run.

However, no matter how you operate, the dialog box still does not return because we have not processed the relevant messages yet.

When you click the maximize, minimize, or close button in the operating system menu or title bar, you will receive the wm_syscommand message. If you do not respond to wm_syscommand, It will be placed in wm_command. However, wm_command usually processes the control message, therefore, it is best to use the wm_syscommand message.

 

2. Respond to wm_syscommand in the dlgproc in the dialog box.

Case wm_syscommand: If (wparam = SC _close) {// If the close/destroy dialog box is executed, the message destroywindow (hdlg) will be received;} return 0;

3. We already know that the response button is processing wm_command. To change the text in static text, you can use the static_settext macro, setwindowtext, and wm_settext. However, no matter which method we use, we have to solve a problem-how to obtain the handle of the static text control. So, let's take a look at this function:

 
Hwnd winapi getdlgitem (_ in_opt _ hwnd hdlg, _ in _ int niddlgitem );

As you can guess, the first parameter is the handle of the dialog box, and the second parameter is the ID of the control to return the handle. Okay. Let's try.

 
Case wm_command: {If (loword (wparam) = idc_btn) {ncount ++; // each time you click it, + 1 // get the control handle hwnd hstatic = getdlgitem (hdlg, idc_disp); // set the control text wchar STR [maxchar]; // format the string int n = wsprintf (STR, l "you clicked % d times. ", Ncount); // after the last character, add the end character STR [N] = '\ 0'; setwindowtext (hstatic, STR) ;}} return 0;

Run it now. Each time you click a button, the static text control displays "you have clicked the X button.

 

Okay, you're done.

 

The following is a complete code list.

# Include <windows. h> # include "resource. H "lresult callback (hwnd, uint MSG, wparam, lparam); int_ptr callback (hwnd hdlg, uint MSG, wparam, lparam); hinstance hgapp; // current application handle int winapi winmain (hinstance hthisapp, hinstance hprevapp, lpstr lpcmd, int nshow) {lpcwstr Cn = l "my "; wndclass WC = {sizeof (wndclass)}; WC. hbrbackground = (hbrush) color_window; WC. Hinstance = hthisapp; WC. lpfnwndproc = winmainproc; WC. lpszclassname = cn; WC. style = cs_hredraw | cs_vredraw; // register a window class registerclass (& WC); // create a window hwnd = createwindow (CN, l "Main Window", ws_overlappedwindow, 360,280, null, null, hthisapp, null); If (! Hwnd) return 0; hgapp = hthisapp; // display window showwindow (hwnd, nshow); // update window updatewindow (hwnd); // message loop MSG; while (getmessage (& MSG, null,) {translatemessage (& MSG); dispatchmessage (& MSG);} return 0 ;} lresult callback winmainproc (hwnd, uint MSG, wparam, lparam) {hwnd hdlg; Switch (MSG) {Case wm_create: hdlg = createdialog (hgapp, makeintresource (idd_mydlg), hwnd, (dlgproc ); if (hdlg) {// displayed dialog box sh Owwindow (hdlg, sw_normal);} return 0; Case wm_destroy: postquitmessage (0); Return 0; default: Return defwindowproc (hwnd, MSG, wparam, lparam );} return 0;} // The int_ptr callback dlgproc (hwnd hdlg, uint MSG, wparam, lparam) {static int ncount = 0; // Number of Button Clicks Switch (MSG) {Case wm_syscommand: If (wparam = SC _close) {// If the close/destroy dialog box is executed, the message destroywindow (hdlg);} return 0; Case wm_command: {if (Loword (wparam) = idc_btn) {ncount ++; // + 1 for each click. // obtain the control handle hwnd hstatic = getdlgitem (hdlg, idc_disp ); // set the control text wchar STR [maxchar]; // format the string int n = wsprintf (STR, l "you have clicked the % d button. ", Ncount); // after the last character, add the end character STR [N] = '\ 0'; setwindowtext (hstatic, STR) ;}} return 0 ;} return (int_ptr) false ;}

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.