Win32 dialog Box Program (2)

Source: Internet
Author: User

Then the Win32 dialog Program (1) to write, solve the remaining problem, that is, understanding the function and its invocation relationship. Some parts of the article are their own inference, so there must be inaccurate or even wrong to describe the place, please correct me, thank you ~

Body Segmentation ************************************

WinMain function

The Win32 Dialog Program (1) says that WinMain is the entry for the application and has four input parameters, according to the description on MSDN:

input variable declaration description
hinstance     HINSTANCE
hinstance    hprevinstance
lpstr    lpcmdline the command line for the application, excluding the program name
int    ncmdshow Set how the window is displayed, such as ncmdshow=sw_maximize for maximized windows, see MSDN

Because WinMain is the application's entry, that is, the first function of the program run, so these four parameters should be the operating system passed to WinMain, as to how the operating system brought them in, and now no control [email protected][email protected]~

What is a handle? What is an instance? If we run two Notepad programs at the same time, we will find that the different actions against them are not interfering with each other, both " Windows " even though the two instances of Notepad , they are loaded in memory in different spaces, but in order to achieve non-interference operation and processing , we need to distinguish each other , we use the handle To identify different instances of the application (two Notepad handles are different), we can access the information of the corresponding object through the handle, but the handle is not a pointer, the program cannot use the handle to read the information directly in the file, it is only a unique integer value.

lpCmdLine is a command line that does not contain a program name, and the function GetCommandLine () can also be used to return a command line string, depending on MSDN, but it will contain the program name. To verify what this so-called "command line" is, add two lines of code at the beginning of the WinMain function body to display it

12 MessageBox(NULL, GetCommandLine(), TEXT("CMDLINE"), MB_OK);MessageBox(NULL, lpCmdLine, TEXT("CMDLINE_1p"), MB_OK);

Run in VC6.0, GetCommandLine () appears as the full path including the program name, lpCmdLine appears empty, as follows:

So I run in command line mode

Shows what the so-called command line for the application is, maybe it can be used to pass parameters to the program for interactive operation.

In addition, the entry function name is not necessarily WinMain or main (), but it is necessary to call a function like GetCommandLine () in the body of the main function to introduce the four parameters of WinMain as variables (which need to be used later), in addition to the _ Twinmain (further review required)

Now we have a general understanding of the WinMain function, and then we'll see what the other functions are doing.

Understanding of Function and program framework

Paste WinMain Function First

12345678910 intAPIENTRY WinMain(HINSTANCEhInstance,                     HINSTANCEhPrevInstance,                     LPSTRlpCmdLine,                     intnCmdShow){    //Enable IPAddress、Calendar.etc    InitCommonControls();    DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, Main_Proc);    return0;}
Name of function Parameters Description
initcommoncontrols none This function registers and initializes the generic control window class, which is called when using some advanced controls. The linker links your program to Comctl32.lib, and then loads the Comctl32.dll when the program starts. (This function is deprecated, using initcommoncontrolsex)
DialogBox (HInstance, Makeintresource (Idd_main), NULL, Main_proc)

According to MSDN, the function creates a modal dialog box from a dialog box template resource, and then gives control to the custom Main_proc function, This callback function ends the modal dialog box by calling the EndDialog function.

Parameter description : parameter 1 is a handle containing the dialog box template, which can be null; parameter 2 is the dialog box template, using MAKEINTRESOURCE Macro to convert the dialog ID (integer) to resource type; parameter 3 is a handle to the window owns the dialog box; parameters 4 is a pointer to the dialog box procedure, a function pointer.

The MainDlg.cpp contains four functions:

Name of function

Parameters

Function

(Hwnd hwnd, uint  umsg, wparam wparam, lparam lparam)

message shunt for message shunt  

(Hwnd hwnd, hwnd  hwndfocus, lparam lparam)

return

(Hwnd hwnd, int  id, hwnd hwndctl, uint codenotify)

switch (ID), Case

Main_onclose

(HWND hwnd)

EndDialog

Message Splitter
1234567891011 BOOLWINAPI Main_Proc(HWND hWnd, UINTuMsg, WPARAMwParam, LPARAMlParam){    switch(uMsg)    {        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);        HANDLE_MSG(hWnd, WM_CLOSE, Main_OnClose);    }    returnFALSE;}

Handle_msg is also a macro, which is defined in Windowsx.h, as follows:

1 #define HANDLE_MSG(hwnd, message, fn) case(message): returnHANDLE_##message((hwnd), (wParam), (lParam), (fn))

such as code

Handle_msg (HWnd, Wm_initdialog, Main_oninitdialog);

is actually replaced by

Case (Wm_initdialog): Return Handle_wm_create (HWND), (WParam), (LParam), (Main_oninitdialog))

Visible, the function of the visible message shunt is that when the message appears, the control is given to different programs, and some relevant parameters are passed to the corresponding program at the same time.

such as wm_initdialog, such as the message, is passed to the program by the operating system, according to the Msdn,wm_initdialog message is immediately before the dialog box to display (that is, the dialog box in memory is ready, but not yet displayed on the screen) to the program, The program can perform some initialization at this point, so the WM_COMMAND message and the WM_CLOSE message are literally basically understandable.

Next, the code for the other three functions is posted below:

1234567891011121314151617181920212223 BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam){    return TRUE;}void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify){    switch(id)    {        case IDC_OK:        {                       MessageBox(hwnd,TEXT("欢迎"),TEXT("问好"),MB_OK);        }        break;        default:        break;    }}void Main_OnClose(HWND hwnd){    EndDialog(hwnd, 0);}

As can be seen, the Main_proc function is always in execution, waiting for the operating system to pass the message in, according to different messages to give control to different functions, including initialization and shutdown functions, of course, the main implementation of the function Main_oncommand, the dialog box has menus and different controls, They are marked by ID, and if the user has done something to them (such as clicking), enter the appropriate case in the Main_oncommand function body, execute the code in it, as above, when the "OK" button is clicked, the "Welcome" dialog box pops up.

**********************************************************************************************

The above is the "C language can also do great things," the basic framework of the dialog box program, of course, the dialog box program should be very simple in Windows programming, there are many other content to learn, in addition, learning Windows programming is an important part of mastering as many APIs as possible, high-rise flat , Step by Step ~

Legacy issues

The operating system is how to pass parameters to the program (not urgent), that is, a more in-depth understanding of Windows Messaging mechanism;

Reference
    1. C can do great things http://rupeng.com/forum/forum-52-1.html

    2. WinMain and main http://blog.163.com/[email protected]/blog/static/99751486201212345130881/

    3. Message Splitter http://www.cnblogs.com/ifaithu/articles/2478841.html

Win32 dialog Box Program (2)

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.