如何在 Windows 上使用默认 GUI 主题?

发布于 2024-10-07 05:46:47 字数 1772 浏览 2 评论 0原文

我正在使用 winapi 编写 GUI,但我的应用程序看起来像 Windows 98 主题。 如何使用当前的窗口主题?

我尝试创建清单文件,但它不起作用。

Test.cpp

# include <windows.h>

int WINAPI WinMain(
    HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow
){
    MessageBox(NULL, "Hello World!", "Hello", MB_OK);
    return 0;
}

Test.exe.Manifest

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

Manifest.rc

1 24 "Test.exe.Manifest"

我在 Windows XP SP3 上使用 MinGW 编译器进行编译。

g++ Test.cpp -c
windres Manifest.rc -O coff -o Manifest.res
g++ Test.o Manifest.res -o Test.exe -Wl,-subsystem,windows

但是当我运行可执行文件时,它会关闭。如果没有清单和资源文件,它也可以工作,但使用 Windows 98 主题。

谢谢


编辑:

谢谢,现在可以了。

我从教程中复制了清单文件,但没有调用 InitCommonControls()。

InitCommonControls() 有效。

我必须编辑我的 commctrl.h,因为 InitCommonControlsEx() 在那里被禁用,但现在可以工作。

链接器命令已更改:

g++ Test.o Manifest.res -o Test.exe -Wl,-subsystem,windows -lcomctl32

I am programming a GUI with winapi, but my application looks like Windows 98 theme.
How do I use the current window theme?

I tried to create a manifest file, but it does not work.

Test.cpp

# include <windows.h>

int WINAPI WinMain(
    HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow
){
    MessageBox(NULL, "Hello World!", "Hello", MB_OK);
    return 0;
}

Test.exe.Manifest

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

Manifest.rc

1 24 "Test.exe.Manifest"

I compiled with MinGW compiler on Windows XP SP3.

g++ Test.cpp -c
windres Manifest.rc -O coff -o Manifest.res
g++ Test.o Manifest.res -o Test.exe -Wl,-subsystem,windows

But when I run the executable file, it closes. Without the manifest and resource files, it works, but uses Windows 98 theme.

Thanks


Edit:

Thanks, now it works.

I copied the manifest fie from a tutorial and I did not call InitCommonControls().

InitCommonControls() works.

I had to edit my commctrl.h, because InitCommonControlsEx() was disabled there, and now works.

Linker command changed:

g++ Test.o Manifest.res -o Test.exe -Wl,-subsystem,windows -lcomctl32

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日浅笑〃 2024-10-14 05:46:47

在调用 MessageBox 之前尝试此代码

INITCOMMONCONTROLSEX iccx = { sizeof(INITCOMMONCONTROLSEX), ICC_COOL_CLASSES | ICC_BAR_CLASSES};
::InitCommonControlsEx(&iccx);

Try this code before calling MessageBox

INITCOMMONCONTROLSEX iccx = { sizeof(INITCOMMONCONTROLSEX), ICC_COOL_CLASSES | ICC_BAR_CLASSES};
::InitCommonControlsEx(&iccx);
玩世 2024-10-14 05:46:47

您的 MinGW 安装是最新的吗?不再有XP,但在Win7上尝试过,消息框使用Vista/7风格(右下角的“确定”按钮,白色/灰色背景等)。

您不必提供任何编译器开关,只需运行 g++ test.cpp -o test.exe 即可在 Windows 下创建 Win32 可执行文件。

不过,我记得在使用 Visual Basic 6 时也遇到过类似的问题。我认为您必须初始化应用程序内的通用控件(抱歉,现在不记得调用了)。如果您不这样做,则强制通过清单文件将导致崩溃。我对这个不是百分百确定,已经好几年了……但我会先尝试查找一下。同样出于测试目的,您不必将清单包含在可执行文件中。只需将其放在它旁边作为“test.exe.manifest” - 这应该也可以工作,并且允许您使用/不使用清单文件进行测试而无需重新编译。

编辑:请参阅 DreJ 的回答 - 这也是我提到的电话。

Is your MinGW installation up to date? No longer have XP but tried on Win7 and the Message Box uses the Vista/7 style ("OK" button on bottom right, white/gray background, etc.).

You shouldn't have to provide any compiler switches, should be enough to just run g++ test.cpp -o test.exe to create a Win32 executable under Windows.

However I remember having similar issues when still working with Visual Basic 6. I think you have to init the common controls inside your application (sorry, don't remember the call right now). If you don't do it, forcing it through the manifest file will result in a crash. I'm not 100% sure on this one, been years ... but I'd try to lookup that first. Also for testing purposes, you shouldn't have to include the manifest inside your executable. Just put it next to it as "test.exe.manifest" - that should work as well and allows you to test with/without the manifest file without recompiling.

Edit: See DreJ's Answer - that's the call I was refering too.

爱情眠于流年 2024-10-14 05:46:47

有一个有用的指南

MSDN上 在此特定实例中,您似乎没有链接到 comctl32.lib,并且没有调用 InitCommonControls 或 InitCommonControlsEx 来准备新的视觉样式以供使用。

There's a helpful guide on MSDN

In this particular instance, it appears that you are not linking to comctl32.lib, and you are not calling InitCommonControls or InitCommonControlsEx to prepare the new visual styles for usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文