开发者

How do I use the default GUI theme on Windows?

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"/>
<des开发者_C百科cription>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


Try this code before calling MessageBox

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜