"Windows Programming" Reading notes------------->> text output--Multiple lines of text <<

Source: Internet
Author: User
Tags textout

Because TextOut is not able to use/N for multiline output, the position of each of its rows is determined by coordinates, so to achieve multiple lines of output is to calculate the position of each row, so you need to know the height of each row (without considering that it will go beyond the right boundary).

The way the program gets the row height is to get the height of each word from the HDC handle. Use functions as follows: GetTextMetrics (HDC, & TM);

TM is a textmetric structure that needs to be defined by itself. Calling this function requires a HDC handle, which is usually done at the time the program is created, so you can use GETDC to get the handle.

Textmetric contains a lot of information, but do not have to worry about memory, in VC + +, when the completion of the TM. After that, all subsequent attributes will be displayed, as long as you remember the first few letters can be entered immediately. This is also VC + + looks very difficult to remember things a lot of reasons: there are so many useful: typedef struct TAGTEXTMETRIC

{

LONG Tmheight; Text height

LONG tmascent; Text upper Height

LONG tmdescent; Lower height of text

LONG tminternalleading; Where the accent marks appear

LONG tmexternalleading; Line spacing

LONG Tmavecharwidth; Text Average width

LONG Tmmaxcharwidth;

Other structure fields

}

Textmetric, * ptextmetric;

Just check this picture to see how the height of each part of a letter is divided:
> Text output-Multiple lines of text

In addition, the capital letter width is generally 1.5 times times tmavecharwidth. So the text width is generally tmavecharwidth, height is tmheight+tmexternalleading

So, the general initialization code is this (from the Msdn[platform sdk>msdn library> user interface design and development >windows Controls>individual control Example of Information>scroll bars>using Scroll Bars] Case wm_create:
Get the "Handle to" client area ' s device context.
HDC = GetDC (hwnd);

Extract font dimensions from the text metrics.
GetTextMetrics (HDC, & TM);
Xchar = Tm.tmavecharwidth;
Xupper = (tm.tmpitchandfamily & 1?) 3:2) * XCHAR/2;
Ychar = Tm.tmheight + tm.tmexternalleading;

Free of the device context.
ReleaseDC (hwnd, HDC);

Set an arbitrary maximum width to client area.
(Xclientmax is the sum of the widths of average
lowercase letters and uppercase letters.)
Xclientmax = Xchar + * Xupper;

return 0;

MSDN Address: Ms-help://ms. Msdnqtr.2003feb.2052/shellcc/platform/commctls/scrollbars/usingscrollbars.htm

Then, each line is like this: TextOut (hdc, x * xchar, Y * ychar, szbuffer,wsprintf (Szbuffer, TEXT ("What what"));

X,y refers to the first X characters and the line Y. The advantage of using the wsprintf function is that its return value is the length of the string, and it can be formatted using the text in the%i. Plainly wsprintf is in the preformatted to the szbuffer this buffer. But remember to define the Szbuffer first (TCHAR szbuffer [40];)


-----------------------------------------------------------
OK, finally can do, this time to get something to output:

Displays the information that can be obtained from the GetSystemMetrics call. This function is used in this way:

GetSystemMetrics ("index"), the so-called index, is an integer that tells the function what we need to know. Because this function is indexed a lot (nonsense, not much how to practice our multiline output), so we define a. h file, call this structure directly. The specific procedures are not listed, the book has, the proposal in addition to SYSMETS.H, other parts of their own write (note the book on a little Mistake OH)


Here is the example of the engineering package (in fact, yesterday, the MessageBox function to remove the line):

Http://dl2.csdn.net/down4/20070706/06104222215.rar

> Text output-Multiple lines of text here you'll find something that doesn't show up. This is going to use the scroll bar ...

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.