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

Ter*_*ver 11 c++ winapi comctl32 visual-styles

根据文件:

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

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

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

Meh*_*dad 27

实际上还有第三种方式,没有任何明显的表现,尽管它相当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;
}
Run Code Online (Sandbox Code Playgroud)

  • 因此,这将打破微软在未来版本的 Windows 中将资源 ID 124 更改为其他值的一天。 (3认同)
  • 幻数(124)来自哪里? (2认同)
  • @MichaelWalz:是的,这就是为什么它是hacky。 (2认同)
  • 看起来 CreateActCtx 总是会在某个时候使用清单。在本例中,它从 Shell32.dll 获取资源 #124,该资源恰好是清单资源。在 Windows XP 和 Windows 10 之间,资源 #124 始终是清单文件。 (2认同)
  • 此外,使用 CreateActCtx 并不是“Hacky”,它是所有 Windows 窗体程序用来启用视觉样式的方法。它使用 System.Windows.Forms.dll 中的资源 #101(清单)。 (2认同)

Sim*_*ier 13

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

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


Joh*_*ing 6

如果你一直在阅读,你会找到答案:

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

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