windows 如何使 Win32 API 窗口看起来更现代?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3323541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make the Win32 API window more modern looking?
提问by Machiel
I ordered Programming Windows Fifth Edition a few days ago, and started working with it.
几天前我订购了 Programming Windows Fifth Edition,并开始使用它。
I'm starting to learn the win32 api, however, I got a question. The windows do not look modern winxp/win vista/win 7 style at all. How do I fix this?
我开始学习 win32 api,但是,我有一个问题。窗户看起来根本不像现代的 winxp/win vista/win 7 风格。我该如何解决?
It currently looks like this, crap font and all.
它目前看起来像这样,垃圾字体等等。
Thanks in advance!
提前致谢!
Machiel
马基尔
回答by StackedCrooked
To get the font right you should call this after CreateWindow(Ex)
:
要获得正确的字体,您应该在以下之后调用它CreateWindow(Ex)
:
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
HFONT hFont = ::CreateFontIndirect(&ncm.lfMessageFont);
::SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
回答by Billy ONeal
You didn't, apparently, actually read the book. You're looking for WM_SETFONT. There is a reason the common controls aren't the first thing the book covers.
显然,你没有真正读过这本书。您正在寻找WM_SETFONT。公共控件不是本书涵盖的第一件事是有原因的。
回答by Brook Miles
You might want to check GetThemeSysFont
to fill a LOGFONT
of an appropriate system font, create it using CreateFontIndirect
, and WM_SETFONT
to assign it to each control that you create.
您可能需要检查GetThemeSysFont
以填充LOGFONT
适当的系统字体,使用 来创建它CreateFontIndirect
,并将WM_SETFONT
其分配给您创建的每个控件。
To my knowledge there is no way to set a different default for newly created windows in your application. Nor is there a way to set all of the windows that you've already created in a single step (ie. instead of just looping through them all, or assigning individually). The exception is dialog boxes which when created from resources allow the resource to specify the font used for all of the controls on the dialog box.
据我所知,无法为应用程序中新创建的窗口设置不同的默认值。也没有办法在一个步骤中设置您已经创建的所有窗口(即,而不是仅仅循环遍历它们,或单独分配)。例外是对话框,当从资源创建时,允许资源指定用于对话框上所有控件的字体。
回答by Anders
You need to set the font for each control with WM_SETFONT, you create the font by passing NONCLIENTMETRICS.lfMessageFont to CreateFontIndirect(Use SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ...) to get NONCLIENTMETRICS)
您需要使用WM_SETFONT为每个控件设置字体,通过将NONCLIENTMETRICS.lfMessageFont传递给CreateFontIndirect来创建字体(使用SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ...) 来获取 NONCLIENTMETRICS)
For dialog boxes, you use the pseudo font "MS Shell Dlg" @ 8pt on < Vista and "Segoe UI" @ 9pt on >= Vista
对于对话框,在 < Vista 上使用伪字体“ MS Shell Dlg”@8pt,在 >=Vista 上使用“Segoe UI”@9pt
回答by DevinEllingson
You might want to post some screen shots of exactly what differences you are talking about, this would help in figuring out what you need to change.
您可能想发布一些屏幕截图,说明您所谈论的差异到底是什么,这将有助于确定您需要更改的内容。
In general I would say that you probably need to include an approprite manifest with your app so your app uses the latest common controls.
一般来说,我会说您可能需要在您的应用程序中包含一个适当的清单,以便您的应用程序使用最新的通用控件。
Also, these days most UI is not developed using SDK style code, this is very difficult to program/maintain, instead use some kind of UI library, MFC at the very least.
此外,现在大多数 UI 不是使用 SDK 样式代码开发的,这非常难以编程/维护,而是使用某种 UI 库,至少是 MFC。