在 Windows GUI 中更改静态文本框的字体大小

问题描述:

如何在用 C++ 编写的 Windows GUI 应用程序中更改静态文本框的字体大小?

how do you change the font size of a static text box in a Windows GUI application written in C++?

HWND hText = CreateWindowW(L"EDIT", L"enter some text", WS_VISIBLE | WS_CHILD | ES_RIGHT, 100, 100, 100, 50, hWnd, NULL, NULL, NULL);

我是否需要制作另一个窗口消息

do i have to make another Window message

正如@RbMm 所说,使用 CreateFontWM_SETFONT 可以实现这一点.并且官方文档也有相应的介绍.

As @RbMm said that, use CreateFont and WM_SETFONT can achieve this. And the official documents also have corresponding introduction.

更改编辑控件使用的字体.

应用程序可以更改编辑控件使用的字体发送 WM_SETFONT 消息.大多数应用程序都会这样做处理 WM_INITDIALOG 消息.改变字体不会更改编辑控件的大小;发送的应用程序WM_SETFONT 消息可能需要检索文本的字体规格并重新计算编辑控件的大小.更多有关字体和字体规格的信息,请参阅字体和文本.

An application can change the font that an edit control uses by sending the WM_SETFONT message. Most applications do this while processing the WM_INITDIALOG message. Changing the font does not change the size of the edit control; applications that send the WM_SETFONT message may have to retrieve the font metrics for the text and recalculate the size of the edit control. For more information about fonts and font metrics, see Fonts and Text.

最少的代码:

LOGFONT logfont; 
ZeroMemory(&logfont, sizeof(LOGFONT));
logfont.lfCharSet = DEFAULT_CHARSET;
logfont.lfHeight = -20; 
HFONT hFont = CreateFontIndirect(&logfont);

SendMessage(hText, WM_SETFONT, (WPARAM)hFont, TRUE);