Windows program design. window.

Source: Internet
Author: User
Tags drawtext

First Windows window

1#include <windows.h>2 /*displays "Hello, world!" in the client area*/3 4LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);//window procedure Functions5 6 intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance,7PSTR szCmdLine,inticmdshow)8 {9     StaticTCHAR szappname[] = TEXT ("Hellowin");//window class nameTen     Char string[] ="Hello world!"; OneHWND hwnd;//window Handle AMSG msg;//Message Structure -Wndclass Wndclass;//Defining a window class struct-body variable -     /*The following code fills the window class information*/ theWndclass.style = Cs_hredraw | Cs_vredraw;//window Style -Wndclass.lpfnwndproc = WndProc;//Procedure Functions -Wndclass.cbclsextra =0;//Extended Fields -Wndclass.cbwndextra =0;//Extended Fields +Wndclass.hinstance = hinstance;//Current instance handle -Wndclass.hicon = LoadIcon (NULL, idi_application);//Set Program icon +Wndclass.hcursor = LoadCursor (NULL, Idc_arrow);//Set the mouse AWndclass.hbrbackground = (hbrush) getstockobject (White_brush);//fill the background with white atWndclass.lpszmenuname = NULL;//Menu -Wndclass.lpszclassname = Szappname;//class name -                                        /*Register window class*/ -     if(! RegisterClass (&wndclass)) -     { -MessageBox (NULL, TEXT ("This program requires Windows nt!"), in Szappname, mb_iconerror); -         return 0; to     } +     //Creating Windows Create window -hwnd = CreateWindow (Szappname,//window class name theTEXT ("The Hello program"),//Window caption Title *Ws_overlappedwindow,//window style style $Cw_usedefault,//initial x position Horizontal axis OriginPanax NotoginsengCw_usedefault,//initial y position ordinate Origin -Cw_usedefault,//Initial x Size theCw_usedefault,//Initial y size +Null//parent window Handle handle ANull//Window menu handle handle theHINSTANCE,//Program instance handle current instance handle +NULL);//creation parameters A pointer passed to the value of a window wm_create message is generally null - ShowWindow (hwnd, icmdshow); $ UpdateWindow (HWND); $     //The message loop, which has been parked here, exits the message loop to indicate that the program is over. -      while(GetMessage (&msg, NULL,0,0)) -     { theTranslateMessage (&AMP;MSG);//translation -DispatchMessage (&AMP;MSG);//DistributionWuyi     } the     returnMsg.wparam; - } Wu /*message-handling functions*/ - LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) About { $ HDC hdc; - paintstruct PS; - rect rect; -     Switch(message) A     { +      CaseWM_PAINT: theHDC = BeginPaint (hwnd, &PS); -GetClientRect (hwnd, &rect); $DrawText (HDC, TEXT ("Hello world!"), -1, &Rect, theDt_singleline | Dt_center |dt_vcenter); theEndPaint (hwnd, &PS); the         return 0; the     //send end request, inside parameter is exit code -      CaseWm_destroy: inPostQuitMessage (0); the         return 0; the     } About     //Call the default procedure function the     returnDefWindowProc (hwnd, message, WParam, lParam); the}

The first is to tell the system by WNDCLASS this structure that we are going to create a window.

1YpedefstructTagwndclassa {2 UINT style;3 WNDPROC Lpfnwndproc;4     intCbclsextra;5     intCbwndextra;6 hinstance hinstance;7 Hicon Hicon;8 hcursor hcursor;9 Hbrush Hbrbackground;Ten LPCSTR Lpszmenuname; One LPCSTR lpszClassName; A} Wndclassa, *pwndclassa, near *npwndclassa, far *lpwndclassa;

Set different fields within the structure, call the RegisterClass function to tell the system to register a window type like this.

The next step is to create a window type that was just registered by calling the function CreateWindow.

1 #defineCreatewindowa (Lpclassname, Lpwindowname, dwstyle, X, Y,2 nwidth, nheight, hWndParent, HMenu, HINSTANCE, Lpparam)3Createwindowexa (0L, Lpclassname, Lpwindowname, dwstyle, X, Y,4 nwidth, nheight, hWndParent, HMenu, HINSTANCE, Lpparam)5 #defineCreatewindoww (Lpclassname, Lpwindowname, dwstyle, X, Y,6 nwidth, nheight, hWndParent, HMenu, HINSTANCE, Lpparam)7CREATEWINDOWEXW (0L, Lpclassname, Lpwindowname, dwstyle, X, Y,8 nwidth, nheight, hWndParent, HMenu, HINSTANCE, Lpparam)9 #ifdef UNICODETen #defineCreateWindow CREATEWINDOWW One #else A #defineCreateWindow Createwindowa - #endif // ! UNICODE

The created window is not yet displayed, which is called the function ShowWindow to display, as well as UpdateWindow to update.

ShowWindow (    _in_ hwnd hwnd,    int  ncmdshow); UpdateWindow (    _in_ hwnd hwnd);

Message loops. The GetMessage function obtains a message from the message queue. The TranslateMessage function transfers some keyboard messages. The DispatchMessage function sends a message to the window message handler.

Then look at WndProc inside this function. BeginPaint starts drawing the window. GetClientRect gets the size of the window display area. DrawText displays the string. EndPaint ends the drawing window. PostQuitMessage Inserts an "exit program" message in the message queue. DefWindowProc performs the default message processing.

Programs typically do not call window message handlers directly, and window message handlers are usually called by Windows itself. By calling the SendMessage function, the program can call its own window message handlers directly.

Process the message.

The first message processed by WndProc is WM_PAINT. This message notifies the program when a portion of the window display area is displayed or all becomes "invalid", so that it must "update the screen". The style field of the WNDCLASS structure is set to flag Cs_hredraw and Cs_vredraw, which indicates that the display contents of the display area become invalid again when the window is resized, the window is minimized, and the window is moved.

GetClientRect (hwnd, &rect);

The first argument is a handle to the program window. The second parameter is an indicator that points to the rectangle structure of a rect-shaped state. The structure has four long fields, left, top, right, and bottom, respectively. GetClientRect set these four fields to the size of the window display area. The left and top fields are typically set to the 0,right and bottom fields to the width and height of the display area (pixel points).

DrawText (hdc, TEXT ("Hello world! "),-1, &rect,            | Dt_center | Dt_vcenter);

DrawText can output text (as its name suggests). Since the function is to output text, the first parameter is the device content handle returned from BeginPaint, the second argument is the text to be output, the third parameter is-1, indicating that the string is terminated in bytes 0.

DrawText the last parameter is a series of bit flags, which are defined in WINUSER.H (although the DrawText is called as a GDI function because of the effect of its display output, it does become part of the user module because of the fairly advanced drawing capabilities. This function is described in/platform sdk/graphics and multimedia services/gdi/fonts and text). The flag indicates that the text must be displayed on one line, both horizontally and vertically in the center of the rectangle specified by the fourth parameter. Therefore, this function call will let the string "hello world!" Appears in the center of the display area.

Once the display area becomes invalid (as happens when the size is changed), WndProc receives a new WM_PAINT message. WndProc gets the changed window size by calling GetClientRect and displays the text in the center of the new window.

 Case    Wm_destroy:        postquitmessage (0);

The WM_DESTROY message indicates that Windows is closing the window according to the user's instructions. Occurs when you click the Close button or when Close is selected on the program's system menu.

return DefWindowProc (HWND, message, WParam, LParam);

The wparam field of a struct is the value passed to the PostQuitMessage function (usually 0). Then the return narrative exits the WinMain and terminates the program.

Windows program design. window.

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.