在 ocx 内部如何获取所在页面的 URL

.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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值