Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable visual styles without a manifest

According to the docs:

"If you want your application to use ComCtl32.dll version 6, you must add an application manifest or compiler directive to specify that version 6 should be used if it is available."

Notice the logical OR above? So what is this mysterious compiler directive?

I've got a native Win32 C++ application that is wholly contained in a single .cpp file. There are no resource files, manifest files, etc. I'd like to keep it that way, but I would also like to use visual styles.

like image 802
Tergiver Avatar asked Nov 29 '10 21:11

Tergiver


People also ask

How do I enable visual styles?

To enable your application to use visual styles, you must use ComCtl32. dll version 6 or later. Because version 6 is not redistributable, it is available only when your application is running on a version of Windows that contains it. Windows ships with both version 5 and version 6.

How do I enable visual styles in Windows 10?

Visual styles can be enabled in an application by calling the Application. EnableVisualStyles method or by using an application manifest that specifies that ComCtl32. dll version 6 or later will be used to draw controls. Visual styles are being used to draw the client area of application windows.

What is enable XP visual styles?

EnableVisualStyles() doesn't mean that the buttons are now visually drawn when that method is run, it means that it will use the built-in Windows theming to style controls instead of the "classic Windows" look and feel. If your computer is running without a style, then you wouldn't notice any difference.

What is manifest file in Visual Studio?

The build system in Visual Studio allows the manifest to be embedded in the final binary application file, or generated as an external file. This behavior is controlled by the Embed Manifest option in the Project Properties dialog. To set this property, open the Manifest Tool node, then select Input and Output.


1 Answers

There's actually a third way with no manifests whatsoever, though it's rather 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;
}
like image 78
user541686 Avatar answered Sep 28 '22 01:09

user541686