首页 > 解决方案 > 如何使用 GDI+ 在 C++ 中更改常用控件(如按钮、标签等)的文本字体样式?

问题描述

我正在尝试使用 GDI+ 更改按钮的字体样式,但我不知道该怎么做。

我的按钮 -

HWND btn = CreateWindow(L"BUTTON", L"My button", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 50, 50, 100, 300, hWnd, NULL, NULL, NULL);

我已经初始化了 GDI+ -

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

并关闭它 -

GdiplusShutdown(gdiplusToken);

请提供任何帮助

标签: visual-c++fontsvisual-studio-2019gdi+common-controls

解决方案


通用控件不使用 GDI+。你需要使用LOGFONT。

HWND TextBox;
TextBox = CreateWindowW(WC_EDIT, L"", WS_BORDER | WS_VISIBLE | WS_CHILD, 330, 130, 300, 100, hWnd, NULL, NULL, NULL);

LOGFONT logfont; 
ZeroMemory(&logfont, sizeof(LOGFONT));

logfont.lfCharSet = DEFAULT_CHARSET; //Font
logfont.lfHeight = 20; //Font Height
logfont.lfWidth = 23; //Font Width
logfont.lfWeight = 300; //Font Weight

//If want
logfont.lfItalic = true; // Italic (Boolean)
logfont.lfUnderline = true; // Underline (Boolean)


HFONT hFont = CreateFontIndirect(&logfont);   
SendMessage(TextBox, WM_SETFONT, (WPARAM)hFont, TRUE);

推荐阅读