This section is an introduction to the basics of text programming
We are familiar with the basic functions of text programming through the following code, and what each of these functions does.
我们通过CClientDC创建一个设备描述上下文对象dc,然后定义一个TEXTMETRIC对象,TEXTMETRIC结构体对象用来表示目前设备描述表中字体的相关信息,其中最为重要的信息就是tmAveCharWidth平均字符的宽度(因为字符之间的宽度不一样,比如‘w’和‘i’,明显前者比后者宽),tmHeight字符的高度,这个数据是相对于整体字符来说的,它包括tmAscent和tmDescent的和。我们调用GetTextMetrics函数得到字体相关信息。 我们要通过CreateSolidCaret函数创建插入符,此函数是CWnd类的函数,所以调用的时候不用指明窗口句柄,在哪个窗口类调用,就是在哪个窗口类出现。创建之后要通过调用showcaret函数才能在指定窗口显示。 我们还可以通过bitmap创建位图插入符,首先通过LoadBitmap函数载入位图,接着调用CreateCaret函数将位图载入插入符对象。 利用SetTimer函数可以设定定时器,定时器的第三个指针如果不为null,直接在指定时间调用指向的函数,如果为NULL,则在指定时间窗口将接受一个WM_TIMER消息。
CClientDC DC (this) ; Textmetric TM; Dc (&TM); createsolidcaret (Tm.tmavecharwidth/8 , Tm.tmHeight) ; showcaret () ; Bitmap (IDB_BITMAP1); createcaret (&bitmap) ; showcaret () ; settimer (1 , 100 , NULL) ;
In the VC program, we can pre-set the specified string, specified in the Project's Resource tab, there is a string table, where the specified string can be preset, and each string has a specified ID, We can call LoadString to initialize the specified CString string object by specifying the ID. The GetTextExtent call returns an object of the CSize class, with CX, CY representing the width and height of the CString string in the GetTextExtent function argument list, respectively.
Here we want to introduce a noun, called the path layer, we have to know what the path layer plays the role of what it is to be flexible to select the entire scope of the specified area, once selected, the following operations are in the region. In summary, the path layer is the specified area to manipulate.
How to set the path layer, we through Beginpath (), Rectangle (50,50,50+sz.cx,50+sz.cy), and Endpath () these three functions of the combination of completion. where Rectangle (50,50,50+sz.cx,50+sz.cy) Specifies the area that is the path layer area. Then we then use the Selectclippath () function to set its parameters to achieve the selection of the region.
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 the font, we need to use the CFont class object to implement the font modification in MFC programming. We can initialize the Font object with a variety of functions, where CreatePointFont (300, "italic", NULL) can initialize the CFont object. After setting the Font object, we use the SelectObject function to load the specified font into the device description table.
We use the SetTextColor to set the text color and get the background color from the device description table through the GetBkColor () function. Use the TextOut function to output text to the window and use the Setcaretpos function to customize the position of the caret.
Now let's look at the powerful features of the CString class, which has a lot of string-related handlers and overloads many operators, and when we're programming in MFC, when it comes to character manipulation, CString is the perfect choice. Where the left function intercepts a specified length of character from a specified character and returns the specified character, the GetLength () function returns the length of the specified string.
And the last function is DrawText, which writes a string to the specified range, and the last parameter of the function controls the gill type.
CCLIENTDC DC (this);
CFont font;
Font. CreatePointFont (300, "italics", 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);
}
MFC Text Programming Overview