将单词字体设置为斜体字体时,单选按钮文本无法正确显示

时间:2015-11-12 15:09:27

标签: c++ c winapi

将单选按钮的字体设置为斜体字体时,单选按钮文本不会一直显示。它被夹在右侧。它只在视觉样式打开时才会发生。我在Windows XP上测试它。你是如何解决这个问题的?

#include <Windows.h>
#include <Commctrl.h>


#pragma comment(linker, \
  "\"/manifestdependency:type='Win32' "\
  "name='Microsoft.Windows.Common-Controls' "\
  "version='6.0.0.0' "\
  "processorArchitecture='*' "\
  "publicKeyToken='6595b64144ccf1df' "\
  "language='*'\"")

#pragma comment(lib, "Comctl32.lib")


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HFONT hFont = 0;
    static HWND hBtn = 0;

    switch(msg)
    {
    case WM_CREATE:
        {
            HDC hdc = GetDC(hwnd);
            int nHeight = -MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72);
            hFont = CreateFont(nHeight, 0, 0, 0, FW_NORMAL, TRUE, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, L"Microsoft Sans Serif");
            ReleaseDC(hwnd, hdc);

            hBtn = CreateWindowEx(0, L"Button", L"Hello", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 
                20, 20, 220, 20, hwnd, 0, GetModuleHandle(0), 0);
            SendMessage(hBtn, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(FALSE, 0));
        }
        break;

    case WM_PAINT: //"Hello" gets displayed properly with WM_PAINT but not in radio button
        {
            HDC hdc;
            PAINTSTRUCT ps;
            HFONT hOldFont;
            hdc = BeginPaint(hwnd, &ps);
            hOldFont = (HFONT)SelectObject(hdc, hFont);
            TextOut(hdc, 20, 80, L"Hello", 5);
            SelectObject(hdc, hOldFont);
            EndPaint(hwnd, &ps);
        }
        break;

    case WM_DESTROY: 
        DeleteObject(hFont);
        PostQuitMessage(0); 
        break;

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

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
    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";

    RegisterClassEx(&wc);

    InitCommonControls();
    hwnd = CreateWindowEx(0, L"MainClass", L"Hello", WS_OVERLAPPEDWINDOW, 140, 140, 400, 200, 0, 0, hInstance, 0);

    ShowWindow(hwnd, nCmdShow);
    while(GetMessage(&msg, 0, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

enter image description here

0 个答案:

没有答案