Windows Programming _ sun Xin C ++ lesson5 text operations

Source: Internet
Author: User
Tags drawtext textout

Windows Programming _ sun Xin C ++ lesson5 text operations

Highlights of this section:

1. Create and display the insert Operator

2. Obtain the font and the height and width of the specified string.

3. Use of the cstring class

4. Concepts and usage of path layer and cut zone

5. Character Input and display

6. Achieve karaoke subtitles

//************************************** **************************************** **********************

1. Create and display the insert Operator
// Create an insert operator that matches the font height and average width
Textmetric TM;
Cclientdc DC (this );
DC. gettextmetrics (& TM );
Createsolidcaret (TM. tmavecharwidth/8, TM. tmheight); // create an insert operator and display the TM. tmavecharwidth/8 experience values.
Setcaretpos (m_ptorigin); // The insert operator is set on the first line.
Showcaret ();
// Bitmap insert Operator
/* Cclientdc (this );
M_bitmap.loadbitmap (idb_bitmap1 );
Createcaret (& m_bitmap); // create an insert operator and display TM. tmavecharwidth/8 as experience values
Showcaret ();*/
2. font and string width and height Information Acquisition
Use the gettextmetrics () function to obtain information such as the font width and height. Use the gettextextent () function to obtain the length and width of the output string.

The structure of the font information is textmetric. The Int tmascent and INT tmdescent variables in the structure are as follows:

3. cstring Constructor
STR method 1 cstring STR ("text experiment! ");
STR method 2 cstring STR; STR = "text experiment! ";
Construct STR method 3 StR. loadstring (ids_stringtest );
4. Path layer and cut area
The path layer forms objects similar to a region and can contain a series of drawing operations, which are generally used to produce complex elements;
The cut area is limited to the drawing area of a certain space;
DEMO code:
//************************************** **************************************** **********************

Void ctextview: ondraw (CDC * PDC)
{
Ctextdoc * pdoc = getdocument ();
Assert_valid (pdoc );
// Todo: Add draw code for native data here
Cstring STR ("text experiment! "); // Construct STR method 1
// Construct STR method 2 cstring STR; STR = "text experiment! ";
// Construct STR method 3 StR. loadstring (ids_stringtest)
PDC-> textout (50, 50, STR );
Csize cs = PDC-> gettextextent (STR );

PDC-> beginpath ();
PDC-> rectangle (50, 50, CS. cx + 50, CS. Cy + 50); // output characters starting from rectangular circles
PDC-> endpath ();

PDC-> selectclippath (rgn_diff); // use the cut area rgn_diff as the mode parameter
Int I;
For (I = 0; I <= 300; I + = 10)
{
PDC-> moveTo (I, 0 );
PDC-& gt; lineto (I, 300 );
PDC-> moveTo (0, I );
PDC-> lineto (300, I );
}
}
//************************************** **************************************** **********************

No cutting area is used. The effect is as follows:

Use the cut area. The effect is as follows:

5. Character Input and display
(1) Save the input and add the member variable cstring m_strinput; cpoint m_ptorigin;
(2) In response to the wm_char and wm_lbuttondown messages, add the corresponding code in the response function as follows:
//************************************** **************************************** **********************

Void ctextview: onlbuttondown (uint nflags, cpoint point)
{
// Todo: add your message handler code here and/or call default
Setcaretpos (point );
M_strinput.empty ();
M_ptorigin = point;
Cview: onlbuttondown (nflags, point );
}
Void ctextview: onchar (uint nchar, uint nrepcnt, uint nflags)
{
// Todo: add your message handler code here and/or call default
Cclientdc DC (this );
Textmetric TM;
Cfont font;
Font. createpointfont (300, "文 ", null); // create a font
Cfont * poldfont = Dc. SelectObject (& font );
DC. gettextmetrics (& TM );
// Press enter to wrap the line
If (0x0d = nchar)
{
M_strinput.empty ();
M_ptorigin.y = m_ptorigin.y + TM. tmheight;
Setcaretpos (m_ptorigin); // Insert the operator to the next row
}
Else
// Return key
If (0x08 = nchar)
{
Colorref cf = Dc. settextcolor (DC. getbkcolor (); // background color output
DC. textout (m_ptorigin.x, m_ptorigin.y, m_strinput );
M_strinput = m_strinput.left (m_strinput.getlength ()-1 );
DC. settextcolor (CF );
}
Else // normal character input
{
M_strinput + = nchar;
}
Csize cs = Dc. gettextextent (m_strinput );
Cpoint pt;
PT. x = m_ptorigin.x + CS. CX;
PT. Y = m_ptorigin.y;
Setcaretpos (PT );
DC. textout (m_ptorigin.x, m_ptorigin.y, m_strinput );
DC. SelectObject (poldfont );
Cview: onchar (nchar, nrepcnt, nflags );
}
//************************************** **************************************** **********************

The input results are as follows:

6. Achieve karaoke subtitles
(1) Use the drawtext () function to draw in a specific area (rectangular area)
(2) set the timer to regularly expand the width of the rectangular area
About Timer:
Function prototype:
Uint settimer (uint nidevent, uint nelapse, void (callback Export * lpfntimer) (hwnd, uint, uint, DWORD ));
A. settimer (1,100, null); Use the message response function ontimer () provided by the MFC framework ();
B. settimer (, & timerproc); Use a custom callback function to process wm_timer messages
The prototype and test code of the message response function are as follows:
Void callback export timerproc (
Hwnd, // handle of cwnd that called settimer
Uint nmsg, // wm_timer
Uint nidevent, // timer Identification
DWORD dwtime // system time
)
{
: MessageBox (hwnd, "self defined function to deal with wm_timer MSG! "," Tip ", mb_ OK );

}
(3) regularly output text and change text color
The code for the karaoke subtitle experiment is as follows:
//************************************** **************************************** **********************

Void ctextview: ontimer (uint nidevent)
{
// Todo: add your message handler code here and/or call default
Killtimer (2 );
// Subtitle effect demonstration

Cstring STR ("wellcom, MFC word Edit! This program is to pratice word processing! ");
Cclientdc DC (this );
Cfont font;
Font. createfont (
50, // nheight
15, // nwidth
0, // nescapement
0, // norientation
Fw_normal, // nweight
False, // bitalic
False, // bunderline
0, // cstrikeout
Ansi_charset, // ncharset
Out_default_precis, // noutprecision
Clip_default_precis, // nclipprecision
Default_quality, // nquality
Default_pitch | ff_swiss, // npitchandfamily
"Arial"); // create a font
Cfont * oldfont = Dc. SelectObject (& font );
Textmetric TM;
DC. gettextmetrics (& TM );
M_nwidth + = 5;

Crect rect;
Rect. Left = 0;
Rect. Top = 200;
Rect. Bottom = rect. Top + TM. tmheight;
Rect. Right = m_nwidth; // defines the rectangular area of the drawing.

DC. textout (0,200, STR );
Colorref cf = Dc. settextcolor (RGB (255, 0, 0 ));
DC. drawtext (STR, & rect, dt_left); // output from left to right

Rect. Top = 150;
Rect. Bottom = rect. Top + TM. tmheight;
DC. drawtext (STR, & rect, dt_right); // output from the right

Csize size;
Size = Dc. gettextextent (STR );
If (m_nwidth> size. CX) // If the string length is exceeded, output again
{
M_nwidth = 0;
DC. settextcolor (RGB (0,255, 0 ));
DC. textout (0,200, STR );
}
DC. settextcolor (CF );
DC. SelectObject (oldfont );

Cview: ontimer (nidevent );
}
//************************************** **************************************** **********************

The effect is as follows:

Summary:

1. Understand the differences between font information and specified string font information

2. Master the creation and use of the insert character and font

3. Master the simple principle of Character Input

4. Master the use of timer and its callback Functions

 

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.