< 폰트 >

 

폰트도 GDI 오브젝트이며 폰트를 만들고 DC로 전송한 후 문자열을 출력하면 DC에 선택된 폰트를 사용하여 문자열을 출력한다.

 

1
2
3
4
HFONT CreateFont(int cHeight, int cWidth, int cEscapement, int cOrientation, int cWeight, 
                 DWORD bItalic, DWORD bUnderline, DWORD bStrikeOut, DWORD iCharSet, 
                 DWORD iOutPrecision, DWORD iClipPrecision, DWORD iQuality, DWORD iPitchAndFamily,
                 LPCTSTR pszFaceName);
cs

 

폰트를 만들기 위해서는 CreateFont 함수를 사용하며 이 함수가 리턴하는 핸들을 HFONT형의 변수에 대입한다.

많은 인자 중 실질적으로 변경되는 인자는 문자의 크기를 지정하는 cHeight와 글꼴 모양을 지정하는 pszFaceName 정도이며

나머지 인자는 디폴트로 사용한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    HFONT hFont, OldFont;
    TCHAR *str = TEXT("폰트 Test 1234");
 
    switch(iMessage) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        hFont = CreateFont(50,0,0,0,0,0,0,0,HANGEUL_CHARSET,0,0,0,
            VARIABLE_PITCH | FF_ROMAN, TEXT("궁서"));
        OldFont = (HFONT)SelectObject(hdc, hFont);
        TextOut(hdc, 100100, str,lstrlen(str));
        SelectObject(hdc, OldFont);
        DeleteObject(hFont);
        EndPaint(hWnd, &ps);
        return 0;
    }
 
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
cs

 

궁서체의 50픽셀 높이를 가지는 폰트를 출력하는 코드이다.

그러나 인자가 많기 때문에 LOGFONT 구조체를 사용하여 폰트를 정의하고 CreateFontIndirect 함수로 폰트를

만드는 방법도 있다.

 

1
HFONT CreateFontIndirect(CONST LOGFONT* lplf);
cs

 

LOGFONT 구조체를 사용하면 인수의 개수가 적으므로 호출 속도가 빠르고, 여러 개의 폰트를 만들어야 할 때 LOGFONT의

멤버 중 일부만을 변경하여 재사용할 수도 있다.  또한 , LOGFONT 구조체 배열을 사용하면 사용할 폰트의 목록을 미리

작성해 놓을 수 있다는 장점이 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    HFONT hFont, OldFont;
    TCHAR *str = TEXT("폰트 Test 1234");
    LOGFONT lf;
 
    switch(iMessage) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        
        lf.lfHeight = 50;
        lf.lfWidth = 0;
        lf.lfEscapement = 0;
        lf.lfOrientation = 0;
        lf.lfWeight = 0;
        lf.lfItalic = 0;
        lf.lfUnderline = 0;
        lf.lfStrikeOut = 0;
        lf.lfCharSet = HANGEUL_CHARSET;
        lf.lfOutPrecision = 0;
        lf.lfClipPrecision = 0;
        lf.lfQuality = 0;
        lf.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
        lstrcpy(lf.lfFaceName, TEXT("궁서"));
        hFont = CreateFontIndirect(&lf);
 
        OldFont = (HFONT)SelectObject(hdc, hFont);
        TextOut(hdc, 100100, str,lstrlen(str));
        SelectObject(hdc, OldFont);
        DeleteObject(hFont);
        EndPaint(hWnd, &ps);
        return 0;
    }
 
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
cs

 

LOGFONT를 이용하여 글자를 출력한 코드이다.

 

< 텍스트 색상 >

 

폰트 오브젝트외에 출력되는 문자열에 영향을 주는 세 함수가 있다.

 

1
2
3
COLORREF SetTextColor(HDC hdc, COLORREF color);
COLORREF SetBkColor(HDC hdc, COLORREF color);
int SetBkMode(HDC hdc, int mode);
cs

 

SetTextColor는 Text의 색상을 Set하는 함수이며, SetBkColor는 글자 뒤쪽의 배경 색상을 Set하는 함수이다.

두 함수의 반대 함수는 GetTextColor, GetBKColor이며 현재 설정된 문자색과 배경색을 조사할 수 있는 함수이다.

 

SetBKMode 함수는 배경색상을 사용할 방법을 설정한다. OPAQUE와 TRANSPARENT 두 옵션이 있고 디폴트는 OPAQUE이다.

OPAQUE : 불투명한 배경을 사용한다. 그래서 배경 색상에 의해 뒷쪽의 그림이 지워진다.

TRANSPARENT : 투명한 배경을 사용한다. 그래서 배경이 뒷쪽의 그림이 지워지지 않는다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    HBRUSH MyBrush, OldBrush;
    HFONT hFont, OldFont;
    TCHAR *str = TEXT("폰트 Test 1234");
 
    switch(iMessage) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
 
        MyBrush = CreateHatchBrush(HS_CROSS, RGB(0,0,255));
        OldBrush = (HBRUSH)SelectObject(hdc, MyBrush);
        Rectangle(hdc, 5050400200);
        SelectObject(hdc, OldBrush);
        
        hFont = CreateFont(30,0,0,0,0,0,0,0,HANGEUL_CHARSET,0,0,0,
            VARIABLE_PITCH | FF_ROMAN, TEXT("궁서"));
 
        OldFont = (HFONT)SelectObject(hdc, hFont);
        SetTextColor(hdc, RGB(255,0,0));
        SetBkColor(hdc, RGB(255,255,0));
        TextOut(hdc, 100100, str,lstrlen(str));
 
        SetBkMode(hdc, TRANSPARENT);
        TextOut(hdc, 100150, str,lstrlen(str));
 
        SelectObject(hdc, OldFont);
        DeleteObject(MyBrush);
        DeleteObject(hFont);
        EndPaint(hWnd, &ps);
        return 0;
    }
 
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
cs

 

프로그램을 실행시키면 SetBKMode에 따른 결과를 확인할 수 있다.

비트맵이나 그림 위에 문자열을 출력한다면 투명 모드를 사용하면 좋을 것이다.

 

< 글자 회전시키기 >

 

CreateFont의 세 번째 인수인 nEscapement를 변경하면 문자열의 각도를 바꾸어 가며 회전시킬 수 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    int i;
    TCHAR *str = TEXT("                      Beautiful Korea");
    HFONT MyFont, OldFont;
 
    switch(iMessage) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        SetBkMode(hdc, TRANSPARENT);
        for (i=0; i<900; i+=100) {
            MyFont = CreateFont(50,0,i,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
                CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, TEXT("Times New Roman"));
            OldFont = (HFONT)SelectObject(hdc, MyFont);
            TextOut(hdc,100,450,str,lstrlen(str));
            SelectObject(hdc, OldFont);
            DeleteObject(MyFont);
        }
        EndPaint(hWnd, &ps);
        return 0;
    }
 
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
cs

+ Recent posts