Change font style on win32 buttons.

Hey guys,

I am making a simple win32 program to practice making some different kinds of controls etc. I made some buttons and I was wondering if their was any way to change the text attributes on them, like the font style, font size, boldness etc.

Here is my code:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define _WIN32_WINNT 0x0600
#define _WIN32_IE 0x900
#include <windows.h>
#include <commctrl.h>

#define IDC_BUTTON_1 101
#define IDC_BUTTON_2 102

const char g_ClassName[] = "windowClass";

LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch(uMsg) {
        case WM_CREATE: {
            HWND hwndButton1 = CreateWindowEx(NULL,
                "BUTTON",
                "Click Me",
                WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_TEXT,
                50, 220, 100, 24,
                hwnd,
                (HMENU)IDC_BUTTON_1,
                GetModuleHandle(NULL),
                NULL);

            if (hwndButton1 == NULL) {
                MessageBox(NULL, "FAILURE Creating button 1", "ERROR!", MB_OK | MB_ICONWARNING);
                return 0;
            }

            HWND hwndButton2 = CreateWindowEx(NULL,
            "BUTTON",
            "Click Me 2",
            WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
            200, 220, 100, 24,
            hwnd,
            (HMENU)IDC_BUTTON_2,
            GetModuleHandle(NULL),
            NULL);

            if (hwndButton2 == NULL) {
                MessageBox(NULL, "FAILURE Creating button 2", "ERROR!", MB_OK | MB_ICONWARNING);
                return 0;
            }
        }
        break;
        case WM_COMMAND:
            switch(LOWORD(wParam)) {
                case IDC_BUTTON_1:
                    MessageBox(hwnd, "You clicked button 1!", "Button Clicked!", MB_OK | MB_ICONINFORMATION);
                break;
                case IDC_BUTTON_2:
                    MessageBox(hwnd, "You clicked button 2!", "Button Clicked!", MB_OK | MB_ICONINFORMATION);
                break;
            }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow) {

INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_STANDARD_CLASSES;
InitCommonControlsEx(&icex);

HWND hwndMain;
WNDCLASSEX wc;
MSG Msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = MainWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_ClassName;
wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

if (!RegisterClassEx(&wc)) {
    MessageBox(NULL, "Failed to register window class!", "ERROR!", MB_OK | MB_ICONWARNING);
    return 0;
}



hwndMain = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_ClassName,
    "Controls Test",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
    NULL, NULL, hInstance, NULL);



if (hwndMain == NULL) {
    MessageBox(NULL, "Failed to create window!", "ERROR!", MB_OK | MB_ICONWARNING);
    return 0;
}

ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);

while (GetMessage(&Msg, NULL, 0, 0) > 0) {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
}


Any help is appreciated!

Thanks!
You can replace the font used by a control, which means you can choose the face, size, etc.

Where

static HFONT s_hFont = NULL;

has been delared near start of WndProc (usually...)

1. create a font in your WM_CREATE handler

1
2
3
4
5
6
7
8
9
10
11
12
13
            const TCHAR* fontName = _T("Croobie");
            const long nFontSize = 10;

            HDC hdc = GetDC(hWnd);

            LOGFONT logFont = {0};
            logFont.lfHeight = -MulDiv(nFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
            logFont.lfWeight = FW_BOLD;
            _tcscpy_s(logFont.lfFaceName, fontName);

            s_hFont = CreateFontIndirect(&logFont);

            ReleaseDC(hWnd, hdc);


Note
- you are responsible for ensuring that the font stays around while the control or controls use it. And for destroying in in WM_DESTROY (using DeleteObject.)
- the MulDiv trick allows you to use point size, rather than logical units, to specify your font size.

2. use WM_SETFONT to tell controls to use new font

SendMessage(s_hWndButton, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));

Note
- if you pass NULL for the font, the control will switch back to using the default system font.

Andy

WM_SETFONT message
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632642%28v=vs.85%29.aspx

LOGFONT
http://msdn.microsoft.com/en-us/library/aa911419.aspx

CreateFontIndirect function
http://msdn.microsoft.com/en-us/library/windows/desktop/dd183500%28v=vs.85%29.aspx

(there is also CreateFont, if you like lots of function params; I find the 'indirect version easier to use as you can zero most of the struct members and just fill in the few you need. It helps that all the defaults correspond to a value of 0.)

And I use this tool to check out fonts, to see what I want to use

AMP Font Viewer 3.86
http://www.ampsoft.net/utilities/FontViewer.php
Last edited on
PS I see you're still returning 0 if there's an error in WM_CREATE.

You do know that you can abort the window creation by returning -1, if you want to??

Andy
PS I see you're still returning 0 if there's an error in WM_CREATE.

You do know that you can abort the window creation by returning -1, if you want to??

Andy


Thanks for the tip! I changed the return values for errors to -1 in my program. Like I said I am still learning.

Also for the font I will read the documentation pages and then try to implement it! I will post back here with how it goes.

Thanks for the help!
I read all the documentation and I tried it and it worked great! Thanks for your help!
Topic archived. No new replies allowed.