Graphics 클래스의 DrawString() 메서드를 이용하면 문자열을 그릴수 있다. 다음은 그 예이다.
void CGdiPlusDemoView::OnPaint()
{
CPaintDC dc(this); // device context for painting
Graphics graphics(dc);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
LinearGradientBrush lgBrush(Point(0, 0), Point(0, 200),
Color::WhiteSmoke, Color::Chocolate);
graphics.FillRectangle(&lgBrush, Rect(0, 0, 600, 150));
Font font(_T("Arial"), 100, FontStyleBold, UnitPixel); //Pixel단위로 100만큼 bold를 적용하여 Arial 글씨체로 적용하라
PointF ptText(10.0f, 10.0f);
HatchBrush brush(HatchStyleSmallCheckerBoard,
Color(255, 128, 0, 0), Color::Transparent); //붉은색으로 그리되 배경을 투명하게 처리
graphics.DrawString(_T("Test String"), -1, &font, ptText, &brush); //빗살무늬브러시를 적용
//-1이면 문자열이 NULL로 끝나는 것으로 판단하며 값을 명시하면 해당 길이만큼 출력
//ptText는 문자열을 그릴 좌표를 담은 PointF 클래스 객체에 대한 참조(&)
//&brush는 문자열을 그릴 때 어떤 색상으로 할 것인지 명시
}
출력 형식
일반 GDI 에서 DrawText() 함수를 이용하면 특정 사각형을 기준으로 문자열을 정렬하여 출력이 가능하다. GDI+ 에서도 이와 같은 출력 형식을 지정할 수 있는데 특이한 점은 출력 형식 자체가 StringFormat 클래스로 객체화되어 있다는 것이다. 그리고 DrawString() 클래스는 당연히 StringFormat() 클래스의 객체를 이용한 그리기를 지원한다. 다음은 그 예이다.
void CGdiPlusDemoView::OnPaint()
{
CPaintDC dc(this); // device context for painting
Graphics graphics(dc);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
/*
LinearGradientBrush lgBrush(Point(0, 0), Point(0, 200),
Color::WhiteSmoke, Color::Chocolate);
graphics.FillRectangle(&lgBrush, Rect(0, 0, 600, 150));
Font font(_T("Arial"), 100, FontStyleBold | FontStyleItalic, UnitPixel);
PointF ptText(10.0f, 10.0f);
HatchBrush brush(HatchStyleSmallCheckerBoard,
Color(255, 128, 0, 0), Color::Transparent);
graphics.DrawString(_T("Test String"), -1, &font, ptText, &brush);
*/
Font font(_T("Arial"), 50, FontStyleBold, UnitPixel);
SolidBrush sbrush(Color::Black);
Pen pen(Color::Blue);
StringFormat format;
format.SetAlignment(StringAlignmentCenter); //가로와 세로를 기준으로 문자열을 어떻게 정렬할것인가 명시
format.SetLineAlignment(StringAlignmentCenter);
//StringAlignmentNear는 수평기준으로 왼쪽정렬
//StringAlignmentFar는 수평기준으로 오른쪽정렬
format.SetFormatFlags(StringFormatFlagsDirectionVertical); //문자열을 세로로출력
graphics.DrawRectangle(&pen, RectF(10,10,200,200));
graphics.DrawString(_T("Test String2"), -1, &font, RectF(10,10,200,200), &format, &sbrush);
//RectF 기준으로 정렬된다.
format.SetFormatFlags(format.GetFormatFlags() & 0); //플래그값 앤드연산으로 없애기
format.SetTrimming(StringTrimmingEllipsisCharacter); //너무 길면 ...으로 표시하라
graphics.DrawString(_T("Test String with Trimming"), -1, &font, RectF(300,300,300,50), &format,&sbrush);
format.SetHotkeyPrefix(HotkeyPrefixShow); //바로가기 키를 &으로 된것을 만든다. 메뉴인터페이스 구현시 사용
graphics.DrawString(_T("Test &String"), -1, &font, RectF(300,10, 300,50), &format, &sbrush);
}
'Windows > MFC' 카테고리의 다른 글
GDI+ , 좌표계 변환 (0) | 2011.12.13 |
---|---|
GDI+ , 경로와 영역( Path , Region ) (0) | 2011.12.13 |
GDI+ , 외부 이미지 파일의 처리 (Image Class) (0) | 2011.12.13 |
GDI+ , 브러쉬(Brush) (0) | 2011.12.13 |
GDI+ , 도형 그리기 (0) | 2011.12.13 |