Win32. Enable visual styles in dll

后端 未结 1 935
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 08:27

I\'ve got no experience in C++ and Win API so sorry if this question is nooby. I\'ve got DLL where I create some components, MessageBox for example. I added pra

相关标签:
1条回答
  • 2020-12-16 08:34

    If you want your DLL to use visual style aware controls, i.e. comctl32 v6, even if your host application does not use it, you have to use Activation Context API. Here's an example on how to use it:

     HANDLE hActCtx;
     ACTCTX actCtx;
     ZeroMemory(&actCtx, sizeof(actCtx));
     actCtx.cbSize = sizeof(actCtx);
     actCtx.hModule = hInst;
     actCtx.lpResourceName = MAKEINTRESOURCE(2);
     actCtx.dwFlags = ACTCTX_FLAG_HMODULE_VALID | ACTCTX_FLAG_RESOURCE_NAME_VALID;
    
     hActCtx = CreateActCtx(&actCtx);
     if (hActCtx != INVALID_HANDLE_VALUE) {
         ULONG_PTR cookie;
         ActivateActCtx(hActCtx, &cookie);
    
         // Do some UI stuff here; just show a message for example
         MessageBox(NULL, TEXT("Styled message box"), NULL, MB_OK);
    
         DeactivateActCtx(0, cookie);
         ReleaseActCtx(hActCtx);
     }
    

    Here hInst is the module handle of your DLL, you can save it in a global variable in DllMain or use GetModuleHandle function to get it. This sample implies your DLL stores Common Controls version 6.0 manifest in its resources with ID 2.

    You can call CreateActCtx only once when your DLL initializes, and ReleaseActCtx when it's not needed any more. Call ActivateActCtx before creating any windows and call DeactivateActCtx before returning control to application.

    0 讨论(0)
提交回复
热议问题