void FormattedTextOut(HDC hDC, int x, int y, LPCTSTR fmt, ...)
{
char buf[2048];
va_list vlist;
va_start(vlist, fmt);
int nRet = _vsnprintf(buf, sizeof(buf), fmt, vlist); /* this is safe */
// int nRet = vsprintf(buf, fmt, vlist); /* this is not safe */
va_end(vlist);
if (0 <= nRet)
::TextOut(hDC, x, y, buf, lstrlen(buf));
}
GDI+
FontFamily -> GdiplusBase
Font -> GdiplusBase
StringFormat -> GdiplusBase
.SetAlignment()
StringAlignmentCenter
.SetLineAlignment()
Graphics
.DrawString()
와이드 문자를 사용한다.
.SetTextRenderingHint()
TextRenderingHintClearTypeGridFit
void CTextView::OnDraw(CDC* pDC)
{
CLineDrawingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
Graphics graphics(pDC->m_hDC);
// 글꼴 생성 FontFamily fontFamily(L"Arial"); Font font(&fontFamily, 24, FontStyleBold, UnitPixel);
// 브러시 생성
SolidBrush brush(Color(255, 0, 0, 255));
// 가운데 정렬 StringFormat stringFormat;
stringFormat.SetAlignment(StringAlignmentCenter);
stringFormat.SetLineAlignment(StringAlignmentCenter);
CRect rect;
GetClientRect(rect);
// 윈도우의 중심점에 정렬한 텍스트 출력
PointF pointF(rect.Width()/2.0f, rect.Height()/2.0f);
WCHAR string[] = L"Hello. This is a GDI+ Font & Text output test";
graphics.DrawString(string, (INT)wcslen(string), &font, pointF, &stringFormat, &brush);
// 텍스트 출력 품질 조절
graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
// 사각형의 중앙에 정렬한 텍스트 출력
RectF rectF(300, 20, 200, 100);
graphics.DrawString(string, (INT)wcslen(string), &font, rectF, &stringFormat, &brush);
}