如何在没有清单的情况下启用视觉样式

时间:2010-11-29 21:57:48

标签: c++ winapi comctl32 visual-styles

根据docs

“如果您希望应用程序使用ComCtl32.dll版本6,则必须添加应用程序清单或编译器指令,以指定应该使用版本6(如果可用)。”

注意上面的逻辑OR?那么这个神秘的编译器指令是什么?

我有一个原生的Win32 C ++应用程序,它完全包含在一个.cpp文件中。没有资源文件,清单文件等。我想保持这种方式,但我也想使用视觉样式。

3 个答案:

答案 0 :(得分:25)

实际上还有第三种方式没有任何明显的,虽然它相当hacky:

#include <windows.h>

// NOTE: It is recommended that you delay-load ComCtl32.dll (/DelayLoad:ComCtl32.dll)
// and that you ensure this code runs before GUI components are loaded.
// Otherwise, you may get weird issues, like black backgrounds in icons in image lists.
ULONG_PTR EnableVisualStyles(VOID)
{
    TCHAR dir[MAX_PATH];
    ULONG_PTR ulpActivationCookie = FALSE;
    ACTCTX actCtx =
    {
        sizeof(actCtx),
        ACTCTX_FLAG_RESOURCE_NAME_VALID
            | ACTCTX_FLAG_SET_PROCESS_DEFAULT
            | ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID,
        TEXT("shell32.dll"), 0, 0, dir, (LPCTSTR)124
    };
    UINT cch = GetSystemDirectory(dir, sizeof(dir) / sizeof(*dir));
    if (cch >= sizeof(dir) / sizeof(*dir)) { return FALSE; /*shouldn't happen*/ }
    dir[cch] = TEXT('\0');
    ActivateActCtx(CreateActCtx(&actCtx), &ulpActivationCookie);
    return ulpActivationCookie;
}

答案 1 :(得分:11)

如果您使用的是Visual Studio,则可以将此行添加到stdafx.cpp中,例如:

#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

答案 2 :(得分:6)

如果你一直在阅读,你会找到the answer

  

如果您使用的是Microsoft Visual C ++ 2005或更高版本,则可以将以下编译器指令添加到源代码中,而不是手动创建清单。为了便于阅读,该指令在这里分为两行。

#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' 
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")