vs 2005/2008/2010 ATL ActiveX控件显示XP风格

vs 2005/2008/2010 ATL ActiveX控件在IE浏览器中控件显示的是原始的风格,要显示XP风格只要进行以下操作

一、在Stdafx.h中增加下以内容

复制代码
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
复制代码

二、增加CXPStyleAC类

复制代码
//XPStyleAC.h

#pragma once

class CXPStyleAC
{
protected:
    CXPStyleAC(void);

public:
    ~CXPStyleAC(void);

public:
     static CXPStyleAC* Instance();

     BOOL CreateAc();
     void DestoryAc();

     BOOL ActivateAc();
     BOOL DeactiveAc();

private:
    HANDLE m_hActAc;
    ULONG_PTR m_ulAcCookie;
};
复制代码
复制代码
//XPStyleAC.cpp

#include "StdAfx.h"
#include "XPStyleAC.h"


CXPStyleAC::CXPStyleAC(void) : m_hActAc(INVALID_HANDLE_VALUE), m_ulAcCookie(0)
{
}


CXPStyleAC::~CXPStyleAC(void)
{
}

CXPStyleAC* CXPStyleAC::Instance()
{
    static CXPStyleAC oXpStyleAc;

    return &oXpStyleAc;
}

BOOL CXPStyleAC::CreateAc()
{
    ATLASSERT(INVALID_HANDLE_VALUE == m_hActAc);

    BOOL bRet = FALSE;
    static int s_nModule = 0;
    MEMORY_BASIC_INFORMATION mbi;
    ::VirtualQuery(&s_nModule, &mbi, sizeof(mbi));

    TCHAR szModule[MAX_PATH];
    int n = ::GetModuleFileName((HMODULE)mbi.AllocationBase, szModule, MAX_PATH);
    ATLENSURE(n > 0);

    if (n > 0)
    {
        // Set up the activation context structure
        ACTCTX actctx;
        memset(&actctx, 0, sizeof(actctx));
        actctx.cbSize = sizeof(actctx);
        actctx.hModule = (HMODULE)mbi.AllocationBase;
        actctx.lpSource = szModule;
        actctx.lpResourceName = ISOLATIONAWARE_MANIFEST_RESOURCE_ID;
        actctx.dwFlags = ACTCTX_FLAG_HMODULE_VALID | ACTCTX_FLAG_RESOURCE_NAME_VALID;

        // Create the activation context
        m_hActAc = CreateActCtx(&actctx);
        ATLASSERT(INVALID_HANDLE_VALUE != m_hActAc);

        if (m_hActAc != INVALID_HANDLE_VALUE)
        {
            bRet = TRUE;
        }
    }
    
    return bRet;
}

void CXPStyleAC::DestoryAc()
{
    if (m_hActAc != INVALID_HANDLE_VALUE)
    {
        ReleaseActCtx(m_hActAc);
        m_hActAc = INVALID_HANDLE_VALUE;
    }
}

BOOL CXPStyleAC::ActivateAc()
{
    BOOL bRet = FALSE;

    if (m_hActAc != INVALID_HANDLE_VALUE)
    {
        bRet = ActivateActCtx(m_hActAc, &m_ulAcCookie);
        ATLASSERT(bRet);
    }

    return bRet;
}

BOOL CXPStyleAC::DeactiveAc()
{
    BOOL bRet = FALSE;

    if (m_hActAc != INVALID_HANDLE_VALUE && m_ulAcCookie != 0)
    {
        bRet = DeactivateActCtx(0, m_ulAcCookie);
        ATLASSERT(bRet);
    }

    return bRet;
}
复制代码

三、在dllmain中创建Active context

复制代码
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        CXPStyleAC::Instance()->CreateAc();
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
        CXPStyleAC::Instance()->DestoryAc();
    }

    return _AtlModule.DllMain(dwReason, lpReserved); 
}
复制代码

四、重写CComCompositeControl的CreateControlWindow方法,在创建控件前激活context

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// CTestXpStyle
class  ATL_NO_VTABLE CTestXpStyle :
     public  CComObjectRootEx<CComSingleThreadModel>,
     public  CStockPropImpl<CTestXpStyle, ITestXpStyle>,
     public  IOleControlImpl<CTestXpStyle>,
     public  IOleObjectImpl<CTestXpStyle>,
     public  IOleInPlaceActiveObjectImpl<CTestXpStyle>,
     public  IViewObjectExImpl<CTestXpStyle>,
     public  IOleInPlaceObjectWindowlessImpl<CTestXpStyle>,
     public  ISupportErrorInfo,
     public  IObjectWithSiteImpl<CTestXpStyle>,
     public  IQuickActivateImpl<CTestXpStyle>,
#ifndef _WIN32_WCE
     public  IDataObjectImpl<CTestXpStyle>,
#endif
     public  IProvideClassInfo2Impl<&CLSID_TestXpStyle, NULL, &LIBID_xpstyleLib>,
     public  IObjectSafetyImpl<CTestXpStyle, INTERFACESAFE_FOR_UNTRUSTED_CALLER>,
     public  CComCoClass<CTestXpStyle, &CLSID_TestXpStyle>,
     public  CComCompositeControl<CTestXpStyle>
{
public :
 
 
     CTestXpStyle()
     {
         m_bWindowOnly = TRUE;
         CalcExtent(m_sizeExtent);
     }
 
         //................................
 
 
     HRESULT FinalConstruct()
     {
         return  S_OK;
     }
 
     void  FinalRelease()
     {
     }
 
     <span style= "color: #ff0000;" > virtual  HWND CreateControlWindow( HWND hWndParent, RECT& rcPos)
     {
         CXPStyleAC::Instance()->ActivateAc();
         HWND h = __super::CreateControlWindow(hWndParent, rcPos);
         CXPStyleAC::Instance()->DeactiveAc();
         return  h;
     }</span>
};   

五、编译运行,大功告成

六、参考资料

  http://support.microsoft.com/kb/830033/zh-cn

  http://blogs.msdn.com/b/junfeng/archive/2006/03/19/sxs-activation-context-activate-and-deactivate.aspx?Redirected=true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值