In-depth analysis of WTL - Win32 model

xiaoxiao2021-03-06  58

WTL is an abbreviation for Windows Template Library. Initially, WTL is an SDK example developed by Microsoft ATL (Active Template Library) team members. Mainly based on ATL's package for Win32 API. After 2.0, the function is gradually improved, which has become a full support window framework (Windows Framework). Compared with the MFC, there is no MFC perfect. For example, the MFC supports the DOC / View architecture, and WTL does not support. At the same time, WTL also does not have Microsoft's official support. However, WTL is based on template, and its application is only 24KB, and it is not like MFC, depending on the DLL (MFC requires MFC42.dll). WTL series articles have been deeply analyzed for WTL. I hope that you can easily have a deep understanding of WTL, so that you can develop high-quality Windows applications.

Win32 thread model

For the sake of future discussions, first look at the Win32 thread model. A Win32 application (or process) is composed of one or more concurrent threads, where the first start-up thread is called the main thread. Win32 defines two types of threads, interface threads, and working threads. Each process of Win32 can have one or more interface threads and / or multiple working threads. The interface thread has one or more windows that have a message queue and other elements that belong to the interface thread. The working thread is a general thread, it does not have a window, no message queue. The interface thread usually has one or more windows. When a window has a message, the interface thread calls the corresponding window function (Windows Process) to handle the event. Since a message loop is handled by its interface, it is not necessary to send messages to which the message is to be sent. Therefore, Windows guarantees the synchronization problem between the thread. For working threads, synchronization between threads must be implemented by programmers. Avoid death locks and competition as much as possible.

Win32 application model

Win32 applications can be divided into two major classes: console applications and window interface programs (Windows GUI Application). The entry function of the console program is Main (), the entrance function of the window interface program is WinMain (). The entry function is the starting point of the main thread of the program. The development framework (Framework) discussed here is a for window interface programs. Window interface programs are usually divided into the following categories: SDI, MDI, Multi-SDI, and Dialog applications. SDI (Single Document Interface) application typically has only one main window (usually a frame window, Frame Window). The frame window contains menus, toolbars, status bars, and customer workspaces called views. Multi-SDI (Multiple Threads SDI) application has a framework main window. For example, after the IE browser, use the File / New / Window command, another IE window will appear. MDI (MULTIPLE Document Interface) has a primary frame window, but there are multiple sub-frame windows. Each sub-window has its own vision. Dialog applications are based on dialogments. Typically a simple SDI application consists of two functions. One is the application portal function WinMain () and the other is the window function of the application window. Program (main thread) starts from the entrance function. In this function, first is registered and create a main window. Then, start the message loop. In the message loop, the message is sent to the window function according to different messages. When the message is an exit message, the inlet function exits the message loop and then end the program. Below is a simplest Windows interface application. // Application Portal Function

Int apientry Winmain (Hinstance Hinstance,

Hinstance Hprevinstance,

LPSTR LPCMDLINE,

INT ncmdshow)

{

MSG msg;

// 1. Registration window

WNDCLASSEX WCEX;

Wcex.cbsize = sizeof (wndclassex);

WCEX.Style = CS_HREDRAW | CS_VREDRAW;

WCEX.LPFNWndProc = (WndProc) WndProc; / / Specify window functions

Wcex.cbclsextra = 0;

Wcex.cbWndextra = 0;

WCEX.HINSTANCE = HINSTANCE

WCEX.HICON = Loadicon (Hinstance, (LPCTSTR) IDI_HELLOWORLD);

Wcex.hcursor = loadingcursor (null, idc_arrow);

Wcex.hbrbackground = (Hbrush) (Color_Window 1);

WCEX.LPSZMENUNAME = (LPCSTR) IDC_HELLOWORLD;

WCEX.LPSZCLASSNAME = SzWindowClass;

Wcex.hiconsm = loading (Wcex.hinstance, (LPCTSTR) IDI_SMALL);

RegisterClassex (& WCEX);

// 2. Create a window of this type

Hwnd hwnd;

HWnd = CREATEWINDOW (SzwindowClass, Sztitle,

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0,

CW_USEDEFAULT, 0, NULL, NULL,

Hinstance, NULL;

IF (! hwnd) Return False;

// 3. Display the window according to NCMDSHOW

ShowWindow (HWND, NCMDSHOW);

UpdateWindow (HWND);

// 4. Start the message loop, send the message to the corresponding window function

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

Return msg.wparam;

}

// Window function

LResult Callback WndProc (HWND HWND,

Uint Message,

WPARAM WPARAM,

LParam LPARAM)

{

Paintstruct PS;

HDC HDC;

Char * szhello = "Hello, World!";

Switch (Message)

{

Case WM_Paint:

HDC = BeginPaint (HWND, & PS);

RECT RT;

GetClientRect (hwnd, & rt);

DrawText (HDC, Szhello, Strlen (Szhello), & RT, DT_CENTER

Endpaint (hwnd, & ps);

Break;

Case WM_DESTROY:

PostquitMessage (0); // Exit application

Break;

DEFAULT:

Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);

}

Return 0;

}

The execution process of the above program is as follows: 1. The Registration window class must provide a string of the identification window class when using CreateWindWo () or CREATEWINDOWEEX (). This window class defines the basic properties of some windows. One of the important tasks is to provide a window function to the operating system. This function is a callback function that handles messages sent to the window. In the above program, only two messages are handled simply. One is to draw "Hello World." String to the window area. The other is to send an "exit application" message to the application when the window is revoked. 2, create a window 3, display window 4, start message loop, distribute and process messages.

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

In the above message loop code, the getMessage () takes a message from the message queue of the thread. If the message is 0 (POSTQUITMESSAGE (0) "sent when the" WM_DESTROY "message is handled by the window function, the message loop will be exited. Then call the translateMessage () translation message. After the translation, the DISPATCHMESSAGE () is then distributed to the corresponding window function for processing. (In fact, DispatchMessage () calls the message as the window function of the corresponding window as the parameter, which is the substantive distribution).

转载请注明原文地址:https://www.9cbs.com/read-85728.html

New Post(0)