MFC 对话框控件的默认字体是什么?
下图(放大,以便您更好地看到差异)显示了动态创建的编辑控件(上面的两个示例)和从对话框编辑器创建的编辑控件(下面的示例)之间的字体差异。如何使动态创建的 CEdit 控件的字体看起来像默认字体(下面的示例)?
我创建了如下所示的 CEdit 控件:
obj->CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""),
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
rect.left, rect.top, rect.Width(), rect.Height(),
GetSafeHwnd(), reinterpret_cast<HMENU>(mId));
obj->SetFont(&mFont); // mFont was created in the Dialog Constructor
// with mFont.CreatePointFont(80, _T("MS Shell Dlg"));
感谢您的帮助!
The picture below (enlarged, so you better see the differences) shows Font differences between dynamically created Edit controls (the upper two examples) and Edit Controls created from the Dialog Editor (the lower example). How can I make the font of my dynamically created CEdit controls looking like the default (the lower example)?
I have created the CEdit Controls like following:
obj->CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""),
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
rect.left, rect.top, rect.Width(), rect.Height(),
GetSafeHwnd(), reinterpret_cast<HMENU>(mId));
obj->SetFont(&mFont); // mFont was created in the Dialog Constructor
// with mFont.CreatePointFont(80, _T("MS Shell Dlg"));
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个示例是使用系统字体 (
SYSTEM_FONT
),通过GetStockObject
函数,这是一种位图字体,自 Windows 3 以来就不再使用。更多信息请访问 Raymond Chen 的博客,以及迈克尔·卡普兰的博客。第二个示例是使用 “MS Shell Dlg”字体,就像您要求的那样。这实际上映射到一种名为“Microsoft Sans Serif”或“MS Sans Serif”的字体,这是 Windows 95 和 98 时代的 UI 字体。这也称为
DEFAULT_GUI_FONT
,它确实曾经用于是它的准确名称,但可惜,它不再准确了。从 Windows 2000 开始(并在 XP 中继续使用),Tahoma 被用作默认 UI 字体。这就是您在第三个示例中看到的内容:Tahoma 8 pt。不幸的是,即使在这些操作系统上,“MS Shell Dlg”也不会返回 Tahoma,它仍然返回 MS Sans Serif,这就是它看起来错误的原因。
因此,您可以简单地将 Tahoma 指定为 GUI 字体,但这实际上并不正确,因为它会在未安装或支持 Tahoma 的旧版本操作系统中或在外语版本的操作系统中中断,出于必要而使用不同字体的地方。相反,您应该指定
DS_SHELLFONT
标志,该标志一切都很好,直到 Windows Vista 出现。在 Windows Vista 中,Microsoft 的权力认为 Tahoma 有点老了,Windows 应该另一个 UI 字体升级。他们内部开发了自己的特殊字体,名为 Segoe UI,据说是为了获得最佳的屏幕可读性而设计的。在一个特殊的小改动中,他们决定默认大小现在应该为 9 pt,而不是每个以前版本的操作系统使用的 8 pt,无论字体如何。您可能认为“MS Shell Dlg”、“MS Shell Dlg2”或
DS_SHELLFONT
(或全部三个)都会为您提供这种新奇的 Segoe UI 字体,但你错了。呃哦。现在事情变得棘手了... Vista 不仅使用了与 XP 不同的字体,无法通过通用的标识符轻松访问,而且还使用了不同的大小,从而改变了如果您可以让对话框显示出来,那么您的对话框将在这些系统上显示出来。在很多很多地方,Windows shell 团队似乎只是在挑战——Tahoma 8 pt到处都使用,即使启用了 Aero 主题,而它应该使用 Segoe UI 9分。这种事情确实让 UI 看起来很粗糙,而且在 Vista 的早期,它也是很多人挑剔的话题。现在,似乎大多数人已经忘记了它,但用户界面并没有开始看起来不再那么分散和不一致。
而且您不是 Windows shell 团队:您无法在自己的应用程序中摆脱这种情况。 主要规则对于 Windows Vista 用户体验,甚至明确指出您应该始终:
老实说,我还没有真正听说过解决这个问题的好的解决方案。我怀疑,到我这样做的时候,就没有人需要再支持 Windows XP了(尽管大多数人还没有完全支持 Windows XP)。但这就是我所做的:我使用
SystemParametersInfo
函数。幸运的是,无论当前的 Windows 版本和用户选择的主题如何,系统消息框字体 (lfMessageFont
) 的字体和大小都是正确的。我初始化窗口或对话框的代码通常看起来像这样(
SystemInfo::IsVistaOrLater
是我编写的一个辅助函数;实现是显而易见的):或者在 MFC 中更容易,使用方便的 <代码>SendMessageToDescendants 方法
(
m_DlgFont
是为该类定义的CFont
对象):如果您不使用 MFC,我强烈建议您实现自己的
SendMessageToDescendants
递归版本>。它使初始化代码变得更加简单。The first example is using the System font (
SYSTEM_FONT
), as retrieved with theGetStockObject
function, which is a bitmap font that has not been used since the days of Windows 3. More information is available on Raymond Chen's blog, and Michael Kaplan's blog.The second example is using the "MS Shell Dlg" font, just like you asked it to. That actually maps to a font called "Microsoft Sans Serif" or "MS Sans Serif", the UI font back in the days of Windows 95 and 98. This is also known as
DEFAULT_GUI_FONT
, which indeed used to be an accurate name for it, but alas, it is accurate no longer.Beginning with Windows 2000 (and continued in XP), Tahoma was used as the default UI font. This is what you are seeing in the third example: Tahoma 8 pt. Unfortunately, even on those operating systems, "MS Shell Dlg" does not return Tahoma--it still returns MS Sans Serif, which is why it looks wrong.
So, you could simply specify Tahoma as the GUI font, but that wouldn't really be correct, because it would break in older versions of the OS where Tahoma isn't installed or supported, or on foreign language versions of the operating system, where a different font is used out of necessity. Instead, you're supposed to specify the
DS_SHELLFONT
flag, which Raymond talks about here.And all was fine and good until Windows Vista came out. And in Windows Vista, the powers that be at Microsoft decided that Tahoma was getting a little long-in-the-tooth and Windows was due for another UI font upgrade. They developed their own special font in-house called Segoe UI, supposedly designed for optimum on-screen readability. And in a special little twist, they decided that the default size should now be 9 pt, instead of 8 pt as used by every previous version of the OS, regardless of the font face. And you would probably think that either "MS Shell Dlg", "MS Shell Dlg2", or
DS_SHELLFONT
(or all three) would get you this new-fangled Segoe UI font, but you'd be wrong.Uh oh. Now things get tricky... Not only does Vista use a different font than XP that is not easily accessible with a one-size-fits-all identifier, but it also uses a different size, changing the way your dialog will look on those systems, if you can get it to display at all. In many, many places, the Windows shell team appeared to simply punt the challenge--Tahoma 8 pt is used all over the place, even with the Aero theme enabled, when it's supposed to be using Segoe UI 9 pt. This kind of thing really makes the UI look unpolished, and it was the subject of lots of nitpicking back in the early days of Vista. Now, it seems most people have forgotten about it, but the UI hasn't started looking any less scattered and inconsistent.
And you're not the Windows shell team: you can't get away with this in your own app. The Top Rules for the Windows Vista User Experience even state explicitly that you should always:
To be honest, I haven't really heard a good solution to this problem yet. And I suspect that by the time I ever do, no one will need to support Windows XP anymore (although most people aren't quite there yet). But here's what I do: I extract the default system font at runtime using the
SystemParametersInfo
function. Fortunately, the system message box font (lfMessageFont
) is the correct font face and size, regardless of the current version of Windows and the user's chosen theme.My code to initialize windows or dialogs generally looks something like this (
SystemInfo::IsVistaOrLater
is a helper function I've written; the implementation is the obvious):Or even easier in MFC, with the handy
SendMessageToDescendants
method(
m_DlgFont
is aCFont
object defined for the class):If you're not using MFC, I highly recommend implementing your own recursive version of
SendMessageToDescendants
. It makes the initialization code a lot simpler.在我的项目中,我从主对话框复制字体。但主对话框必须由对话框编辑器构建。
In my projects i copy the font from the main dialog. But the main dialog must be built by the Dialog Editor.