Windows program design: Zero-Basic self-learning _ 3_display and update of Windows program _ getting device content handle _ drawing display area during non-wm_paint message

Source: Internet
Author: User
Tags control characters textout

After reading the books for a few days, I found that the book is too thick and more than 1600 pages. It is the thickest book I have read carefully, I don't know how the author and the translator stick to it. So many texts are written. I feel very tired when I am studying myself because I have to do some work and have little connection with the computer, so the learning progress is a little slow .......... however, this is my interest. Although I feel tired, I am happy to learn it... hey

WindowsProgramDisplay and update the wm_paint Message Processing Mechanism in the window, and continue to talk about the topic that was not completed this time ........

Draws the display area during a non-wm_paint message

During application compilation, sometimes you do not need to draw a display area, but only want to get information about some device content; or we want to paint during non-wm_paint message processing.
In this case, another method is required. We can handle the problem in the following way:
1. Get the device content handle and release the device content handle:
We can use the following two API functions to obtain the device content handle and release the device content handle:
Get device content handle: getdc
Prototype:
HDC getdc (hwnd );
Release device content handle: releasedc
Prototype:
Int releasedc (hwmd, HDC)
2. Tip:
Like beginpaint and endpaint, The getdc and releasedc functions must be used in pairs. If getdc is called during message processing
The releasedc function must be called during the same message processing period.
You cannot call getdc when processing message A, but call releasedc when processing message B.
3. Comparison with beginpaint
Getdc can also get the device content handle, but unlike beginpaint, getdc will not make any invalid region valid;
This can be understood as follows:
When an invalid region is generated, the system records the format of the invalid region and sends a wm_paint message.
The beginpaint function clears the wm_paint message, while the getdc function does not clear the wm_paint message. As long as the wm_paint message exists,
An invalid region exists.
The device content handle returned by getdc has a rectangle that is equal to the entire display area, so that you can draw a part of the display area
Not just draw on invalid rectangles.
4. You can call this function instead of calling the beginpaint function but the entire region:
Valideterect (hwnd, null );
In this way, wm_paint messages delivered to the Message Queue can be cleared.
5. Use getdc and releasedc
You can call getdc and releasedc to respond to keyboard messages and mouse messages, so that you can update the display through mouse or keyboard input.
Display area, and do not need to wait until the window appears invalid area to draw the window.
6. getdc and getwindowdc
Getdc returns a device content handle used to draw in the display area, while the getwindow function returns a device content handle used to draw in the entire window.
Device content handle.
The program should also process the wm_ncpaint non-display area draw message.

To draw a window:
1. Get the device content handle of the window to be drawn
2. Call GDI to draw a window
3. Release the device content handle.

4.5 textout Functions
The textout function is used to display text. Its syntax is:
Textout (HDC, X, Y, pstext, ilength );
Parameters:
HDC: device content handle, which can be the handle returned by the getdc and beginpaint functions.
Properties of device content control the features of the displayed string
The device content has an attribute that specifies the text color. The default color is black. The default device content also defines the white character output background.
When you use the textout function to output text, the text output is displayed based on the default device content attribute.
The text background color is different from the background color of the device when the window category is defined. The background color in the window category is a painter, which is used by windows to erase the display area, not a device.
Part of the content. Generally, wndclass. hbrbackground is set
Set the paint brush to white_brush.

Pstext: the string to be displayed. The string cannot contain ASCII control characters (such as line breaks, carriage returns, tabulation, and backspace). Windows displays these as solid blocks.

X, Y: X and Y are the starting coordinate positions when the characters are displayed. X is the horizontal position, and the direction increases to the right; Y is the vertical position, and the downward direction increases. (0, 0) is the application
The upper left corner of the area. The coordinate system becomes the logical coordinate system. There are multiple coordinate imaging methods in windows, which control the logic specified by the GDI function.
The position is converted to the display coordinate of the actual pixel coordinate. In the device content definition, the default mode is mm_text. The unit is the same as the actual unit.

The device content defines a cropping area. The device content handle obtained by getdc is the entire display area, and the device content handle obtained by beginpaint is an invalid area. Windows
The string is not drawn anywhere outside the cropping area.

System Font:
The device content defines the font used by windows to display text in textout. The default font is the system font, or the system_font in the Windows header file,
The system font is the default font for Windows to display strings in the title bar, menu, and dialog box.

Character size
The Minimum Operating Principle for Windows displays is 640*480. Various information can be obtained through calling system functions.
1. The getsystemmetrics function obtains the size information of various visual components on the user interface,
2. gettextmetrics obtains the font size. gettextmetrics returns the information of the selected font in the device content,
Therefore, the gettextmetrics function needs to manipulate the device content handle. When this function is called, Windows assigns values of different text sizes.
To the textmetrics struct.
Textmetrics struct has 20 fields. Usually we need to operate on the previous fields.

Typedef struct tagtextmetric
{
Long tmheght; // tmheight = tmascent + tmdescent indicates the maximum length of Characters in the baseline.
Long tmascent;
Long tmdescent;
Long tminternalleading;
Long tmexternalleading;
Long tmavecharwidth;
Long tmmaxcharwidth;
Other fields;
} Textmetric, * ptextmetric;
Leading: The spacing refers to the space that the printer inserts between two texts. In the textmetric structure, the internal spacing is included in tmascent, and usually
Is where the accent symbol appears. The tminternalleading field can be set to 0. At this time, the letter of the duplicate note is slightly shortened to print the accent.
The textmetrics structure contains two fields that describe the character width:
Tmavecharwidth: weighted average width of lowercase letters
Tmmaxcharwidth: the width of the widest character in the font
It should be noted that the non-width font is used in windows. For example, W is wider than I.
Average width of uppercase letters: It can be calculated using tmavecharwidth * 150%.
The unit of the above field value is determined by the method of the selected device content image. By default, the image method is mm_text, and the image is measured in pixels.

Use the following method to obtain text information:
HDC;
Textmetric TM;
HDC = getdc (hwnd)
Gettxtmetrics (HDC, & TM );
Releasedc (hwnd, HDC );
In this way, you can view the characters in the current device content through the fields of the TM struct variable.

Format text:
After windows is started, the system font size will not change. In the program, you can call the gettextmetrics function once to obtain the system font information.
We recommend that you call the gettextmetrics function when processing the wm_create message, because the wm_create message is the first message received by the window message processing program.
You can do this:
Defined in the message processing program:
Static int cxchar, // storage system character width
Cychar; // storage system character height
In message processing:
Case wm_create:
HDC = getdc (hwnd );
Gettextmetrics (HDC, & TM );
Cxchar = TM. tmavdcharwidth;
Cychar = TM. tmheight + TM. tmexternalleading;
Release (hwnd, HDC );
Return 0;

Formatting string functions: sprintf and wsprintf (available in Windows ).
Wspritnf function prototype:
Int wsprintf (char * DEST, char * Source ,...);

Exp:
Int ilength;
Tchar szbuffer [40];

Ilength = wsprintf (szbuffer, text ("the sum of % I and % I is % I"), IA, IB, Ia + IB );
Textout (HDC, X, Y, szbuffer, ilength );
The wsprintf function puts the formatted string into a string, and returns the number of characters in the string.
This is exactly the two parameters used by the textout function.

Because the function call method is _ stdcall, you can:
Textout (HDC, X, Y, szbuffer, wsprintf (szbuffer, text ("the sum of % I and % I is % I"), IA, IB, IA + IB )).

Obtain the system visual component size information:
Getsyetemmetrics Function
The getsystemmetrics function returns the size information of different visual components in windows, including labels, cursors, title bars, and scroll bars. These sizes and display cards
Related to the driver.
Its function prototype is:
Int winapi getsystemmetrics (INT index)

Today, it's just a bit messy, but it's probably not a problem to understand ....................

Next time I guess I want to talk about the scroll bar. I read a book a few days ago, but I don't understand it very well. I will try again later ..........

The preparation of Windows applications mainly aims to understand the event-driven mechanism and the internal mechanism of each function. Through this classic book, we can enhance our understanding of Windows programs, if you have a certain foundation, you can also study the classic core programming course, which is also more than 1000 books. It may take a while to finish reading it ......

I plan to finish learning this book slowly, and then I will look at teacher Luo's 700-plus-pages book. I guess I will have a rough idea of the operating mechanism of windows .......

Maybe I will switch to the industry in the future, maybe I will not switch .........

who knows what will happen in the future?

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.