以前我经常使用 VB6 来开发几个个人项目。升级到 Windows 7 后,我决定使用 vb.net Express Edition 2010。
如果我希望我的 VB6 应用程序与 Windows 的视觉风格融为一体,我将使用 此处。简而言之,我将在应用程序中使用清单文件和几次调用,并且大多数元素看起来与应用的 XP 主题类似。如果它在 2000、95 或 98 上运行,那么它看起来就像一个标准的 Windows 应用程序。一切都很好。
现在我已经转向 vb.net,我编写了一个简单的“Hello,world”应用程序,但我完全不知道如何使其看起来像 Windows 7 主题(例如,字体与系统字体和小部件的样式正确)。
仅仅更改字体是一种黑客行为,并且在设置不同或运行默认字体不同的不同版本的 Windows 的计算机上看起来不合适。
无论 Windows 版本如何,如何确保我的应用程序与应用的 Windows 主题相匹配?
Previously I used to piddle around with VB6 to develop a couple of personal projects. Following my upgrade to Windows 7, I've decided to piddle about with vb.net Express Edition 2010.
If I wanted my VB6 application to blend in with the visual style of Windows, I would use the code and techniques described here. In short, I would use a Manifest file and a couple of calls within the application and most of the elements would look similar to the XP theme applied. If it was run on 2000, 95 or 98 then it would look like a standard Windows app. All was good.
Now I've moved onto vb.net, I've written a simple "Hello, world" application but I have absolutely no idea on how to make it look like the Windows 7 theme (eg. the font matches the system font and the widgets are styled correctly).
Just changing the font is a hack and will look out of place on machines that are set-up differently or run a different version of Windows where the default font is different.
How do I ensure my application matches the applied Windows theme irrespective of the version of Windows?
发布评论
评论(1)
如果您创建 Windows 窗体应用程序,其中很多操作都是自动完成的。他们(大部分)将使用标准的本机 Windows 控件,这些控件用主题颜色绘制自己。但也有例外:
表单项模板使用名为 Microsoft Sans Serif 的默认字体。您必须将其更改为 Segoe UI 以匹配 Vista/Win7 默认值。这仅对于 Form 类是必需的,您放置在其上的所有控件都会自动继承该字体。在 XP 计算机上,Windows 字体映射器会注意到字体丢失并自动回退到 MSS。
MenuStrip 类使用自定义渲染来绘制菜单项。当您将 RenderMode 属性更改为 System 时,它会尝试匹配 Windows 样式,但它的绘制方式与 Win7 样式不匹配。右键单击工具箱,选择项目,然后选择主菜单。这是一个旧版本,使用 Windows 来绘制菜单,因此它会生成正确的主题外观。
ToolStrip 的问题非常相似。它的旧版本是 ToolBar。这是一个难以接受的问题,它没有使用钢筋,这使得工具栏看起来又平又难看。
WPF 中也存在类似的问题,但附加的问题是 WPF 不使用任何标准 Windows 控件。并且在微妙的地方出错。
A lot of this is automatic if you create a Windows Forms app. They will (mostly) use the standard native Windows controls which draw themselves with the theme colors. But there are exceptions:
the Form item template uses a default Font named Microsoft Sans Serif. You'll have to change it to Segoe UI to match the Vista/Win7 default. This is only necessary for the Form class, all controls you put on it will automatically inherit that font. On an XP machine, the Windows font mapper will notice that the font is missing and automatically fall back to MSS.
the MenuStrip class uses custom rendering to draw the menu items. It tries to match the Windows style when you change the RenderMode property to System but the way it draws doesn't match the Win7 style. Right-click the toolbox, Choose Items and select MainMenu. That's a legacy version that does use Windows to draw menus so it produces the proper theme appearance.
A very similar problem for ToolStrip. It's legacy version is ToolBar. This is a hard one to swallow, it doesn't use a rebar which make the tool bar look flat and ugly.
There are similar problems in WPF but with the added problem that WPF doesn't use any of the standard Windows controls. And gets it wrong in subtle places.