MFC text programming overview, mfc text Overview

Source: Internet
Author: User
Tags drawtext textout

MFC text programming overview, mfc text Overview

This section describes the basic knowledge of text programming.
We use the following code to familiarize ourselves with the basic functions of text programming and their respective functions.

We use CClientDC to create a device description context object dc, and then define a TEXTMETRIC object. The TEXTMETRIC struct object is used to indicate the font information in the current device description table, the most important information is the average width of the tmAveCharWidth character (because the width between the characters is different, such as 'W' and 'I', the former is obviously wider than the latter ), the height of the tmHeight character. This data is relative to the overall character. It includes the sum of tmAscent and tmDescent. Call the GetTextMetrics function to obtain the font information. We need to use the CreateSolidCaret function to create an insert operator. This function is a function of the CWnd class. Therefore, you do not need to specify the window handle, in which window class to call, or in which window class. After creation, you must call the showcaret function to display it in the specified window. We can also use bitmap to create bitmap inserts. First, we can use the LoadBitmap function to load the bitmap, and then call the CreateCaret function to load the bitmap into the object. The SetTimer function can be used to set the timer. If the third pointer of the timer is not null, the system calls the pointing function at the specified time. If it is NULL, A WM_TIMER message is received in the specified time window.
    CClientDC dc(this);    TEXTMETRIC tm;    dc.GetTextMetrics(&tm);    CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);    ShowCaret();    bitmap.LoadBitmap(IDB_BITMAP1);    CreateCaret(&bitmap);    ShowCaret();    SetTimer(1,100,NULL);

In the vc program, we can set the specified string in advance. The specified method is in the Resource tab of the project, there is a string table, where you can preset the specified string, in addition, each string has a specified ID. We can call LoadString by specifying the ID to initialize the specified cstring object. Call GetTextExtent to return a CSize class object. The cx and cy of this object respectively indicate the width and height of the cstring specified in the GetTextExtent function parameter list.
Here we want to introduce a term called Path layer. We need to know what role the path layer plays. Its function is to flexibly select the specified area in the entire scope, once selected, the following operations are performed in this region. In short, the path layer specifies the region to be manipulated.
How to set the path Layer? We use BeginPath (), Rectangle (50, 50, 50 + sz. cx, 50 + sz. cy); and EndPath () are combined. The region specified by Rectangle (50, 50, 50 + sz. cx, 50 + sz. cy) is the path layer. Then, based on the SelectClipPath () function, we can select the region by setting its parameters.

CString s;s.LoadString(61446);pDC->TextOut(50,50,s);CSize sz=pDC->GetTextExtent(s);pDC->BeginPath();pDC->Rectangle(50,50,50+sz.cx,50+sz.cy);pDC->EndPath();pDC->SelectClipPath(RGN_AND);

For fonts, we need to modify the font through the CFont class objects in the mfc programming. You can use multiple functions to initialize font objects. CreatePointFont (300, "point ", NULL) can initialize CFont objects. After the font object is set, we use the SelectObject function to load the specified font into the device description table.
We use SetTextColor to set the text color, and the GetBkColor () function to obtain the background color in the device description table. Use the TextOut function to output text to the window, and use the SetCaretPos function to re-customize the position of the insert operator.

Now let's take a look at the powerful functions of the CString class. This class has many string-related processing functions and involves multiple operators. When we program in mfc, once we involve character operations, cstring is the best choice. The Left function intercepts characters of the specified length from the Left side of the specified character and returns the specified character. The GetLength () function returns the length of the specified string.

The last function is DrawText, which writes strings in the specified area. The last parameter of the function controls the alignment format.
CClientDC dc (this );
CFont font;
Font. CreatePointFont (300, "", NULL );
CFont * OldFont = dc. SelectObject (& font );

COLORREF clr=dc.SetTextColor(dc.GetBkColor());dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);m_strLine=m_strLine.Left(m_strLine.GetLength()-1);dc.SetTextColor(clr);dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);    CPoint pt;pt.x=m_ptOrigin.x+size.cx;pt.y=m_ptOrigin.y;SetCaretPos(pt); dc.SetTextColor(RGB(255,0,0));dc.DrawText(s,rect,DT_LEFT);

}

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.