Win32的。在dll中启用视觉样式 [英] Win32. Enable visual styles in dll

查看:403
本文介绍了Win32的。在dll中启用视觉样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有C ++和Win API的经验,所以如果这个问题是nooby,抱歉。我有DLL,我创建了一些组件,例如 MessageBox 。我添加了pragma注释来启用视觉样式,并且它不起作用(并且我不知道从这个答案: windows 7风格的Internet Explorer上的组合框工具栏,怎么样?

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 pragma comment to enable visual styles and it does not work (and it shouldn't as I know from this answer: windows 7 style for combobox on internet explorer toolbar, how?

Dll代码(省略导出等等):

Dll code(omit export and so on):

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

MYDLL_API int fnmydll(void)
{
   MessageBox(NULL, L"Message", NULL, 0);
   return 42;
}

然后我从我的应用程序调用这个dll函数:

Then I invoke this dll function from my app:

#include <iostream>
#include <Windows.h>
#include "my-dll.h"

int WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    fnmydll();
    return 0;
}

我有我的消息框,但没有视觉样式。据我所知,当我的dll被调用时,我应该激活上下文,但是MSDN没有例子。你能给我这样的例子,或者至少解释更多细节发生了什么?因为我甚至不明白为什么函数 BOOL GetCurrentActCtx(_Out_HANDLE * lphActCtx); 接收指向 ACTCTX 的指针,但是签名一些 HANDLE 类型。

And I've got my message box but without visual styles. As far as I understand I should activate context when my dll is invoked but MSDN has no examples how to do it. Could you please give me such example or at least explain what is going on in more details? Because I can't even understand why function BOOL GetCurrentActCtx(_Out_ HANDLE *lphActCtx); receives pointer to ACTCTX but has signature with some HANDLE type.

推荐答案

如果你想要你的DLL使用视觉风格感知控件,即comctl32 v6,即使您的主机应用程序不使用它,您必须使用激活上下文API。这是一个如何使用它的例子:

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);
 }

这里 hInst 您的DLL的模块句柄,您可以将其保存在 DllMain 中的全局变量中,或使用 GetModuleHandle 功能来获取它。此示例表示您的DLL在其ID为2的资源中存储Common Controls 6.0版清单。

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.

您可以调用 CreateActCtx 只有当你的DLL初始化时,和 ReleaseActCtx ,当它不再需要了。致电 ActivateActCtx ,然后再创建任何窗口并调用 DeactivateActCtx ,然后将控制权返回应用程序。

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.

这篇关于Win32的。在dll中启用视觉样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆