Windows/Windows API

폰트 선택 (ChooseFont)

aucd29 2013. 10. 1. 18:55
#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="FontDial";

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(WHITE_BRUSH);
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc=(WNDPROC)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,0,0,0)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}

LOGFONT lf;
COLORREF Col;
char str[]="폰트 대화상자 Test 1234";
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    CHOOSEFONT CFT;
    HFONT MyFont, OldFont;
    switch(iMessage) {
    case WM_CREATE:
        lf.lfHeight=20;
        strcpy(lf.lfFaceName,"명조");
        return 0;
    case WM_LBUTTONDOWN:
        memset(&CFT, 0, sizeof(CHOOSEFONT));
        CFT.lStructSize = sizeof(CHOOSEFONT);
        CFT.hwndOwner=hWnd;
        CFT.lpLogFont=&lf;
        CFT.Flags=CF_EFFECTS | CF_SCREENFONTS;
        if (ChooseFont(&CFT)) {
            Col=CFT.rgbColors;
            InvalidateRect(hWnd, NULL, TRUE);
        }
        return 0;
    case WM_PAINT:
        hdc=BeginPaint(hWnd,&ps);
        MyFont=CreateFontIndirect(&lf);
        OldFont=(HFONT)SelectObject(hdc,MyFont);
        SetTextColor(hdc,Col);
        TextOut(hdc,100,100,str,strlen(str));
        SelectObject(hdc,OldFont);
        DeleteObject(MyFont);
        EndPaint(hWnd,&ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}