.h文件
//$---- Active Form HDR ---- (stActiveFormHdr)
#ifndef LesWebImplH
#define LesWebImplH
//---------------------------------------------------------------------------
#include <classes.hpp>
#include <controls.hpp>
#include <stdCtrls.hpp>
#include <forms.hpp>
#include "LesWebCtrl_TLB.h"
#include <comdef.h>
#include <mshtml.h>
#include <exdisp.h>
#include <shlguid.h>
#include <activscp.h>
//---------------------------------------------------------------------------
class TLesWebX : public TActiveForm
{
__published: // IDE-managed Components
...
private: // User declarations
...
protected:
...
public: // User declarations
...
};
//---------------------------------------------------------------------------
//extern PACKAGE TLesWebX *LesWebX;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
class ATL_NO_VTABLE TLesWebXImpl:
VCLCONTROL_IMPL(TLesWebXImpl, LesWebX, TLesWebX, ILesWebX, DIID_ILesWebXEvents)
,public IObjectWithSiteImpl<TLesWebXImpl>
,public IPersistPropertyBagImpl<TLesWebXImpl>
,public IObjectSafetyImpl<TLesWebXImpl,INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA>
{
void __fastcall CreateEvent(TObject *Sender);
void __fastcall DestroyEvent(TObject *Sender);
public:
IHTMLDocument2* __fastcall GetHTMLDocument2(void);
IHTMLWindow2* __fastcall GetHTMLWindow2(void);
HWND __fastcall GetIEWinHandle(void);
AnsiString __fastcall GetPageURL(void);
...
void InitializeControl()
{
m_VclCtl->WebX=this;
m_VclCtl->OnCreate = CreateEvent;
m_VclCtl->OnDestroy = DestroyEvent;
}
// The COM MAP entries declares the interfaces your object exposes (through
// QueryInterface). CComRootObjectEx::InternalQueryInterface only returns
// pointers for interfaces in the COM map. VCL controls exposed as OCXes
// have a minimum set of interfaces defined by the
// VCL_CONTROL_COM_INTERFACE_ENTRIES macro. Add other interfaces supported
// by your object with additional COM_INTERFACE_ENTRY[_xxx] macros.
//
BEGIN_COM_MAP(TLesWebXImpl)
VCL_CONTROL_COM_INTERFACE_ENTRIES(ILesWebX)
COM_INTERFACE_ENTRY_IMPL(IPersistPropertyBag)
COM_INTERFACE_ENTRY(IObjectSafety)
COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()
// The PROPERTY map stores property descriptions, property DISPIDs,
// property page CLSIDs and IDispatch IIDs. You may use use
// IPerPropertyBrowsingImpl, IPersistPropertyBagImpl, IPersistStreamInitImpl,
// and ISpecifyPropertyPageImpl to utilize the information in you property
// map.
//
// NOTE: The BCB Wizard does *NOT* maintain your PROPERTY_MAP table. You must
// add or remove entries manually.
//
BEGIN_PROPERTY_MAP(TLesWebXImpl)
// PROP_PAGE(CLSID_LesWebXPage)
END_PROPERTY_MAP()
/* DECLARE_VCL_CONTROL_PERSISTENCE(CppClass, VclClass) is needed for VCL
* controls to persist via the VCL streaming mechanism and not the ATL mechanism.
* The macro adds static IPersistStreamInit_Load and IPersistStreamInit_Save
* methods to your implementation class, overriding the methods in IPersistStreamImpl.
* This macro must be manually undefined or removed if you port to C++Builder 4.0. */
DECLARE_VCL_CONTROL_PERSISTENCE(TLesWebXImpl, TLesWebX);
// The DECLARE_ACTIVEXCONTROL_REGISTRY macro declares a static 'UpdateRegistry'
// routine which registers the basic information about your control. The
// parameters expected by the macro are the ProgId & the ToolboxBitmap ID of
// your control.
//
DECLARE_ACTIVEXCONTROL_REGISTRY("LesWebCtrl.LesWebX", 1);
protected:
STDMETHOD(get_Visible(VARIANT_BOOL* Value));
STDMETHOD(set_Visible(VARIANT_BOOL Value));
...
...
...
};
//---------------------------------------------------------------------------
#endif
cpp文件
// TYourOCXImpl 是你的 ATL 类,我这里实际是个 TActiveForm
// 获取 IHTMLDocument2 指针
IHTMLDocument2* __fastcall TLesWebXImpl::GetHTMLDocument2(void)
{
IHTMLDocument2* iDoc=NULL;
try {
if(m_spClientSite!=NULL) {
CComPtr<IOleContainer> iOct(NULL);
OleCheck(m_spClientSite->GetContainer(&iOct));
OleCheck(iOct->QueryInterface(IID_IHTMLDocument2, (void**)&iDoc));
}
}
catch(...) {
iDoc=NULL;
}
if(iDoc!=NULL) return iDoc;
try {
if(m_spUnkSite!=NULL) {
CComQIPtr<IServiceProvider,&IID_IServiceProvider> iSpv(m_spUnkSite);
if(iSpv!=NULL) {
CComPtr<IHTMLWindow2> iHmw(NULL);
OleCheck(iSpv->QueryService(SID_SHTMLWindow, IID_IHTMLWindow2,
(void**)(&iHmw)));
if(iHmw!=NULL)
OleCheck(iHmw->get_document(&iDoc));
}
}
}
catch(...) {
iDoc=NULL;
}
return iDoc;
}
// 获取 当前页面 的URL地址
AnsiString __fastcall TYourOCXImpl::GetPageURL(void)
{
AnsiString s("");
CComPtr<IHTMLDocument2> iDoc(GetHTMLDocument2());
if(iDoc==NULL) return s;
try {
CComBSTR url("");
OleCheck(iDoc->get_URL(&url));
s=AnsiString(url.Copy());
}
catch(...) { s=""; }
//MessageBox(GetActiveWindow(),s.c_str(),"ref",MB_OK);
return s;
}