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

时间:2023-02-11 14:23:56

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()
{
} 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 = ;
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 > ); if (n > )
{
// Set up the activation context structure
ACTCTX actctx;
memset(&actctx, , 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 != )
{
bRet = DeactivateActCtx(, 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

// 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()
{
} virtual HWND CreateControlWindow( HWND hWndParent, RECT& rcPos)
{
CXPStyleAC::Instance()->ActivateAc();
HWND h = __super::CreateControlWindow(hWndParent, rcPos);
CXPStyleAC::Instance()->DeactiveAc();
return h;
}
};

五、编译运行,大功告成

六、参考资料

  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