Windows Programming version 5th, Chapter 3rd window and message notes

Source: Internet
Author: User

AD First:

LIBUIDK Interface Library: When making QQ, 360 interface, you can find the most powerful interface library. Based on Directhwnd technology!

3.1 Creation of Windows
A simple Win32 program is as follows (assuming the project is named "HelloWin32", the following code is to use vc6.0 to create a "Win32 application" named HelloWin32, and select "A typical" Hello world " Application "After the created code is streamlined):

HelloWin32.cpp:Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"


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

int Apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
Register class.
Static TCHAR szclassname[] = _t ("HelloWin32");
Wndclassex Wcex;

wcex.cbsize = sizeof (wndclassex);

Wcex.style = Cs_hredraw | Cs_vredraw;
Wcex.lpfnwndproc = (WNDPROC) WNDPROC;
Wcex.cbclsextra = 0;
Wcex.cbwndextra = 0;
Wcex.hinstance = hinstance;
Wcex.hicon = LoadIcon (hinstance, (LPCTSTR) idi_hellowin32);
Wcex.hcursor = LoadCursor (NULL, Idc_arrow);
Wcex.hbrbackground = (hbrush) (color_window+1);
Wcex.lpszmenuname = (LPCSTR) idc_hellowin32;
Wcex.lpszclassname = Szclassname;
WCEX.HICONSM = LoadIcon (Wcex.hinstance, (LPCTSTR) idi_small);

RegisterClassEx (&WCEX);

Perform Application Initialization:
HWND hwnd = CreateWindow (Szclassname, _t ("The Hello Win32 App"), Ws_overlappedwindow, Cw_usedefault, 0, Cw_usedefault, 0, NULL, NULL, HINSTANCE, NULL);

if (hWnd = = NULL)
{
return 0;
}

ShowWindow (HWnd, ncmdshow);
UpdateWindow (HWND);

Haccel hacceltable = Loadaccelerators (hinstance, (LPCTSTR) idc_hellowin32);

Main message loop:
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
}

return msg.wparam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{
Paintstruct PS;
HDC hdc;
Static TCHAR szhello[] = _t ("Hello Win32");

Switch (message)
{
Case WM_CREATE:
Break

Case WM_PAINT:
HDC = BeginPaint (hWnd, &ps);
Todo:add any drawing code here ...
RECT RT;
GetClientRect (HWnd, &RT);
DrawText (hdc, Szhello, strlen (Szhello), &rt, Dt_center);
EndPaint (HWnd, &ps);
Break

Case Wm_destroy:
PostQuitMessage (0);
Break

Default
Return DefWindowProc (hWnd, message, WParam, LParam);
}

return 0;
}

We step into the code above, add a breakpoint in WndProc's WM_PAINT message, and when we execute the UpdateWindow (hWnd) code, we will find that we will go into WM_PAINT, but at this point, we have not entered the following message loop. We know that the message loop is fetching messages from the message queue, since the WM_PAINT message can be processed before the message loop is executed, stating that the WM_PAINT message is sent directly to the window without entering the message queue. The message loop calls WndProc, but not only the message loop can call Wndporc,windows itself. We can imagine the pseudo-code of CreateWindow:
HWND CreateWindow (...)
{
HWND hwnd = NULL;
Allocating window memory, generating windows
HWnd = ...;

CREATESTRUCT cs;
Initialize CS.
WndProc (hWnd, wm_create, 0, LPARAM (&CS)); CreateWindow Call WndProc without a message loop inside

Perform other operations

return hWnd;
}

The pseudo-code for DispatchMessage might look like this:
LRESULT dispatchmessage (const MSG *lpmsg)
{
Perform other operations

WndProc (Lpmsg->hwnd, Lpmsg->message, Lpmsg->wparam, Lpmsg->lparam);

Perform other operations

return 0;
}

In fact, as early as CreateWindow, Windows called WndProc and passed the wm_create message. Wm_create is the first message that is received by a program. Similarly, ShowWindow may trigger wm_size and Wm_showwindow messages.
Messages that are posted to Message Queuing are called "queue messages" and are not in the queue as "non-queue messages". Whether a message is a queue message or a non-queue message is not absolute, such as WM_PAINT can be a queue message (InvalidateRect is called after the window is displayed, a WM_PAINT message is added to the message queue). It can also be a non-queue message (the one above UpdateWindow), for programmers, most of the time, do not care about a message is a queue message, or non-queue messages, you only need to know that they are finally handed over to the WndProc window process, synchronized processing is possible.

When you specify the initial position and dimensions of the window at CreateWindow, the Cw_usedefault parameter indicates that the default value is used, which means that Windows shifts the upper-left position of the newly created window horizontally and vertically in the horizontal and vertical directions, respectively, by 1.

Windows Programming version 5th, Chapter 3rd window and message notes

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.