1.3 Writing The window Procedure (process of writing windows)

Source: Internet
Author: User

Authoring window procedures (Writing the Windows Procedure)

The DispatchMessage function is called as a window procedure for the Message target window. The window procedure has the following signature (signature).

C++

LRESULT CALLBACK WindowProc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM);

4 Parameters:

    • An hwnd is a handle to the corresponding window.
    • umsg is the message code; For example, the WM_SIZE message indicates that the window is resized.
    • WParam and LParam contain additional data related to the message. The exact meaning depends on the message code.

LRESULT is an integer value that your program returns to Windows. It contains the program's response to a particular message. The meaning of this value depends on the message code. CALLBACK is the calling convention for a function.

A typical window procedure is a large switch statement that opens the message code. Add a case for each message that you want to process.

C++

Switch (umsg) {case//  Handle window resizing   //  etc  }

Additional data for the message is contained in the lParam and wParam parameters. Both parameters are integer values of the pointer width (32-bit or 64-bit) size. Their meaning depends on the message code (umsg). For each message, you need to look up the message code on MSDN and convert the parameter to the correct data type. Usually the data is either a numeric value or a pointer to a struct. Some messages do not have any data.

For example, the documentation for the WM_SIZE message indicates that:

    • WParam is a flag that indicates whether the window is minimized, maximized, or resized.
    • The LParam contains the new width and height of the window, and the 16-bit value is packaged into a 32-bit or 64-bit number. You will need to perform some bit shifts (bit-shifting) to get these values. Fortunately, the header file WinDef.h contains the secondary macros that perform this operation.

A typical Windowing procedure (window procedure) handles dozens of messages, so it can grow quite long. One way to make your code more modular is to put the logic that handles each message in a separate function. In the window procedure, convert the wparam and lparam parameters to the correct data type and pass these values to the function. For example, to process a wm_size message, the window procedure looks like this:

C++

LRESULT CALLBACK WindowProc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {Switch(umsg) { Casewm_size: {intwidth = loword (LParam);//Macro to get the Low-order word.            intHeight = HiWord (lParam);//Macro to get the High-order word. //Respond to the message:OnSize (hwnd, (UINT) wParam, width, height); }         Break; }} voidOnSize (HWND hwnd, UINT flag,intWidthintheight) {    //Handle Resizing}

LoWord and HiWord macros get 16-bit width and height values from lParam . (You can find the details of each message code in the MSDN documentation.) The window procedure extracts the width and height, and then passes the values to the OnSize function that you wrote.

Default message processing (handling)

If a specific message in the window procedure is not processed, the message parameters are passed directly to the DefWindowProc function. This function performs the default action for the message, which varies depending on the message type.

C++

    return DefWindowProc (hwnd, umsg, WParam, LParam);

Avoid bottlenecks in window processes (avoiding Bottlenecks in Your window Procedure)

When the window procedure is executed, it blocks any other messages that are created on the same thread by the (block) window. Therefore, avoid lengthy processing in the window process. For example, suppose your program opens a TCP connection and waits indefinitely for the server to respond. If you do this during a window, your UI will not respond until the request is complete. During this time, the window cannot handle mouse or keyboard input, redraw itself, or even shut down.

Instead, you should move your work to another thread, using one of the windows built-in multitasking facilities:

    • Create a new thread. Create a new thread
    • Use a thread pool. Using the thread pool
    • Use asynchronous I/O calls. Using asynchronous I/O calls
    • Use asynchronous procedure calls. Using asynchronous program calls

1.3 Writing The window Procedure (process of writing 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.