Windows program design: Zero-Basic self-learning _ 2_windows program display and update _ wm_paint message

Source: Internet
Author: User
Tags textout

Previous ArticleArticleInside, I copied the classic writing of a WindowsProgramI have a little perceptual knowledge of Windows program design, but I still have a little understanding of the program design. So I need to continue to learn about it and then read the classic book ......

The following is my summary. I will post it for you to see. I hope the heroes will give me some advice and point out the mistakes I have encountered, to help me improve my self-study .......

Hahaha ..........

4.1 Display and update
display area of Windows applications:
the window area allocated to the application by the system except the title bar, application border, menu bar, toolbar, scroll bar, and status bar.
display text and graphics in Windows:
1. You can draw the display area of the Application.
2. When some parts of the application are overwritten, windows does not save the covered area. When the program is removed, windows sends a message notification
the application needs to re-draw the covered area for display
3. Windows sends a wm_paint message to notify the application's window message handler, you need to re-draw the display area
4.2 wm_paint message:
generation time of the wm_paint message:
1. When you call the updatewindow function, windows sends a wm_paint message like an application, notifying the application that the display area needs to be re-painted.
2. When the user of the application moves the window, the hidden area in front of the window is re-visible
3. The user changes the window size and wndclass. cs_vredraw and cs_hredraw are set in style.
4. Call the scrollwindow or scrolldc function in the program to scroll through the display area.
5. Use invalidaterect or invalidatergn function in the program to generate /P>

Possible timing of wm_paint:
1. Windows removes the dialog box or message box that overwrites the application.
2. Drop down the menu and release it.
3. After the tooltip prompt is displayed
In the following cases, Windows restores the display area by saving the information.
1. move the cursor over the display area
2. Drag the icon over the display area
Processing Process:
The application redraws all information about the display area, and re-draws the display area or first draws the display area when the wm_paint message is received (when updatewindow is used)
4.3 invalid and valid rectangles
Invalid rectangle:
1. The area covered in the application display area is the invalid rectangular area. Invalid rectangle is also called invalid area.
2. Windows sends the wm_paint message to the application only because of the invalid rectangle. The application window will receive the wm_paint message only when a part of the display area fails.
Drawing Information Structure:
1. in windows, a drawing information structure is saved for each window. This structure contains the coordinates of the smallest rectangle surrounding the invalid area and other information.
2. If another area of the display area becomes invalid before the application processes the wm_paint message, Windows calculates a new invalid area that contains two areas, and save the change information in the drawing information structure.
3. Windows does not place multiple wm_paint messages in the message pair column.
Invalideaterect Function
1. The window message processing program can call invalidaterectangular to invalidate the rectangle in the display area.
2. When the invalidaterect function is called, if wm_paint already exists in the message queue, that is, an invalid region exists. Windows calculates a new invalid region. If the message queue does not have wm_paint
A wm_paint message is delivered to the message queue.
3. When the wm_paint message is received, the window handler/function can obtain coordinates of invalid areas.
4. Call getupdaterect to obtain coordinates of invalid regions at any time.
Tip:
When processing a wm_paint message, the window message processing program immediately becomes valid in the entire area after the beginpaint call. The program can also call the validaterect function to make any rectangle in the display area valid.
If the call has the effect of making the entire display area valid, the wm_paint in the current message queue will be deleted.
4. 4 GDI
GDI:
To draw a graph in the display area of the window, you can use the image device interface of Windows: GDI
Text drawing:
1. drawtext Function
2. textout Function
Prototype: textout (HDC, int X, int y, char * pstext, int ilength );
HDC: device content handle
X: The X coordinate at the beginning of the text.
Y: displays the Y coordinate at the beginning of the text.
Pstext: pointer to the string to be displayed
Ilength: length of the string to be displayed
Device content:
1. The device content handle is the pass of the GDI function. Through the device content handle, the program can draw in the display area.
2. device content DC: it is the Data Structure Stored in GDI. The device content is related to a specific display device. For a system desktop, the device content is always related to a specific window on the display.
3. Some values in the device content are graphic attributes that define the details of the GDI drawing function.
The process of drawing device content:
1. When the program needs to plot, the device content handle must be obtained. After obtaining the handle, the Windows system will enter the internal device content structure with the defined property values.
2. You can call different GDI functions to obtain the property values of device content.
3. You can call the GDI function to change the property value of the device content.
4. drawing in the display area using other GDI functions
5. After the program draws in the display area, the content handle of the device must be released, and the released handle cannot be used again.
6. The program must obtain and release the device content handle during processing a single message.
Tip:
Except for the device content created by calling createdc, the program cannot store the content handles of other devices between two messages.
Get device content handle:
Method 1: used to process the wm_paint message
1. beginpaint and endpaint Functions
The beginpaint and endpaint functions require a window handle (when Windows calls an application window handler, it is passed as a parameter to the application window handler)
And paintstruct structure address as the parameter.
The beginpaint function returns a device content handle associated with the window.
The prototype is as follows:
HDC beginpaint (hwnd, paintstruct *)
The endpaint function releases the device content handle obtained by the beginpaint function. Its prototype is as follows:
Endpaint (hwnd, paintsturct *)
2. wm_paint Message Processing Process
Prestep:
HDC;
Paintstruct pS;
Step 1: Call the beginpaint function to obtain the device content handle associated with the window;
HDC = beginpaint (hwnd, & PS );
Step 2: Use the GDI function for plotting;
Step 3: Release the device content handle obtained by the beginpaint function:
Endpaint (hwnd, & PS );
Endstep: Return 0; // after each message is processed, return to the Windows system. If the window callback function is successfully executed, return 0.
3. wm_paint message processing structure:
Case wm_paint:
HDC = beginpaint (hwnd, & PS );
Call other GDI Function
Endpaint (hwnd, & PS );
Return 0;
4. If you do not process the wm_paint message in the window message processing function, you must pass the wm_paint message to the defwindowproc function for processing.
Below Code Process the wm_paint message:
Case wm_paint:
Beginpaint (hwnd, & PS );
Endpaint (hwnd, & PS );
Return 0;
Defwindowproc prototype:
Defwindowproc (hwnd, MSG, wparam, lparam); // basically the same structure as the window message processing function
5. Windows generates the wm_paint message because a certain range in the display area becomes invalid. If you do not call beginpaint or endpaint or validaterect, Windows
It is not because the invalid region is valid. On the contrary, Windows will send another wm_paint message and keep sending it.
Drawing Information Structure paintstruct:
1. Drawing Information Structure Definition:
Typedef struct tagpaintstruct
{
HDC; // device content handle
Bool ferase; // whether the background of the invalid rectangle has been erased
Rect rcpaint;
Bool frestore;
Bool fincupdate;
Byte rgbreserved [32];
} Paintstruct;
2. windows call
When the program calls the beginpaint function, Windows will fill in each member of the structure, the application uses only the first three fields, others are used in windows.
Fill in the PS. ferase field when calling the beginpaint function to indicate whether the background of the invalid region has been erased:
PS. ferase = false (0) indicates that the background of the invalid region has been erased. In Windows, the image brush specified by wndclass. hbrbcakground is used to erase the background.

3. If the program calls invalidaterect to invalidate the rectangle in the display area, the last parameter of the invalidaterect function specifies whether to erase the background. If this parameter is set to false
, Windows does not erase the background, and PS. ferase = true is set to non-zero after beginpaint is called.
to redraw A wm_paint message outside the invalid region, call the invalidaterect function:
Exp:
invalidaterect (hwnd, null, true);
call the invalidaterect function before the beginpaint function to make the entire display area invalid and erase the background. However, if the last parameter is false, the background is not erased, window
original information is retained.
4. rect rcpaint is a rect structure. The rcpaint member in the paintstruct structure defines the boundary of the invalid region.

The previous content briefly describes how to use the beginpaint and endpaint functions to process the wm_paint message. If you do not understand it, please forgive me.

The prototype of some of the above functions does not specify the type of the returned value. You can check it by yourself in the compiling environment. Because I am a little lazy, I will not check it.

Hahahaha ................ in the past, the program design in Windows had the foundation of C. In addition, I learned a lot of windows to define various API functions, and I also understood some operating mechanisms, even a novice Programmer (like me) you can also implement a simple application ....................

I found it better to use livewrite .......

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.