Mike32ab
Mike32ab

Reputation: 466

Why doesn't font size show for SYSTEM_FONT in Font dialog?

#include <windows.h>


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_RBUTTONUP:
        {
            HFONT hFont;
            LOGFONT lf;
            CHOOSEFONT cf = {0};

            hFont = (HFONT)GetStockObject(SYSTEM_FONT);
            GetObject(hFont, sizeof(LOGFONT), &lf);

            cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
            cf.hwndOwner = hwnd;
            cf.lpLogFont = &lf;
            cf.lStructSize = sizeof(CHOOSEFONT);

            if(ChooseFont(&cf))
            {
            }
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
    WNDCLASSEX wc = {0};
    HWND hwnd;
    MSG msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    wc.hIcon = LoadIcon(0, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = L"MainClass";

    if(!RegisterClassEx(&wc))
        return 0;

    hwnd = CreateWindowEx(0, wc.lpszClassName, L"First", WS_OVERLAPPEDWINDOW,
        50, 30, 400, 200, 0, 0, hInstance, 0);

    if(!hwnd)
        return 0;

    ShowWindow(hwnd, nShowCmd);

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

    return (int)msg.wParam;


}

The proper font size doesn't show in the edit box of the Size: combo box in Font dialog. This was tested in windows xp sp3. Don't know if this happens in other operating systems. Why doesn't the proper font size show?

Upvotes: 0

Views: 197

Answers (1)

Robert Harvey
Robert Harvey

Reputation: 180878

SYSTEM_FONT appears to be a broken constant that Microsoft hasn't used for years, and it points to a font that is not TrueType or OpenType. SYSTEM_FONT and DEFAULT_GUI_FONT are very old and almost certainly deprecated; I suggest that you refrain from using them.

From the documentation for GetStockObject:

It is not recommended that you employ this method to obtain the current font used by dialogs and windows. Instead, use the SystemParametersInfo function with the SPI_GETNONCLIENTMETRICS parameter to retrieve the current font. SystemParametersInfo will take into account the current theme and provides font information for captions, menus, and message dialogs.

It also says:

It is not recommended that you use DEFAULT_GUI_FONT or SYSTEM_FONT to obtain the font used by dialogs and windows.

See also http://weblogs.asp.net/kdente/394499

Upvotes: 4

Related Questions