Deep Delphi (3)

zhaozj2021-02-17  48

Deep Delphi (3)

Windows Message Mechanism

By Machine

Do you have a very strange why I haven't said Delphi's control? However, no need to worry, the content of the in-depth control will appear soon, but before this, you have to understand the mechanism for Windows graphics interface - Windows message mechanism. Friends who have used Delphi know that Delphi is a programming environment of the true face-to-object, but not only this, the Delphi's object-oriented mechanism is simple to build Delphi code on the Windows message mechanism, not like VB, VFP Calling DLL, OCX, by looking at the source code of the Delphi control, you can know how the whole mechanism organizes, and you can complete these controls all over the whole, because they just write with the Delphi code, not exist I can't see something in the DLL. So what is the so-called Message mechanism? I still remember that when I learned BASIC, there is no thing such as an event. The entire program is drawn with a flow chart. When the program requires the keyboard input, the entire program stops waiting for input, then according to input. thing. This is nothing wrong, but when the graphical interface, the situation is different, the mouse input has become a big problem, and under the multi-task system such as Windows, it is impossible to let a program constant test device status. Sub to get the input. In short, the message mechanism under Windows is completely different, even if there is still a lot of different places even with a simple interrupt event. Simply put, a thread will automatically generate a message queue when creating a form, but the form is not necessary, you can create a message queue through other ways. Then, other programs, or Windows system itself can send messages to this thread to its message queue, inform this thread, there is something that happens, this is the so-called event. Each process can use the GetMessage function to get the first message in its message queue, and getMessage will automatically remove this message from the message queue, and of course you can specify whether you don't delete the message. Listening may still be imagined, then look at the following example:

Program Sample3;

Uses Windows, Messages;

VAR MSG: TMSG;

Begin PostMessage (0, WM_USER, 0, 0); // First enforce message queue PeekMessage (MSG, 0, WM_USER, WM_USER, 0); // Then here is the so-called message loop, only when receiving the message from WM_QUIT GetMessage () will only return false while getMessage (MSG, 0, 0, 0) do begin end; // Here you can do work End before the program ends (after WM_QUIT).

After this program is running, you will not do anything, and you will also ignore the message sent to it. In addition to WM_QUIT, because getMessage has a feature, when you receive other messages, the return value of GetMessage is True. When I receive WM_QUIT, the return value is false, so the message loop is broken. When Windows is closed, Windows automatically sends a message of WM_QUIT to the primary thread of this program, and then the program exits. The main body of most procedures is this look, there is a message loop, that is, each program is constantly using GetMessage to get new messages, then process, and re-end until WM_QUIT is received. And where it is, getMessage is called, if there is no message in the message queue, the function will not return immediately, but to transfer the thread into the sleep state, so the thread will not waste CPUs because of constant loops time. After receiving a new message, the thread will re-wake up, and getMessage puts the received message to a TMSG type parameter returned, so the program can handle this message. Ok, how does this message mechanism combine with the form program? In other words, if the program generates a form, how can the program get the user's input message through this message mechanism? This is about to start from the process of creating a form. The following is a more complicated example: program sample4;

Uses Windows, Messages;

Var Msg: TMSG; WC: TwndClass; // registerclass () The parameter hWnd: Thandle; // Main Fact

Const classname = 'mainwclass';

Function MainwndProc (Handle: Thandle; Msgid: uint; wparam, lparam: integer; stdcall; begin result: = 1; case msgid of wm_close: begin // Close the message generated by the form if messageBox (Handle, ' Close this program? ',' Example program -4 ', MB_ICONQUESTION OR MB_YESNO) = iDYES THEN DESTROYWINDOW (HWND) Else Result: = 0; EXIT; End; WM_DESTROY: Begin // DestroyWindow () Generated PostquitMessage (0 ); END; END; // The remaining message is handed over to the Windows preset processing function, such as the WM_NCPAINT message of the draw form, etc., the WM_NCPAINT message, etc., etc.

Begin // First use the registerclass () to register the form, this is not the class in the Delphi data type! wc.style: = cs_hredraw or cs_vredraw; wc.lpfnwndproc: = @mainwndproc; // message processing function address wc.hinstance: = Hinstance; // The handle of the program is also the base address wc.hicon: = loading (0, PCHAR (IDI_APPLICATION); wc.hcursor: = loadingCursor (0, IDC_ARROW); // Icon wc.hbrbackground: = getStockObject (white_brush); // Background brush wc.lpszclassName: = classname; // front definition constant IF RegisterClass (WC) = 0 THEN HALT (0); hWnd: = CREATEWINDOWEX (0, classname, // Just registered class name 'sample', // Form Title WS_OVERLAPPEDWINDOW, // Form There is title bar, system Menu, Maximum Menu, and Tensile Border Integer (CW_USEDEFAULT), Integer (CW_USEDEFAULT), Integer (CW_USEDEFAULT), INTEGER (CW_USEDEFAULT), 0, 0, Hinstance, NIL); if Hinstance, NIL); if Hinstance, NIL); if hind = 0 THEN HALT (0); ShowWindow (hwnd, cmdshow); UpdateWindow (hwnd); while getMessage (MSG, 0, 0, 0) DO BEGIN TRANSLATEMESSAGE (MSG); DispatchMessage (MSG); // This API assists a message to the corresponding form message processing function End; EXITCODE: = msg.wparam; end.

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

New Post(0)