How do I use the default GUI theme on Windows?

本秂侑毒 提交于 2019-12-24 18:59:30

问题


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

回答1:


Try this code before calling MessageBox

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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4403458/how-do-i-use-the-default-gui-theme-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!