15

根据文档

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

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

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

4

3 回答 3

32

实际上有第三种方式,没有任何清单,虽然它相当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;
}
于 2012-05-04T06:57:37.370 回答
15

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

#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
于 2010-11-29T22:02:26.643 回答
7

如果您继续阅读,您会找到答案

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

#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' 
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
于 2010-11-29T22:04:20.113 回答