Text vertical display issue CDC drawtext

Source: Internet
Author: User
Tags drawtext italic font textout

Since ancient times, Chinese people have the habit of writing Chinese characters from right to left and from top to bottom. When we use the output function in our own applications, the output is always a horizontal text from left to right. Is it possible to achieve the effect of vertical Chinese Characters in our applications? I accidentally discovered a way to use VC to implement the effect of vertical Chinese characters. Now I will introduce it to you.

Windows systems have many built-in word sets with names starting with "@". These word sets share a common feature, that is, all the text contained in the word sets are flat (the word head is left, bottom to right ). If we can rotate these words 270 degrees counter-clockwise and then display them, we can achieve the purpose of vertical Chinese characters.

To rotate the text, we only need to create a logical font and set the display angle of the logical font, and then set its font name to the name of a word set starting. We can create logical fonts in two ways.

First, use the createfont function of the cfont class to directly create a logical font. The createfont parameter is defined as follows:



Bool createfont (
Int nheight, // font height
Int nwidth, // font width
Int nescapement, // font display Angle
Int norientation, // font Angle
Int nweight, // The pounds of the font
Byte bitalic, // italic font
Byte bunderline, // underlined font
Byte cstrikeout, // font with strikethrough
Byte ncharset, // required Character Set
Byte noutprecision, // output precision
Byte nclipprecision, // cut precision
Byte nquality, // actual logical font and output device
// Accuracy between Fonts
Byte npitchandfamily, // font spacing and font set
Lpctstr lpszfacename // font name
);

The unit of nescapement is 0.1 angle and the direction is counter-clockwise. Therefore, in order to establish the flat word, set the value to 2700. lpszfacename is assigned the name of the word set starting, for example, "@ system ".

Second, use the logfont structure to call the cfont class member function createfontindirect as a parameter to create a logical font. The logfont structure is usually used to describe a logical font. each variable in the structure represents each attribute of the logical font, which is the same as the parameters of the createfont function. Its structure is as follows:

Typedef struct taglogfont {// lf
Long lfheight; // font height
Long lfwidth; // font width
Long lfescapement; // font display Angle
Long lforientation; // font Angle
Long lfweight; // The font pounds
Byte lfitalic; // italic font
Byte lfunderline; // underlined font
Byte lfstrikeout; // font with strikethrough
Byte lfcharset; // required Character Set
Byte lfoutprecision; // output precision
Byte lfclipprecision; // cut precision
Byte lfquality; // logical font and output device
// Accuracy between actual Fonts
Byte lfpitchandfamily; // font spacing and font set
Tchar lffacename [lf_facesize]; // font name
} Logfont;


The following is an example to illustrate the implementation of vertical Chinese characters:

1. Create an MFC Appwizard (exe) Application Project chnfont. In the first step of the MFC Appwizard wizard, select single document, and then click Finish-> OK to create the project.

2. Add the following code to the cmainframe: oncreate function to maximize the form. Afxgetmainwnd ()-> showwindow (sw_showmaximized );

3. Add the following code to implement the vertical writing function.

Void cchildview: onpaint ()
{
Cpaintdc DC (this); // device context for painting

////// Use the cfont: createfont (...) function to write Chinese characters vertically ////////
Cfont myfont; // create a font object

// Create a logical font
Myfont. createfont (
56, // font height (font width after rotation) = 56
20, // font width (font height after rotation) = 20
2700, // font display angle = 270 °
0, // norientation = 0
10, // font pounds = 10
False, // non-italic
False, // No underline
False, // No strikethrough
Default_charset, // use the default Character Set
Out_default_precis, // default output precision
Clip_default_precis, // default cut precision
Default_quality, // nquality = default value
Default_pitch, // npitchandfamily = default value
"@ System"); // body name = @ System
Cfont * poldfont = Dc. SelectObject (& myfont); // select the device description table
Crect rtclient;
Getclientrect (rtclient); // obtains the size and position of the customer area.
// Output text in a proper location in the customer Zone
DC. textout (rtclient. Width ()/2 + 30, rtclient. Height ()/8,
"No limit ");
DC. SelectObject (poldfont); // separate myfont from the device Environment
Myfont. deleteobject (); // Delete the myfont object

///////// Use the logfont structure to write Chinese characters vertically //////////////
Logfont lf; // define the font Structure
Lf. lfweight = 10; // font pounds = 10
Lf. lfheight = 56; // font height (font width after rotation) = 56
Lf. lfwidth = 20; // font width (font height after rotation) = 20
Lf. lfunderline = false; // No underline
Lf. lfstrikeout = false; // No strikethrough
Lf. lfitalic = false; // non-italic
Lf. lfescapement = 2700; // font display angle = 270 °
Lf. lfcharset = default_charset; // use the default Character Set
Strcpy (LF. lffacename, "@ system"); // body name = @ System

Cfont mylogfont; // defines the font object
Mylogfont. createfontindirect (& lf); // create a logical font
Poldfont = Dc. SelectObject (& mylogfont); // select the device description table
// Output text in a proper location in the customer Zone
DC. textout (rtclient. Width ()/2-30, rtclient. Height ()/8,
"The Yangtze River is endless ");
DC. SelectObject (poldfont); // separate myfont from the device Environment
Mylogfont. deleteobject (); // Delete the mylogfont object
}
4. Compile the connection and run it.

Or
Cstring m_text;
Csize SZ = PDC-> gettextextent ("effect ");

Crect rtcal; // The rectangular area of the string to be output. Its width is slightly greater than the character width.
Rtcal. width = Sz. cx + 2;
Int midhei = PDC-> drawtext (m_text, & rtcal, flag | dt_calcrect | dt_wordbreak | dt_noclip );

Or simply use:
For (I = 0; I <m_text.getlength (); I ++ ){
PDC-> textout (10, Y, m_text.getat (I ));
Y + = Sz. Cy;
}

 

After the program is executed, it is as follows:

 

 

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.