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
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);
}