herzl shemuelian
herzl shemuelian

Reputation: 3498

How to enable 'Microsoft.Windows.Common-Controls' for specificate control?

I have a old MFC application that I can't to enable 'Microsoft.Windows.Common-Controls' to all controls in this application because new behavior of some controls. But I need it for CEdit that support to EM_SETCUEBANNER.

I try to do that in OnInitDialog:

m_edt = (CEdit *)GetDlgItem(edit_id);
int i= SetWindowTheme(m_edt->m_hWnd, L"Explorer", NULL);

SetWindowTheme returns 0 but I still cannot use the EM_SETCUEBANNER message.

How can I enable Microsoft.Windows.Common-Controls only for CEdit?

Upvotes: 2

Views: 1231

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596186

You need to create an Activatation Context that uses a ComCtrl32 v6 manifest. Then you can activate the context before creating the CEdit, and deactivate the context afterwards.

See How can you use both versions 5 and 6 of the common controls within the same module? on Raymond Chen's blog on MSDN.

For example, I did a quick test:

// setup main UI as needed, then...

// I borrowed code from https://stackoverflow.com/a/10444161/65863
// for testing purposes, but you can set lpSource to your own manifest
// file, if needed...
ACTCTX ctx = {};
ctx.cbSize = sizeof(actCtx);
ctx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID
            | ACTCTX_FLAG_SET_PROCESS_DEFAULT
            | ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
ctx.lpSource = TEXT("shell32.dll");
ctx.lpAssemblyDirectory = TEXT("C:\\Windows\\System32\\"); // <-- don't hard-code this in production code!
ctx.lpResourceName = MAKEINTRESOURCE(124);

HANDLE hActCtx = CreateActCtx(&ctx);
if (hActCtx == INVALID_HANDLE_VALUE) {
    // handle error ...
    return;
}

ULONG_PTR ulCookie = 0;
if (!ActivateActCtx(hActCtx, &ulCookie)) {
    // handle error ...
    ReleaseActCtx(hActCtx);
    return;
}

// make single Edit control as needed ...

DeactivateActCtx(0, ulCookie);
ReleaseActCtx(hActCtx);

And this was the result:

image

The app was compiled without any manifest at all, so ComCtrl32 v5 would be the default. The top Edit control was created using the process's default Activation Context, and the bottom Edit control was created with an explicit Activation Context using a ComCtrl32 v6 manifest, and then EM_SETCUEBANNER applied to it (if you don't want to create your own manifest, you can use resource #124 from shell32.dll, per this answer to How to enable visual styles without a manifest).

Upvotes: 4

Related Questions