在 C + Win API 中编程:如何让 Windows 7 查找控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5663091/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:38:43  来源:igfitidea点击:

Programming In C + Win API: How To Get Windows 7 Look For Controls?

cwindowswinapibuttonvisual-styles

提问by Ishan Sharma

I am programming strictly in C and WinAPI, no C++ or C#. I am a beginner and just learning to draw controls etc. The thing is that when I create Windows or other controls like Command Buttons, they have Windows Native look. Take a look at this:

我严格使用 C 和 WinAPI 进行编程,没有使用 C++ 或 C#。我是初学者,刚刚学习绘制控件等。问题是,当我创建 Windows 或其他控件(如命令按钮)时,它们具有 Windows 本机外观。看看这个:

This is look I am getting!

这是我得到的样子!

But in Windows 7, the command buttons look like this:

但在 Windows 7 中,命令按钮如下所示:

enter image description here

在此处输入图片说明

Now, how do I get command buttons in my program to look like that. Is it even possible? I am following this tutorial, for reference: http://zetcode.com/gui/winapi/

现在,如何让我的程序中的命令按钮看起来像那样。甚至有可能吗?我正在关注本教程,以供参考:http: //zetcode.com/gui/winapi/

Thanks.

谢谢。

采纳答案by GSerg

You enable visual styles for your app by providing an XML manifest, either as a separate file or as an embedded resource. See Enabling Visual Stylesfor details.

您可以通过提供 XML 清单(作为单独的文件或嵌入的资源)来为您的应用程序启用视觉样式。有关详细信息,请参阅启用视觉样式

回答by Cody Gray

That's how the controls looked back in the 1990s, before themes (visual styles) were invented. As you've noticed, modern buttons are now painted all fancy-pants with gradients and throbbing and all that. But for backwards-compatibility reasons, you have to specifically requestthat your controls get that treatment, or they'll fall back to the legacy style.

这就是 1990 年代的控件在主题(视觉样式)被发明之前的样子。正如您所注意到的,现代纽扣现在都被涂上了渐变色和跳动等等所有花哨的裤子。但是出于向后兼容性的原因,您必须特别要求您的控件得到这种处理,否则它们将退回到旧样式。

You do that by specifying a manifest. You can either add one in plain text format to your application's root directory, or you can have Visual Studio (2005 and later) automatically embed one in your EXE.

您可以通过指定清单来做到这一点。您可以将纯文本格式的一个添加到应用程序的根目录中,也可以让 Visual Studio(2005 及更高版本)自动将一个嵌入到您的 EXE 中。

The second route is the way I'd go. Add the following code to your stdafx.hfile to inform the compiler that you want it to add the manifest automatically upon building your project:

第二条路是我要走的路。将以下代码添加到您的stdafx.h文件中,以通知编译器您希望它在构建项目时自动添加清单:

#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif

This MSDN articlehas more information on visual styles than you could ever want.

这篇 MSDN 文章提供了比您想要的更多的视觉样式信息。


And if you really want your application to look native, you need to change the background brush used for the main window. By default, it's set to use the same color as a textbox's background (white).


如果您真的希望您的应用程序看起来是原生的,您需要更改用于主窗口的背景画笔。默认情况下,它设置为使用与文本框背景相同的颜色(白色)。

You want to use the color used to paint 3D controls, instead. Modify the hbrBackgroundmember of your WNDCLASS(or WNDCLASSEX) structure as shown here:

您想改用用于绘制 3D 控件的颜色。修改hbrBackground您的WNDCLASS(或WNDCLASSEX)结构的成员,如下所示:

wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);

Why do you have to add 1? For backwards-compatibility reasons, again. The details are boring. :-)

为什么一定要加1?再次出于向后兼容性的原因。细节很无聊。:-)

回答by Renato Silva

You probably remember when Windows XP arrived with that fresh new look, and you could revert it back to Windows 95/2000 classic appearance, or you could change the color theme. That was called visual styles, which brought to Windows a better theming support.

您可能还记得 Windows XP 以全新的外观出现时,您可以将其恢复为 Windows 95/2000 的经典外观,或者您可以更改颜色主题。这就是所谓的视觉样式,它为 Windows 带来了更好的主题支持。

Classic, Luna, Aero and Metro

Classic、Luna、Aero 和 Metro

Visual styles remained as a featuresince then. Windows Vista and 7 introduced Aero, but they still allowed using the classic theme. Windows 8 has removed both Aero and the classic theme, and added the Metro theme. High-contrast themes are now part of visual styles and do not have the classic appearance anymore.

从那时起,视觉风格一直是一个特色。Windows Vista 和 7 引入了 Aero,但它们仍然允许使用经典主题。Windows 8 删除了 Aero 和经典主题,并添加了 Metro 主题。高对比度主题现在是视觉风格的一部分,不再具有经典外观。

Thus, a classic themed application in Windows 7 when it was expected an Aero style, is an issue with the visual style of the application. I'm not sure why but Microsoft did not leave the old classic theme optional while migrating the API to the new look.

因此,Windows 7 中的经典主题应用程序在预期为 Aero 风格时,是应用程序视觉风格的问题。我不知道为什么,但微软在将 API 迁移到新外观时没有保留旧的经典主题可选。

So, how do I apply the modern style?

那么,我该如何应用现代风格呢?

enter image description here

在此处输入图片说明

Either way this MSDN articleexplains how to enable visual styles for your application. I have seen people saying that enabling Unicode (defining UNICODE) would make it, but it is notrequired. Basically, you just need to write a manifest file specifying version 6 of the common controls DLL as requirement, then embed it as a resource or place under appname.exe.manifestin the executable's directory. A sample manifest would look like this:

无论哪种方式,这篇 MSDN 文章都解释了如何为您的应用程序启用视觉样式。我看到有人说启用 Unicode(定义UNICODE)会成功,但这不是必需的。基本上,您只需要编写一个清单文件,将公共控件 DLL 的第 6 版指定为要求,然后将其作为资源嵌入或放置appname.exe.manifest在可执行文件的目录下。示例清单如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity version="1.0.0.0" processorArchitecture="*"
        name="CompanyName.ProductName.YourApplication" type="win32"/>
    <description>Your application description here.</description>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0" type="win32" processorArchitecture="*"
                publicKeyToken="6595b64144ccf1df" language="*"/>
        </dependentAssembly>
    </dependency>
</assembly>

If you go for the embed option, don't forget to have winuser.hincluded in your resource file. You may also want to initialize the common controls DLL in your WinMain, according to this MinGW guide:

如果您选择嵌入选项,请不要忘记将其winuser.h包含在您的资源文件中。您可能还需要初始化通用控件在DLL中WinMain,根据本MinGW的指南

I've seen this step missed in some examples, and it can cause some odd behaviors under certain operating system versions and certain conditions, such as buttons being invisible, or dialogs failing to be created.

我在一些示例中看到过这一步,它可能会在某些操作系统版本和某些条件下导致一些奇怪的行为,例如按钮不可见,或无法创建对话框。

INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icc);