Change the window size based on the length of the text string

Source: Internet
Author: User
Tags drawtext

 

 

Raise questions:

Based on the content of the text file, use the CWin subclass to dynamically create the control. In the Create method, use the following code to Create all controls based on the message text font selected in the displayed Properties dialog box:

NONCLIENTMETRICS ncm;
Ncm. cbSize = sizeof (NONCLIENTMETRICS );
SystemParametersInfo (SPI_GETNONCLIENTMETRICS, sizeof (ncm), & ncm, 0 );
M_DefaultFont.CreateFontIndirect (& ncm. lfMessageFont );

After obtaining the font, the next question is how to determine the static control height and width to display the complete text?

It should be clear that the "Display Properties" dialog box is right-click on the desktop and selectSelect the "properties" menu item, and then select the dialog box (figure 1) on the "appearance" property page ).

Figure 1

In this dialog box, you can select the activity window, message box, and text color and font. You can call the SystemParametersInfo function and use SPI_GETNONCLIENTMETRICS as the first parameter to find out the font selected. The SystemParametersInfo function fills in the information you need in the NONCLIENTMETRICS structure:
// NONCLIENTMETRICS structure (winuser. h)
//
Struct NONCLIENTMETRICS
{
UINT cbSize;
Int iBorderWidth;
Int iScrollWidth;
Int iScrollHeight;
Int iCaptionWidth;
Int iCaptionHeight;
LOGFONT lfCaptionFont;
Int iSmCaptionWidth;
Int iSmCaptionHeight;
LOGFONT lfSmCaptionFont;
Int iMenuWidth;
Int iMenuHeight;
LOGFONT lfMenuFont;
LOGFONT lfStatusFont;
LOGFONT lfMessageFont;
};
This structure contains the LOGFONT structure used for menus, messages, and other fonts. Once you have a font, how do you know the size of the screen space required to display a given text string? This is a common problem in Windows programming. Fortunately, there is a special function in Windows that can display text anywhere on the screen. This function is DrawText. In Windows, this is a basic text display function. The function is to display (or draw) text strings on the specified screen coordinates.

CDC dc = ...;
CRect rc = ...;
CString str = "Hello ";
Dc. DrawText (str, & rc, 0 );

DrawText displays text in the rectangle you specify-but what if you don't know how big the rectangle should be? At this time, there is an easy way to calculate the size of the rectangle:

CRect rc (0, 0, 0 );
Dc. DrawText (str, & rc, DT_CALCRECT );

DrawText does not actually draw text, but calculates the space required for the text. DrawText changes the size of the input rectangle to adapt to the width and height of the text. After DrawText is called, rc. Widtrh and rc. Height return the width and Height of the text. You can use the coordinates in the upper left corner to initialize CRect, that is, (rc. left, rc. top) instead of (0, 0 ). Then, DrawText generates coordinates based on the text width and height.
Of course, the preceding code segment assumes that the device context of the font has been selected. How can I use LOGFONT alone? If you already have a DC because MFC has passed it to you (for example, when MFC calls the CView: OnDraw of the view or the CWnd: OnDrawItem of the host painting item ), you can use the provided DC as your device context; otherwise, you must call one of the following two CDC creation functions.

// Create a customer DC
CClientDC dc (pMyWnd );
Or
// Create window DC
CWindowDC dc (pMyWnd );

Because you don't want to really draw anything, it doesn't matter to use that device context. As long as you create a screen DC-call any CDC creation function relative to a printer or other devices (unless you want a printer device context ).
Once you have a device context, you have to create a font object and select it as a DC object. This is a standard Windows graphic programming.
// Create a font
CFont font;
Font. CreateFontIndirect (& ncm. lfMessageFont );
// Select the font to DC
CFont * pOldFont = dc. SelectObject (& font );
// Calculate the text size
Dc. DrawText (str, rc, DT_CALCRECT );
// Reply to the old font-this is required
Dc. SelectObject (pOldFont );

 

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.