Win32 manually creating a Windows window, a little note

Source: Internet
Author: User
Tags textout

Excerpt from the document, where the function needs to take the time to see the WinMain add functionality
  1. First, a window class structure of type Wndclassex is created inside the WinMain function. This structure contains information about the window, such as the application icon, the background color of the window, the name displayed in the title bar, the name of the window procedure function, and so on. The typical wndclassex structure is as follows:

     Wndclassex Wcex;    wcex.cbsize = sizeof (wndclassex); Wcex.style = Cs_hredraw |    Cs_vredraw;    Wcex.lpfnwndproc = WndProc;    Wcex.cbclsextra = 0;    Wcex.cbwndextra = 0;    Wcex.hinstance = hinstance;    Wcex.hicon = LoadIcon (hinstance, Makeintresource (idi_application));    Wcex.hcursor = LoadCursor (NULL, Idc_arrow);    Wcex.hbrbackground = (hbrush) (color_window+1);    Wcex.lpszmenuname = NULL;    Wcex.lpszclassname = Szwindowclass; WCEX.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (idi_application)); 
        Wndclassex Wcex;    wcex.cbsize = sizeof (wndclassex);    Wcex.style          = Cs_hredraw | Cs_vredraw;    Wcex.lpfnwndproc    = WndProc;    Wcex.cbclsextra     = 0;    Wcex.cbwndextra     = 0;    Wcex.hinstance      = hinstance;    Wcex.hicon          = LoadIcon (hinstance, Makeintresource (idi_application));    Wcex.hcursor        = LoadCursor (NULL, idc_arrow);    Wcex.hbrbackground  = (hbrush) (color_window+1);    Wcex.lpszmenuname   = NULL;    Wcex.lpszclassname  = Szwindowclass;    Wcex.hiconsm        = LoadIcon (Wcex.hinstance, Makeintresource (idi_application));

    For a field explanation of this structure, see wndclassex.

  2. Now that you have created the window class, you must register it. Use the RegisterClassEx function and pass the window class structure as a parameter:

        if (! RegisterClassEx (&wcex))    {        MessageBox (NULL,            _t ("Call to RegisterClassEx failed!"),            _t ("Win32 Guided Tour "),            NULL);        return 1;    }
        if (! RegisterClassEx (&wcex))    {        MessageBox (NULL,            _t ("Call to RegisterClassEx failed!"),            _t (" Win32 Guided Tour "),            NULL);        return 1;    }
  3. Now that you have registered your own class, create the window. Use the CreateWindow function as follows:

     static TCHAR szwindowclass[] = _t ("Win32app"), static TCHAR sztitle[] = _t ("Win32 Guided Tour Application");//The Par Ameters to CreateWindow explained://szwindowclass:the name of the application//sztitle:the text This appears in the TI  Tle bar//ws_overlappedwindow:the Type of window to create//Cw_usedefault, cw_usedefault:initial position (x, Y)//500, 100:initial size (width, length)//null:the parent of this window//null:this application dows does have a menu bar//h     Instance:the first parameter from winmain//Null:not used in this applicationhwnd hWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, Cw_usedefault, Max, +, NULL, NULL, HINSTANCE, NULL     if (!hwnd) {MessageBox (NULL, _t ("Call to CreateWindow failed!"), _t ("Win32 Guided Tour"), NULL); return 1;} 
    Static TCHAR szwindowclass[] = _t ("Win32app"), static TCHAR sztitle[] = _t ("Win32 Guided Tour Application");//The Paramete RS to CreateWindow explained://szwindowclass:the name of the application//sztitle:the text This appears in the title B  ar//ws_overlappedwindow:the Type of window to create//Cw_usedefault, cw_usedefault:initial position (x, Y)//500, 100: Initial size (width, length)//null:the parent of this window//null:this application dows does have a menu bar//Hinsta Nce:the first parameter from winmain//Null:not used in this applicationhwnd hWnd = CreateWindow (    szwindowclass,
         
          sztitle,    Ws_overlappedwindow,    cw_usedefault, Cw_usedefault, Max    , +,    null,    null,    HINSTANCE,    null), if (!hwnd) {    MessageBox (NULL,        _t ("Call to CreateWindow failed!"),        _t ("Win32 Guided Tour "),        NULL);    return 1;}
         

    This function returns an HWND, which is a handle to a window. For more information, see Windows data types.

  4. Once the window has been created, we can use the following code to display it on the screen:

    The parameters to ShowWindow explained://hwnd:the value returned from createwindow//ncmdshow:the fourth parameter F Rom Winmainshowwindow (hWnd,    ncmdshow); UpdateWindow (HWND);
    The parameters to ShowWindow explained://hwnd:the value returned from createwindow//ncmdshow:the fourth parameter F Rom Winmainshowwindow (hWnd,    ncmdshow); UpdateWindow (HWND);

    So far, this window is not displayed because we have not implemented the WndProc function yet.

  5. The last step of WinMain is the message loop. The purpose of this loop is to listen for messages sent by the operating system. After the application receives the message, it dispatches the message to the WndProc function for processing. The message loop resembles the following:

    View Copy to Clipboard printing in a color-coded format
        msg msg;    while (GetMessage (&msg, NULL, 0, 0))    {        translatemessage (&msg);        DispatchMessage (&msg);    }    return (int) Msg.wparam;
        msg msg;    while (GetMessage (&msg, NULL, 0, 0))    {        translatemessage (&msg);        DispatchMessage (&msg);    }    return (int) Msg.wparam;

    For more information about the structures and functions used in message loops, see MSG, GetMessage, TranslateMessage, and DispatchMessage.

    The steps you have just completed are common to most Win32 applications. For the include directives and global variable declarations required for this application, see the complete code example at the end of this topic.

    At this point, the WinMain function should resemble the following:

    int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine,    int ncmdshow) {wndclassex Wcex;    wcex.cbsize = sizeof (wndclassex); Wcex.style = Cs_hredraw |    Cs_vredraw;    Wcex.lpfnwndproc = WndProc;    Wcex.cbclsextra = 0;    Wcex.cbwndextra = 0;    Wcex.hinstance = hinstance;    Wcex.hicon = LoadIcon (hinstance, Makeintresource (idi_application));    Wcex.hcursor = LoadCursor (NULL, Idc_arrow);    Wcex.hbrbackground = (hbrush) (color_window+1);    Wcex.lpszmenuname = NULL;    Wcex.lpszclassname = Szwindowclass;    WCEX.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (idi_application)); if (! RegisterClassEx (&wcex)) {MessageBox (NULL, _t ("Call to RegisterClassEx failed!"), _t ("        Win32 Guided Tour "), NULL);    return 1; } hInst = hinstance; Store instance handle in our global variable//the Parameters to CreateWindow explained://szwindowclass:the Name of the application//Sztitle:the text this appears in The title bar//ws_overlappedwindow:the type of window to create//Cw_usedefault, cw_usedefault:initial Positio N (x, y)//, 100:initial size (width, length)//null:the parent of this window//null:this application D  oWS not has a menu bar//hinstance:the first parameter from WinMain//Null:not used in this application HWND HWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, Cw_usedefaul    T, n, +, NULL, NULL, HINSTANCE, NULL);             if (!hwnd) {MessageBox (NULL, _t ("Call to CreateWindow failed!"), _t ("Win32 Guided Tour"),        NULL);    return 1; }//The parameters to ShowWindow explained://Hwnd:the value returned from CreateWindow//Ncmdshow:the Fourt H parameter from WinmaIn ShowWindow (HWnd, ncmdshow);    UpdateWindow (HWND);    Main message loop:msg MSG;        while (GetMessage (&msg, NULL, 0, 0)) {translatemessage (&msg);    DispatchMessage (&MSG); } return (int) Msg.wparam;}
    int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine,    int ncmdshow) {wndclassex Wcex;    wcex.cbsize = sizeof (wndclassex); Wcex.style = Cs_hredraw |    Cs_vredraw;    Wcex.lpfnwndproc = WndProc;    Wcex.cbclsextra = 0;    Wcex.cbwndextra = 0;    Wcex.hinstance = hinstance;    Wcex.hicon = LoadIcon (hinstance, Makeintresource (idi_application));    Wcex.hcursor = LoadCursor (NULL, Idc_arrow);    Wcex.hbrbackground = (hbrush) (color_window+1);    Wcex.lpszmenuname = NULL;    Wcex.lpszclassname = Szwindowclass;    WCEX.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (idi_application)); if (! RegisterClassEx (&wcex)) {MessageBox (NULL, _t ("Call to RegisterClassEx failed!"), _t ("        Win32 Guided Tour "), NULL);    return 1; } hInst = hinstance; Store instance handle in our global variable//the Parameters to CreateWindow explained://szwindowclass:the Name of the application//Sztitle:the text this appears in The title bar//ws_overlappedwindow:the type of window to create//Cw_usedefault, cw_usedefault:initial Positio N (x, y)//, 100:initial size (width, length)//null:the parent of this window//null:this application D  oWS not has a menu bar//hinstance:the first parameter from WinMain//Null:not used in this application HWND HWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, Cw_usedefaul    T, n, +, NULL, NULL, HINSTANCE, NULL);             if (!hwnd) {MessageBox (NULL, _t ("Call to CreateWindow failed!"), _t ("Win32 Guided Tour"),        NULL);    return 1; }//The parameters to ShowWindow explained://Hwnd:the value returned from CreateWindow//Ncmdshow:the Fourt H parameter from WinmaIn ShowWindow (HWnd, ncmdshow);    UpdateWindow (HWND);    Main message loop:msg MSG;        while (GetMessage (&msg, NULL, 0, 0)) {translatemessage (&msg);    DispatchMessage (&MSG); } return (int) Msg.wparam;}
Adding features to WndProc
  1. The purpose of the WndProc function is to process the messages received by the application. This is typically done using the Switch function.

    The first message we will handle is a WM_PAINT message. The application receives this message when a portion of the application window must be updated. The first time you create a window, you must update the entire window and pass this message to indicate this action.

    When dealing with WM_PAINT messages, the first thing to do is call BeginPaint, and the last thing you should do is call EndPaint. Between these two function calls, you can handle all the logic to arrange the text, buttons, and other controls in the window. For this application, we display the string "Hello, world!" in the window. To display the text, use the TextOut function, or you can use the DrawText function as follows:

    Paintstruct PS; HDC hdc; TCHAR greeting[] = _t ("Hello, world!"); Switch (message) {case WM_PAINT:    hdc = BeginPaint (hWnd, &ps);    Here your application are laid out.//for this introduction, we just print out "Hello, world!"    The top left corner. TextOut (HDC,        5, 5,        greeting, _tcslen (greeting));    End application-specific Layout section. EndPaint (HWnd, &ps);    break;}
    Paintstruct PS; HDC hdc; TCHAR greeting[] = _t ("Hello, world!"); Switch (message) {case WM_PAINT:    hdc = BeginPaint (hWnd, &ps);    Here your application are laid out.//for this introduction, we just print out "Hello, world!"    The top left corner. TextOut (HDC,        5, 5,        greeting, _tcslen (greeting));    End application-specific Layout section. EndPaint (HWnd, &ps);    break;}
  2. Applications typically handle many other messages, such as Wm_create and Wm_destroy. A simple and complete WndProc function is as follows:

    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {    paintstruct ps;    HDC hdc;    TCHAR greeting[] = _t ("Hello, world!");    Switch (message)    {case    wm_paint:        hdc = BeginPaint (hWnd, &ps);        Here your application are laid out.//for this introduction, we just print out "Hello, world!"        The top left corner. TextOut (HDC,            5, 5,            greeting, _tcslen (greeting));        END Application specific Layout section. EndPaint (HWnd, &ps);        break;    Case Wm_destroy:        postquitmessage (0);        break;    Default:        return DefWindowProc (hWnd, message, WParam, lParam);        break;    }    return 0;}


    Full code :

    #include <windows.h>
    #include <stdlib.h>
    #include <string.h>
    #include <tchar.h>

    Global variables

    The main window class name.static TCHAR szwindowclass[] = _t ("Win32app");

    The string, appears in the application's title Bar.static TCHAR sztitle[] = _t ("Win32 Guided Tour Application");

    HINSTANCE HInst;

    Forward declarations of functions included in this code module:
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

    int WINAPI WinMain (hinstance hinstance,
    HInstance hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    Wndclassex Wcex;

    wcex.cbsize = sizeof (wndclassex);
    Wcex.style = Cs_hredraw | Cs_vredraw;
    Wcex.lpfnwndproc = WndProc;
    Wcex.cbclsextra = 0;
    Wcex.cbwndextra = 0;
    Wcex.hinstance = hinstance;
    Wcex.hicon = LoadIcon (hinstance, Makeintresource (idi_application));
    Wcex.hcursor = LoadCursor (NULL, Idc_arrow);
    Wcex.hbrbackground = (hbrush) (color_window+1);
    Wcex.lpszmenuname = NULL;
    Wcex.lpszclassname = Szwindowclass;
    WCEX.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (idi_application));

    if (! RegisterClassEx (&wcex))
    {
    MessageBox (NULL,
    _t ("Call to RegisterClassEx failed!"),
    _t ("Win32 Guided Tour"),
    NULL);

    return 1;
    }

    HInst = hinstance; Store instance handle in our global variable

    The parameters to CreateWindow explained:
    Szwindowclass:the Name of the application
    Sztitle:the text that appears in the title bar
    Ws_overlappedwindow:the type of window to create
    Cw_usedefault, cw_usedefault:initial position (x, y)
    , 100:initial size (width, length)
    Null:the Parent of this window
    Null:this application does not has a menu bar
    Hinstance:the first parameter from WinMain
    Null:not used in this application
    HWND hwnd = CreateWindow (
    Szwindowclass,
    SzTitle,
    Ws_overlappedwindow,
    Cw_usedefault, Cw_usedefault,
    500, 100,
    Null
    Null
    HINSTANCE,
    Null
    );

    if (!hwnd)
    {
    MessageBox (NULL,
    _t ("Call to CreateWindow failed!"),
    _t ("Win32 Guided Tour"),
    NULL);

    return 1;
    }

    The parameters to ShowWindow explained:
    Hwnd:the value returned from CreateWindow
    Ncmdshow:the fourth parameter from WinMain
    ShowWindow (HWnd,
    nCmdShow);
    UpdateWindow (HWND);

    Main message loop:
    MSG msg;
    while (GetMessage (&msg, NULL, 0, 0))
    {
    TranslateMessage (&MSG);
    DispatchMessage (&MSG);
    }

    return (int) Msg.wparam;
    }

    //
    Function:wndproc (HWND, UINT, WPARAM, LPARAM)
    //
    Purpose:processes messages for the main window.//
    Wm_paint-paint the main window
    Wm_destroy-post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
    {
    Paintstruct PS;
    HDC hdc;
    TCHAR greeting[] = _t ("Hello, world!");

    Switch (message)
    {
    Case WM_PAINT:
    HDC = BeginPaint (hWnd, &ps);

    Here your application are laid out.//for this introduction, we just print out "Hello, world!"
    The top left corner. TextOut (HDC,
    5, 5,
    Greeting, _tcslen (greeting));
    End application-specific Layout section. EndPaint (HWnd, &ps);
    Break
    Case Wm_destroy:
    PostQuitMessage (0);
    Break
    Default
    Return DefWindowProc (hWnd, message, WParam, LParam);
    Break
    }

    return 0;
    }



Win32 manually creating a Windows window, a little note

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.