Font and text output: Text output
This section mainly explains the methods and examples of text output.
Text Output Process
Before the text is output to the device, we need to determine the font, font color, and output text content. The client area of the Windows window is managed by the application, so we also control the format of the output text in the application, such as the location of subsequent characters, line wrapping, and so on.
As a result, the output process of the text roughly includes three steps to determine font information, format text, and perform output operations. The following are explained separately.
1. Determine font information
Text should determine the font information before the output, or the font that is currently in use, or a custom font, after which you can display text based on a certain font or use font information to format the text, for example, we can determine where the next line of characters will be output based on the character height of the current font.
Custom fonts can be done through several member functions of the CFont class that create fonts. The information for obtaining the currently selected font can be implemented using the API function GetTextMetrics, which is prototyped as follows:
BOOL gettextmetrics (__in HDC hdc,__out lptextmetric lptm);
The parameter hdc is a handle to the device context, and the parameter lptm is a pointer to a textmetric struct variable that is used to receive font information. The TEXTMETRIC structure is defined as follows:
C + + code
- typedef struct Tagtextmetric {
- LONG tmheight; //Character height
- LONG tmascent; //characters above the's baseline line height
- LONG tmdescent; //characters below the's baseline line height
- LONG tminternalleading; ///space on top of character height specified by tmheight member
- LONG tmexternalleading; //Line spacing
- LONG tmavecharwidth; average width of//characters
- LONG tmmaxcharwidth; the maximum width of the//character
- LONG tmweight; //character's roughness
- LONG TmOverhang; //Add width between composite fonts
- LONG Tmdigitizedaspectx; //X-axis dimensions for the output device design
- LONG tmdigitizedaspecty; //y-axis size for output device design
- TCHAR Tmfirstchar; //The first character value in a font
- TCHAR Tmlastchar; //The last character value in a font
- TCHAR Tmdefaultchar; //Replace characters that are not in the font
- TCHAR Tmbreakchar; //character as a delimiter
- BYTE tmitalic; //Non 0 indicates italic font
- BYTE tmunderlined; //Not 0 indicates that the font is underlined
- BYTE tmstruckout; //Non 0 indicates the character has strikethrough
- BYTE tmpitchandfamily; //font spacing and font family
- BYTE Tmcharset; //Character Set
- } textmetric, *ptextmetric;
2. Formatted Text
Formatted text typically consists of two types, one for determining the position of subsequent text in a line of text, and one for determining the position of the next line of text as it wraps.
determine the location of subsequent text
In general, we can get the width of the current string and determine the position of subsequent text in the line of text based on this width. The width of the current string can be obtained through the API function GetTextExtentPoint32. The GetTextExtentPoint32 function is prototyped as follows:
BOOL GetTextExtentPoint32 (__in HDC hdc,__in lpctstr lpstring,__in int c,__out lpsize lpsize);
The parameter hdc is a handle to the device context, and the parameter lpstring is a pointer to the text string cache, which is not required to end with a terminator, because the parameter c specifies the length, and the argument C is the length of the string pointed to by lpstring The parameter lpsize is a pointer to a size struct variable that is used to receive the width and height information of a string. The size struct body is defined as follows:
C + + code
- typedef struct Tagsize {
- LONG CX; //Width
- LONG cy; //Height
- } SIZE, *psize;
The starting horizontal and width of the string is known, and the sum of the two is the starting coordinate of the subsequent text.
determines the position of the next line of text when wrapping
After the GetTextMetrics function obtains the information of the current font and deposits it into the textmetric structure, the vertical coordinates of the next line are obtained by calculating the vertical coordinates of the current line of text, the height of the current font, and the sum of the line spacing.
3. Perform text output operation
Finally, a text output operation is performed via the API function TextOut. The TextOut function is prototyped as follows:
BOOL TextOut (__in HDC hdc,__in int nxstart,__in int nystart,__in lpctstr lpstring,__in int cbstring);
The parameter hdc is the handle of the device context, the parameter nxstart is the starting point x coordinate, the parameter nystart is the starting point y-coordinate, the parameter lpstring is the text string to output, and the parameter cbstring the number of characters to output in the string.
Of course, you can also use the device context class CDC's member function textout to output the two overloaded forms of the Cdc::textout function as follows:
Virtual BOOL TextOut (int x,int y,lpctstr lpszstring,int ncount);
BOOL TextOut (int x,int y,const cstring& str);
parameter x Specifies the x-coordinate of the starting point of the text, parameter y specifies the y-coordinate of the starting point of the text, the parameter lpszstring is the text string to output, the parameter ncount the number of bytes in the specified string, and the parameter str is the CString object that contains the characters to output.
application examples for font and text output
Chicken Peck Rice Let's show you a simple example of font and text output. The function is to implement two strings scrolling in both horizontal and vertical directions, respectively. The implementation steps are as follows:
1, create a dialog-based MFC project, the name is set to "Example48".
2. In the automatically generated dialog template Idd_example48_dialog, delete "Todo:place dialog controls here." A static text box.
3. Add member variables for the CExample48 class in the Example48Dlg.h file:
C + + code
int m_ntextx; // the x-coordinate of the starting point of the horizontal scrolling text int m_ntexty; // the y-coordinate of the starting point of the vertical scrolling text CFont M_newfont; // New Font // font before selecting a new font
4. In the constructor of the Cexample48dlg class, initialize the newly added member variable:
C + + code
/* =null */ ) : CDialogEx (Cexample48dlg::idd, pparent) { m_hicon = AfxGetApp ()LoadIcon (idr_mainframe); 260 ; Ten ; M_poldfont = NULL; }
5. In the Cexample48dlg dialog initialization function, create a new font and turn on the timer:
C + + code
BOOL Cexample48dlg::oninitdialog () { Cdialogex::oninitdialog (); //Add "About ..." menu item to System menu. //Idm_aboutbox must is in the System command range. ASSERT ((Idm_aboutbox &0xfff0) ==Idm_aboutbox); ASSERT (Idm_aboutbox <0xf000); cmenu* Psysmenu =GetSystemMenu (FALSE); if(Psysmenu! =NULL) { BOOL Bnamevalid; CString Straboutmenu; Bnamevalid =straboutmenu.loadstring (Ids_aboutbox); ASSERT (bnamevalid); if(!straboutmenu.isempty ()) { Psysmenu->AppendMenu (Mf_separator); Psysmenu->AppendMenu (mf_string, Idm_aboutbox, Straboutmenu); } } //Set The icon for this dialog. The framework does this automatically //When the application ' s main window was not a dialogSetIcon (M_hicon, TRUE);//Set Big iconSetIcon (M_hicon, FALSE);//Set Small Icon //Todo:add Extra Initialization here //Create a new font (18-point, script)M_newfont.createpointfont ( the, _t ("Official Script")); //set the timer to 200ms at a timed timeSetTimer (1, $, NULL); returnTRUE;//return TRUE Unless you set the focus to a control}
6, modify the Cexample48dlg::onpaint () function, if the window does not minimize the output text at the specified location, that is, the OnPaint function in the IF (Isiconic ()) corresponding to the else brace in the corresponding code. The Cexample48dlg::onpaint () function is modified as follows:
C + + code
voidCexample48dlg::onpaint () { if(Isiconic ()) { CPAINTDC DC ( This);//device context for paintingSendMessage (Wm_iconerasebkgnd, Reinterpret_cast<wparam> (DC). GETSAFEHDC ()),0); //Center icon in client rectangle intCxicon =GetSystemMetrics (Sm_cxicon); intCyicon =GetSystemMetrics (Sm_cyicon); CRect rect; GetClientRect (&rect); intx = (rect. Width ()-Cxicon +1) /2; inty = (rect. Height ()-Cyicon +1) /2; //Draw the icon DC. DrawIcon (x, y, M_hicon); } Else { CPAINTDC DC ( This);//device context for painting //sets the font of the M_newfont object to the current font and saves the previous font pointer to the M_poldfontM_poldfont = (cfont*) DC. SelectObject (&M_newfont); //Setdc. SetBkMode (TRANSPARENT);//set the background to transparent! //set text color to reddc. SetTextColor (RGB (255,0,0)); //output text at the specified positiondc. TextOut (M_NTEXTX,Ten, _t ("Welcome to the chicken peck rice! ")); //set text color to greendc. SetTextColor (RGB (0,255,0)); //output text at the specified positiondc. TextOut (Ten, m_ntexty,_t ("Thank you for your attention www.jizhuomi.com")); //Restore the previous font DC. SelectObject (M_poldfont); Cdialogex::onpaint (); } }
7. Find Cexample48dlg in Class View, right click Properties, display its property page, click the Messages button on the Property Page toolbar, find Wm_timer message, add message response function Cexample48dlg: : OnTimer (Uint_ptr nidevent), and in this function, modify the coordinate position of the two text output.
C + + code
voidCexample48dlg::ontimer (uint_ptr nidevent) { //todo:add your message handler code here and/or call default LOGFONT LOGFONT; //get the LOGFONT structure of a m_newfont fontM_newfont.getlogfont (&LogFont); //reduce the value of M_NTEXTX by 5M_NTEXTX-=5; //if M_NTEXTX is less than 10, then the text "Welcome to Chicken peck Rice" back to the starting position if(M_ntextx <Ten) M_NTEXTX =260; //Add the value of m_ntexty to a character heightM_ntexty + =ABS (Logfont.lfheight); //if M_ntexty is greater than 260, then the text "Thank you for attention www.jizhuomi.com" back to the starting position if(M_ntexty >260) M_ntexty =Ten; //invalidates the window client area and then redraws Invalidate (); Cdialogex::ontimer (nidevent); }
In this step, two text can be scrolled horizontally and vertically, respectively. Chicken Peck rice and then simply explain the process: when the program just started, will call the OnPaint function, in the initial position to draw two text, and then each time to the timer time, the OnTimer function will be executed, modify the coordinates of the two of this value, and through invalidate to make the window redraw, The OnPaint function is called again to draw two text. This enables the scrolling effect of two text by modifying the coordinate values at regular intervals.
8, run the program, the final effect such as:
Well, this section is about here, the last example of everyone can enrich their own function to see the effect. Chicken Peck Rice Thank you for your support.
Vs2010/mfc font and text output: Text output