Writes a character string at the specified location using the currently selected font.
| |
virtual BOOL TextOut( int x, int y, LPCTSTR lpszString, int nCount );BOOL TextOut( int x, int y, const CString& str ); |
Parameters
-
x
-
Specifies the logical x-coordinate of the starting point of the text.
-
y
-
Specifies the logical y-coordinate of the starting point of the text.
-
lpszString
-
Points to the character string to be drawn.
-
nCount
-
Specifies the number of bytes in the string.
-
str
-
A CString object that contains the characters to be drawn.
Return Value
Nonzero if the function is successful; otherwise 0.
Remarks
Character origins are at the upper-left corner of the character cell. By default, the current position is not used or updated by the function.
If an application needs to update the current position when it calls TextOut, the application can call the SetTextAlign member function with nFlags set to TA_UPDATECP. When this flag is set, Windows ignores the x and y parameters on subsequent calls to TextOut, using the current position instead.
大概意思是說當調用函數SetTextAlign ,參數為TA_UPDATECP情況下,Windows調用TextOut就會忽視x,y參數值,而用當前位置替代。
void CMy0121DrawTextView::OnDraw(CDC* pDC)
{
CMy0121DrawTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
COLORREF crOld = SetTextColor(pDC->GetSafeHdc(),RGB(255,0,0));
TextOut(pDC->GetSafeHdc(),12,13,_T("Red"),3);
SetTextAlign(pDC->GetSafeHdc(),TA_UPDATECP);
SetTextColor(pDC->GetSafeHdc(),RGB(0,255,0));
TextOut(pDC->GetSafeHdc(),12,30,_T("Green"),5); //文本不會覆蓋
TextOut(pDC->GetSafeHdc(),12,30,_T("Green Two"),9); //文本不會覆蓋
TextOut(pDC->GetSafeHdc(),12,30,_T("Green Two"),9); //文本不會覆蓋
RECT rcText;
rcText.left = 50;
rcText.top = 50;
rcText.bottom = 100;
rcText.right = 150;
SetTextAlign(pDC->GetSafeHdc(),TA_NOUPDATECP); // 這個必須有,要不顯示不正常
DrawText(pDC->GetSafeHdc(),"DrawRect",8,&rcText,DT_LEFT | DT_SINGLELINE |DT_END_ELLIPSIS);
SetTextColor(pDC->GetSafeHdc(),crOld);
}