본문 바로가기
프로그래밍/API

API-DrawText

by 리뷰하는 (게임)프로그래머_리프TV 2010. 4. 19.


DrawText,
TextOut과 비슷하지만, 영역이 존재하고 그 안에 문자열을 넣는다고 보면 될 것 같다.

#include <windows.h>

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("DrawText");

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
					 LPSTR lpszCmdParam, int nCmdShow )
{
	HWND hWnd;
	MSG Message;
	WNDCLASS WndClass;
	g_hInst = hInstance;

	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.hbrBackground = (HBRUSH)GetStockObject( COLOR_WINDOW+1 );
	WndClass.hCursor = LoadCursor( NULL, IDC_NO );
	WndClass.hIcon = LoadIcon( NULL, IDI_ERROR );
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = WndProc;
	WndClass.lpszClassName = lpszClass;
	WndClass.lpszMenuName = NULL;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass( &WndClass );

	hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, (HMENU)NULL, hInstance, NULL );
	ShowWindow( hWnd, nCmdShow );

	while( GetMessage( &Message, NULL, 0, 0 ) )
	{
		TranslateMessage( &Message );
		DispatchMessage( &Message );
	}
	return (int)Message.wParam;
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam )
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rt = {100, 100, 400, 300};
	TCHAR* str = TEXT("ㄱㄱㄱㄱㄱㄱㄱㄱㄱㄱㄱㄱㄱ ㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷ ㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹ ㅁㅁㅁㅁㅁ" );

	switch( iMessage )
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_PAINT:
		hdc = BeginPaint( hWnd, &ps );
		DrawText( hdc, str, -1, &rt, DT_CENTER | DT_WORDBREAK );
		EndPaint( hWnd, &ps );
		return 0;
	}
	return( DefWindowProc( hWnd, iMessage, wParam, lParam));
}​


바뀐건 PAINT에서 TextOut을 해주던 것을, 그냥 DrawText로 바꿔 준것 밖에 없다.

DrawText( hdc, str, -1, &rt, DT_CENTER | DT_WORDBREAK );​


마찬가지로 처음엔 BeginPaint 사용해서 hdc를 받아 주고, 마지막엔 EndPaint 사용했다.
DrawText에는 hdc, "문자열", 문자열의길이, 사각영역, 스타일 이 들어 가는데,
-1을 두게 되면 끝까지 출력한다고 보면 된다.
DT_CENTER는 문자열을 중앙으로 정렬, DT_WORDBREAK 를 사용하면,
자동개행 이라고 한다.
rt에 대해서 해맬 수도 있지만, RECT 구조체에는,
시작점 x,y 와 끝점 x,y를 받아 그 사이의 영역을 정의 한다고 보면 된다.

출력 결과

'프로그래밍 > API' 카테고리의 다른 글

API-Key입력(1)  (0) 2010.04.21
API-MessageBox  (0) 2010.04.19
API-GraphOut  (0) 2010.04.19
API-TextOut  (0) 2010.04.19
API-창만들기  (0) 2010.04.19