MFC对话框控件的默认字体是哪种? [英] Which Font is the default for MFC Dialog Controls?

查看:183
本文介绍了MFC对话框控件的默认字体是哪种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的图片(放大后,您会更好地看到它们的区别)显示了动态创建的Edit控件(上面的两个示例)和从Dialog Editor创建的Edit控件(下面的示例)之间的Font差异.如何使动态创建的CEdit控件的字体看起来像默认字体(下面的示例)?

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)?

我已经创建了如下的CEdit控件:

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"));

感谢您的帮助!

推荐答案

第一个示例使用系统字体(SYSTEM_FONT),该字体是通过 Raymond Chen的博客 Michael Kaplan的博客.

The first example is using the System font (SYSTEM_FONT), as retrieved with the GetStockObject 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.

第二个示例使用,就像您要求的那样.实际上,它映射为一种名为"Microsoft Sans Serif"或"MS Sans Serif"的字体,这是Windows 95和98时代的UI字体.这也称为DEFAULT_GUI_FONT,它确实曾经是Windows的准确名称.它,但是可惜,它不再准确了.

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.

从Windows 2000(在XP中继续使用)开始,Tahoma被用作默认的UI字体.这是您在第三个示例中看到的:Tahoma 8 pt.不幸的是,即使在那些操作系统上,"MS Shell Dlg"也不会返回Tahoma -它仍会返回MS Sans Serif,这就是为什么它看起来不对的原因.

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.

因此,您可以简单地将Tahoma指定为GUI字体,但这并不是真正正确的,因为它会在未安装或不支持Tahoma的较旧版本的OS或Windows XP的外语版本中中断.操作系统,必要时使用其他字体.相反,您应该指定DS_SHELLFONT标志, Raymond会说.关于这里.

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.

在Windows Vista出现之前,一切都很好.在Windows Vista中,Microsoft的权力决定了Tahoma变得有些长牙了,而Windows应该是 Segoe UI ,据说是为了在屏幕上达到最佳效果而设计的可读性.经过一番特殊的修改后,他们决定默认大小应为 9 pt ,而不是操作系统的每个以前版本使用的8 pt,而不管字体是什么字体.您可能会大概认为"MS Shell Dlg","MS Shell Dlg2"或DS_SHELLFONT(或全部三个)会为您提供这种新型的Segoe UI字体,但是您会是错的.

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.

哦,哦.现在,事情变得棘手了……Vis​​ta不仅使用不同于XP的字体(不易使用一刀切"的所有标识符进行访问),而且还使用了不同的 size ,从而更改了对话框在这些系统上的显示方式(如果可以显示的话).在许多地方,Windows Shell团队似乎只是在挑战一下-在应该使用Segoe UI的情况下,即使启用了Aero主题,Tahoma 8 pt仍在整个地方使用 9点这种事情确实使UI看起来不整洁,这在Vista的早期就成为了很多挑剔的主题.现在,似乎大多数人已经忘记了它,但是UI并没有开始看起来更加分散和不一致.

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.

您不是Windows Shell团队:您无法在自己的应用程序中摆脱困境. 顶部Windows Vista用户体验的规则甚至明确指出您应该始终:

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:

  • 使用新Windows Vista系统字体Segoe UI.
  • 通过始终使用Windows Theme API引用系统字体,大小和颜色来尊重用户的设置.请勿对字体,大小或颜色使用固定值.
  • Use Segoe UI, the new Windows Vista system font.
  • Respect the user's settings by always referencing the system font, sizes, and colors using the Windows Theme APIs. Don't use fixed values for fonts, sizes, or colors.

说实话,我还没有真正听说过针对这个问题的解决方案.而且我怀疑到我这样做的时候,已经没有人需要支持Windows XP了(尽管大多数人还没有.)但是,我的工作是:我在运行时使用 SystemParametersInfo函数.幸运的是,无论当前版本的Windows和用户选择的主题如何,系统消息框字体(lfMessageFont)都是正确的字体和大小.

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.

我用来初始化窗口或对话框的代码通常看起来像这样(SystemInfo::IsVistaOrLater是我编写的一个辅助函数;其实现很明显):

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):

// Get the system message box font
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);

// If we're compiling with the Vista SDK or later, the NONCLIENTMETRICS struct
// will be the wrong size for previous versions, so we need to adjust it.
#if(_MSC_VER >= 1500 && WINVER >= 0x0600)
if (!SystemInfo::IsVistaOrLater())
{
    // In versions of Windows prior to Vista, the iPaddedBorderWidth member
    // is not present, so we need to subtract its size from cbSize.
    ncm.cbSize -= sizeof(ncm.iPaddedBorderWidth);
}
#endif

SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
HFONT hDlgFont = CreateFontIndirect(&(ncm.lfMessageFont));

// Set the dialog to use the system message box font
SetFont(m_DlgFont, TRUE);
SendMessage(hWnd, WM_SETFONT, (WPARAM)hDlgFont, MAKELPARAM(FALSE, 0));

使用便捷的SendMessageToDescendants方法甚至在MFC中更轻松
(m_DlgFont是为该类定义的CFont对象):

Or even easier in MFC, with the handy SendMessageToDescendants method
(m_DlgFont is a CFont object defined for the class):

// Get the system message box font
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
LOGFONT lfDlgFont = ncm.lfMessageFont;
m_DlgFont.CreateFontIndirect(&lfDlgFont);

// Set the dialog and all its controls to use the system message box font
SetFont(m_DlgFont, TRUE);
SendMessageToDescendants(WM_SETFONT, (WPARAM)m_DlgFont.m_hFont, MAKELPARAM(FALSE, 0), TRUE);

如果您不使用MFC,我强烈建议您实现自己的SendMessageToDescendants递归版本.它将初始化代码简化了 lot .

If you're not using MFC, I highly recommend implementing your own recursive version of SendMessageToDescendants. It makes the initialization code a lot simpler.

这篇关于MFC对话框控件的默认字体是哪种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆