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

问题描述 投票:0回答:3

根据文档

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

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

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

c++ winapi comctl32 visual-styles activation-context-api
3个回答
34
投票

实际上还有第三种方法,没有任何清单,尽管它相当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;
}

20
投票

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

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

9
投票

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

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

#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' 
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
© www.soinside.com 2019 - 2024. All rights reserved.