Windows Programming-Windows

Source: Internet
Author: User
Tags getmessage

Note the MessageBox function window name must use the text function to contain

Handle: Returns the flag number that identifies the resource

Common handle: Window handle (HWND) icon handle (HICON), cursor handle (hcursor), brush handle (Hbrush)

CS window category CW Build window DT draw text IDI diagram ID IDC cursor ID MB message box SND Sound WM window message ws window style

#include <Windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain

(hinstance hinstance,//application's instance handle

HINSTANCE hprevinstance,//handle to the previous instance of the application

PSTR szcmdline,//Command line argument string

int icmdshow)//How the program displays the window when it is initialized

{static TCHAR szappname[] = TEXT ("Hellowin");//define String

Wndclass Wndclass; Defining a Wndclass window class object

Wndclass.style = cs_hredraw| Cs_vredraw; Specify window type style

/*cs_hredraw for width change cs_vredraw for height change cs_noclose Disable the Close command of the system menu cs_dblclks send a mouse double-click Message to the window we want to remove a style that the variable has, You can then do a reverse (~) operation on the style identifier, and then work with the variable (&) to achieve it.  For example, to remove the Cs_vredraw style that the previous style variable has, you can write code: Style=style & ~ Cs_vredraw. */

Wndclass.lpfnwndproc = WndProc; A pointer to a window procedure function//lpfnwndproc a function pointer to a window function in this case

Wndclass.cbclsextra = 0; Storage space occupied by class variables Wndclass.cbwndextra = 0; Storage space occupied by instance variable//default setting is 0

Wndclass.hinstance = hinstance; The instance handle of the program that contains the window procedure Wndclass.hicon = LoadIcon (NULL, idi_application); Specifies the window class cursor handle

/*1. You can load standard icons provided by the Windows system, or you can load user-created icons and return system-assigned handles. 2. If the system icon is loaded, the first parameter is set to NULL, and the second parameter is the icon Resource 3. Set the resource as the ID, where idm_ is the menu resource, and Idi_ is the icon resource Idb_ as the button resource */

Wndclass.hcursor = LoadCursor (NULL, Idc_arrow); Specifies the window class icon handle, calling the same method as above

Wndclass.hbrbackground = (hbrush) getstockobject (White_brush); Specifies the window class brush handle

/*1.hbrush for type conversion 2.white_brush to white brush, also black_brush for black brush */

Wndclass.lpszmenuname = NULL; Specify the name of the menu resource

Wndclass.lpszclassname = Szappname; Specify window class name

Registration window//The function has only one parameter, which is a pointer to the window class object that was designed in the previous step.

If the function succeeds, the return value is an atom that uniquely identifies the registered class, and if the function fails, the return value is 0

if (! RegisterClass (&wndclass)) {MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, mb_iconerror);//  can omit return 0; }

If the window is created successfully, the CreateWindow function returns the handle assigned to the window by the system, otherwise, NULL is returned.

Note that you should define a window handle variable to receive the handle value returned after the window is created before creating the window

HWND hwnd; Defining Handle variables

hwnd = CreateWindow//Create window

(Szappname,//class name, specifying the class to which the window belongs

TEXT ("The Hello Program"),//window name

Ws_overlappedwindow,//Window style

Cw_usedefault,//The initial x-coordinate of the upper-left corner of the window relative to the upper-left corner of the screen

Cw_usedefault,//y

Cw_usedefault,//width of window

Cw_usedefault,//height of the window

NULL,//handle to the parent window of a child window

NULL,//menu handle

HINSTANCE,//instance handle of the application that created the Window object

NULL); Create additional parameters specified by the window

ShowWindow (hwnd, icmdshow);//Display window

/* Call the function ShowWindow to display the window, and the prototype declaration of the function is as follows: BOOL ShowWindow (HWND hwnd,//Handle to Window int ncmdshow//show State); The ShowWindow function has two parameters, the first parameter HWND is the window handle returned after the window was successfully created in the previous step, and the second parameter ncmdshow specifies the state of the window display, which is commonly used in the following ways. N sw_hide: Hides the window and activates other windows. N sw_show: Activates and displays the window in its original position at the original size. N sw_showmaximized: Activates the window and maximizes its display. N sw_showminimized: Activates the window and minimizes the display. N sw_shownormal: Activates and displays the window. If the window is a minimized or maximized state, the system restores it to its original size and size. The application should specify this flag the first time the window is displayed */

UpdateWindow (HWND);//Update window

MSG msg;//defining message structure body variables

The GetMessage function returns 0 only if the Wm_quit message is received, and then exits

while (GetMessage (&msg, NULL, 0, 0)) {

TranslateMessage (&MSG);

TranslateMessage converts a virtual key message to a character message, and the character message is posted to the calling thread's message queue

DispatchMessage (&MSG);

The Dispathmessage function actually passes the message back to the operating system, and the message is processed by the operating system call Function procedure}

/* After creating a window, displaying a window, updating a window, we need to write a message loop that constantly pulls the message out of the message queue and responds. To get the message out of the message queue, we need to call the GetMessage () function,

The prototype of the function is declared as follows:

BOOL GetMessage (

Lpmsg lpmsg,//address of structure with message

HWND hwnd,//Handle of window

UINT Wmsgfiltermin,//First message

UINT Wmsgfiltermax//Last message);

The parameter lpmsg points to a message (MSG) struct, and the message information GetMessage out of the thread's message queue is saved in the struct object. The parameter hWnd specifies which window to receive the message. Typically we set it to null to receive window messages belonging to all windows of the calling thread. The parameter wmsgfiltermin specifies the minimum value of the message to get, typically set to 0. The parameter Wmsgfiltermax specifies the maximum value of the message to get. If both Wmsgfiltermin and Wmsgfiltermax are set to 0, all messages are received. */

return msg.wparam;

The parameter value of the PostQuitMessage function (see Code, usually 0) will be used as the wparam parameter of the WM_QUIT message (see note ⑦).

At this point the parameter wparam is 0. You receive a wm_quit message loop that terminates naturally, and then the program executes the following statement:

return msg.wparam; That is, return a value of 0 normal exit WinMain and terminate the program.

}

LRESULT CALLBACK WndProc

(HWND hwnd,//Window handle UINT

message,//Messages code

WPARAM WPARAM,//Two additional parameters for message code

LPARAM LPARAM) {

HDC hdc;

Paintstruct PS;

Rect rect;

Switch (message) {

Case WM_CREATE://Creation Process

PlaySound (TEXT ("Hellowin.wav"), NULL, Snd_filename | Snd_async);

return 0;

Case WM_PAINT:

HDC = BeginPaint (hwnd, &PS);

GetClientRect (hwnd, &rect);

DrawText (HDC, TEXT ("Hello, Windows 98!"),-1, &rect, Dt_singleline | Dt_center | Dt_vcenter);

EndPaint (hwnd, &PS);

return 0;

Case wm_close://off State

if (Idyes = = MessageBox (hwnd), TEXT ("Is it really over?") "), TEXT (" message "), Mb_yesno))

{DestroyWindow (HWND); }

/*1.int MessageBox (HWND hwnd,lpctstr lptext,lpctstr lpcaption,uint utype); The HWND represents the owning window, the second is the display message content, the third is the dialog box title, and the fourth bit button type Idabort:abort button is selected. The Idcancel:cancel button is selected.     The Idignore:ignore button is selected. The Idno:no button is selected. The Idok:ok button is selected. The Idretry:retry button is selected.    The Idyes:yes button is selected. */

return 0;

Case wm_destroy://Destruction

PostQuitMessage (0);

return 0; }

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

}

/* Enter the team message and do not enter the team message (understanding): In the news and not into the team in fact, in Windows messages can be divided into "team" and "not in the team." The incoming message is returned and assigned to the window procedure in the program's message loop by Windows into the program message queue. Messages that do not enter the queue are sent directly to the window procedure when Windows calls the window. That is, the incoming message is "sent" to the message queue, and the message that does not enter the team is sent directly to the window procedure. In any case, the window process will get all the messages from the window-including the incoming team and the non-incoming team. The window procedure is the "Message Center" of the window. The incoming message is basically the result of the user input, given in the form of keystrokes (such as wm_keydown and WM_KEYUP messages), keystrokes (WM_CHAR), mouse movements (wm_mousemove), and Mouse keys (WM_LBUTTONDOWN). The incoming message also contains a clock message (Wm_timer), a refresh message (WM_PAINT), and an exit message (Wm_quit). Other messages are not in the queue. In many cases, non-incoming messages come from calling a specific Windows function. For example, when WinMain calls CreateWindow, Windows creates a window and sends a WM_CREATE message to the window procedure during processing. When WinMain calls ShowWindow, Windows sends wm_size and WM_SHOWWINDOW messages to the window procedure. When WinMain calls UpdateWindow, Windows sends a WM_PAINT message to the window procedure. The incoming message signal from the keyboard or mouse input can also appear in the queue message. For example, when you select a menu item with the keyboard or mouse, the keyboard or mouse message is entered, and the WM_COMMAND message that indicates that the menu item is selected may not be in the team.

Windows Programming-Windows

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.