⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ieautomationview.cpp

📁 webbrowser
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// IEAutomationView.cpp : implementation file
//

#include "stdafx.h"

#include <initguid.h> // Realizes CLSID definitions
#include "msscript.h" // defines CLSIDs for VBScript and JScript
//#include "StrSafe.h"
#include <Mshtmdid.h>
#include <exdispid.h>
#include <Exdisp.h>
#include <idispids.h>
#include "IEAutomation.h"
#include "IEAutomationDoc.h"
#include "IEAutomationView.h"
#include "ChildFrm.h"
#include "CmdTargetPlus.h"
#include "DlgInputText.h"
#include "DlgDocOutline.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CIEAutomationView
//[ uuid(6627B8C6-C0B2-4438-B989-61EF9A97FCD8), version(1.0) ]
//library IEAutomation
const GUID CDECL BASED_CODE _tlid =
		{ 0x6627B8C6, 0xC0B2, 0x4438, { 0xb9, 0x89, 0x61, 0xef, 0x9a, 0x97, 0xfc, 0xd8 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;
const GUID	CGID_WebBrowser={
	0xED016940, 0xBD5B, 0x11cf, 0xBA,0x4E,0x00,0xC0,0x4F,0xD7,0x08,0x16};

IMPLEMENT_DYNCREATE(CIEAutomationView, CHtmlView)

CIEAutomationView::CIEAutomationView()
{
	EnableAutomation();
	//{{AFX_DATA_INIT(CIEAutomationView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
///////////////////////////////////////////////////
//Active Script Support
	m_axsp = NULL;
	m_axs  = NULL;
	m_bScriptIsRunning = FALSE;
///////////////////////////////////////////////////
// Hook up windows objects to their dispatch implementations
// so they can fire COM events when Windows events occur
	#pragma message(__LOC__"Need to do Windows Objects hook up here\r\n") 
// vice versa, so dispatch implementations can affect windows
///////////////////////////////////////////////////
//Offline Support
	m_bOffline=FALSE;
///////////////////////////////////////////////////
//Host Flags
	m_dwHostFlags=DOCHOSTUIFLAG_NO3DBORDER;
	m_domExternal.m_pView=this;
	ExternalAddRef();
}

CIEAutomationView::~CIEAutomationView()
{
///////////////////////////////////////////////////
//Active Script Support
	if (m_axsp)
		m_axsp->Release();
	if (m_axs){
		m_axs->Close();
		m_axs->Release();
	}
///////////////////////////////////////////////////
}

void CIEAutomationView::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CHtmlView::OnFinalRelease();
}

void CIEAutomationView::DoDataExchange(CDataExchange* pDX)
{
	CHtmlView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIEAutomationView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CIEAutomationView, CHtmlView)
	//{{AFX_MSG_MAP(CIEAutomationView)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_GO_BACK, OnGoBack)
	ON_COMMAND(ID_GO_FORWARD, OnGoForward)
	ON_COMMAND(ID_GO_SEARCH_THE_WEB, OnGoSearchTheWeb)
	ON_COMMAND(ID_GO_START_PAGE, OnGoStartPage)
	ON_COMMAND(ID_VIEW_STOP, OnViewStop)
	ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
	ON_COMMAND(ID_VIEW_FONTS_LARGEST, OnViewFontsLargest)
	ON_COMMAND(ID_VIEW_FONTS_LARGE, OnViewFontsLarge)
	ON_COMMAND(ID_VIEW_FONTS_MEDIUM, OnViewFontsMedium)
	ON_COMMAND(ID_VIEW_FONTS_SMALL, OnViewFontsSmall)
	ON_COMMAND(ID_VIEW_FONTS_SMALLEST, OnViewFontsSmallest)
	ON_COMMAND(ID_VIEW_SOURCE, OnViewSource)
	ON_WM_DESTROY()
	ON_COMMAND(ID_EDIT_FIND, OnEditFind)
	ON_COMMAND(ID_VIEW_OPTION, OnViewOption)
	ON_COMMAND(ID_FILE_OFFLINE, OnFileOffline)
	ON_UPDATE_COMMAND_UI(ID_FILE_OFFLINE, OnUpdateFileOffline)
	ON_COMMAND(ID_FAVORITES_MANAGE, OnFavoritesManage)
	ON_COMMAND(ID_FAVORITES_ADD, OnFavoritesAdd)
	ON_COMMAND(ID_SCRIPT_EDIT, OnScriptEdit)
	ON_UPDATE_COMMAND_UI(ID_SCRIPT_EDIT, OnUpdateScriptEdit)
	ON_COMMAND(ID_SCRIPT_RUN, OnScriptRun)
	ON_UPDATE_COMMAND_UI(ID_SCRIPT_RUN, OnUpdateScriptRun)
	//}}AFX_MSG_MAP
//Knowledge Base  
//PRB: WebBrowser Control Disappears When Script Calls window.close()
//new code
	ON_WM_PARENTNOTIFY() 
//end new code
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CIEAutomationView, CHtmlView)
	//{{AFX_DISPATCH_MAP(CIEAutomationView)
	DISP_PROPERTY_EX(CIEAutomationView, "WebBrowser", GetWebBrowser, SetNotSupported, VT_DISPATCH)
	DISP_FUNCTION(CIEAutomationView, "HostCreateObject", HostCreateObject, VT_DISPATCH, VTS_VARIANT)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

BEGIN_EVENTSINK_MAP(CIEAutomationView, CHtmlView)
	ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, DISPID_DOCUMENTCOMPLETE, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)
	ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, DISPID_WINDOWCLOSING, WindowClosing,VTS_BOOL VTS_PBOOL)
	ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 250 /* BeforeNavigate2 */, BeforeNavigate2, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
END_EVENTSINK_MAP()

// Note: we add support for IID_IIEAutomationView to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {31BB0017-183E-4BB6-AF90-907EFE4CEA01}
static const IID IID_IIEAutomationView =
{ 0x31bb0017, 0x183e, 0x4bb6, { 0xaf, 0x90, 0x90, 0x7e, 0xfe, 0x4c, 0xea, 0x1 } };

// {31BB0017-183E-4BB6-AF90-907EFE4CEA01}
static const IID CLSID_IIEAutomationView =
{ 0x31bb0017, 0x183e, 0x4bb6, { 0xaf, 0x90, 0x90, 0x7e, 0xfe, 0x4c, 0xea, 0x1 } };

IMPLEMENT_OLETYPELIB(CIEAutomationView, _tlid, _wVerMajor, _wVerMinor)
BOOL CIEAutomationView::GetDispatchIID(IID* piid)
{
	*piid = IID_IIEAutomationView ;

	return TRUE;
}

BEGIN_INTERFACE_MAP(CIEAutomationView, CHtmlView)
	INTERFACE_PART(CIEAutomationView, IID_IActiveScriptSite, ActiveScriptSite)
	INTERFACE_PART(CIEAutomationView, IID_IActiveScriptSiteWindow, ActiveScriptSiteWindow)
	INTERFACE_PART(CIEAutomationView, IID_IIEAutomationView, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIEAutomationView diagnostics

#ifdef _DEBUG
void CIEAutomationView::AssertValid() const
{
	CHtmlView::AssertValid();
}

void CIEAutomationView::Dump(CDumpContext& dc) const
{
	CHtmlView::Dump(dc);
}
CIEAutomationDoc* CIEAutomationView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CIEAutomationDoc)));
	return (CIEAutomationDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// IActiveScriptSite Implementation

STDMETHODIMP_(ULONG)CIEAutomationView::XActiveScriptSite::AddRef()
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)
    return pThis->ExternalAddRef();
}

STDMETHODIMP_(ULONG)CIEAutomationView::XActiveScriptSite::Release()
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)
    return pThis->ExternalRelease();
}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::QueryInterface(
    REFIID iid, void FAR* FAR* ppvObj)
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)
    return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::GetLCID(LCID* plcid)
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)

	return E_NOTIMPL;
}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::GetItemInfo(
            /* [in] */ LPCOLESTR pstrName,
            /* [in] */ DWORD dwReturnMask,
            /* [out] */LPUNKNOWN* ppiunkItem,
            /* [out] */LPTYPEINFO* ppti)
{
	HRESULT hr = S_OK; 

    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)
	USES_CONVERSION;

	TRACE("GetItemInfo: Name = %s Mask = %x\n", OLE2CT(pstrName), dwReturnMask);

	if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
	{
		if (!ppti)
			return E_INVALIDARG;
		*ppti = NULL;
	}

	if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
	{
		if (!ppiunkItem)
			return E_INVALIDARG;
		*ppiunkItem = NULL;
	}

	// Global object
	if (!_wcsicmp(L"Scripter", pstrName))
	{
		if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
		{
			hr=pThis->GetTypeInfoOfGuid(0 /* lcid unknown! */, CLSID_IIEAutomationView, ppti);
			if(SUCCEEDED(hr))
				(*ppti)->AddRef();      // because returning
		}
		
		if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
		{
			*ppiunkItem = pThis->GetIDispatch(TRUE);
			(*ppiunkItem)->AddRef();    // because returning
		}
		return S_OK;
	}

	// Check map for named item
	CCmdTarget* pdispimpl = NULL;
	if (!(pThis->mapNamedItems.Lookup(OLE2CT(pstrName), (CObject*&) pdispimpl)))
	{
		return TYPE_E_ELEMENTNOTFOUND;
	}

	LPUNKNOWN pUnk = NULL;
	ASSERT(NULL != pdispimpl);
	if (pdispimpl->IsKindOf(RUNTIME_CLASS(CCmdTargetPlus)))
		// Automation class
		pUnk = pdispimpl->GetIDispatch(TRUE);
	else
		// Wrapper Class probably
		pUnk = ((CWnd*)pdispimpl)->GetControlUnknown();

	if (NULL == pUnk)
	{
		TRACE("No IUnknown for item!\n");
		// This shouldn't ever happen, since we're providing the items
		return E_FAIL;
	}

	if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
	{
		// Use IProvideClassInfo to get ITypeInfo of coclass!
		IProvideClassInfo *pci = NULL;
		hr = pUnk->QueryInterface(IID_IProvideClassInfo, (void**)&pci);
		if (SUCCEEDED(hr) && pci)
			hr = pci->GetClassInfo(ppti);
		if (FAILED(hr))
			return E_FAIL;
	}
	
	if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
	{
		*ppiunkItem = pUnk;
		(*ppiunkItem)->AddRef();    // because returning
	}

	return S_OK;
}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::GetDocVersionString(LPBSTR pbstrVersion)
{
	METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)

	return E_NOTIMPL;

}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::OnScriptTerminate( 
            /* [in] */ const VARIANT* pvarResult,
            /* [in] */ const EXCEPINFO* pexcepinfo)
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)

	return S_OK;

}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::OnStateChange( 
            /* [in] */ SCRIPTSTATE ssScriptState)
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)

	return S_OK;
}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::OnScriptError( 
            /* [in] */ IActiveScriptError* pse)
{
	METHOD_PROLOGUE_EX(CIEAutomationView, ActiveScriptSite)

	EXCEPINFO ei;
	DWORD     dwSrcContext;
	ULONG     ulLine;
	LONG      ichError;
	BSTR      bstrLine = NULL;
	CString strError;

	pse->GetExceptionInfo(&ei);
	pse->GetSourcePosition(&dwSrcContext, &ulLine, &ichError);
	pse->GetSourceLineText(&bstrLine);
	
	CString desc;
	CString src;

	desc = (LPCWSTR)ei.bstrDescription;
	src = (LPCWSTR)ei.bstrSource;

	strError.Format("%s\nSrc: %s\nLine:%d Error:%d Scode:%x", desc, src, ulLine, (int)ei.wCode, ei.scode);
	AfxMessageBox(strError);

	TRACE(strError);
	TRACE("\n");

	return S_OK;
}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::OnEnterScript()
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)
	TRACE("OnEnterScript\n");

	return S_OK;
}

STDMETHODIMP CIEAutomationView::XActiveScriptSite::OnLeaveScript()
{
    METHOD_PROLOGUE(CIEAutomationView, ActiveScriptSite)
	TRACE("OnLeaveScript\n");

	return S_OK;
}

/////////////////////////////////////////////////////////////////////////////
// IActiveScriptSiteWindow Implementation

STDMETHODIMP_(ULONG) CIEAutomationView::XActiveScriptSiteWindow::AddRef()
{
    METHOD_PROLOGUE_EX_(CIEAutomationView, ActiveScriptSiteWindow)
    return pThis->ExternalAddRef();
}

STDMETHODIMP_(ULONG) CIEAutomationView::XActiveScriptSiteWindow::Release()
{
    METHOD_PROLOGUE_EX_(CIEAutomationView, ActiveScriptSiteWindow)
    return pThis->ExternalRelease();
}

STDMETHODIMP CIEAutomationView::XActiveScriptSiteWindow::QueryInterface(REFIID iid, LPVOID* ppvObj)
{
    METHOD_PROLOGUE_EX_(CIEAutomationView, ActiveScriptSiteWindow)
    return pThis->ExternalQueryInterface(&iid, ppvObj);
}

STDMETHODIMP CIEAutomationView::XActiveScriptSiteWindow::EnableModeless(BOOL fEnable)
{
    METHOD_PROLOGUE_EX_(CIEAutomationView, ActiveScriptSiteWindow)
	
	CWinApp* pApp = AfxGetApp();
	if (!pApp)
		return E_FAIL;

	pApp->EnableModeless(fEnable);
	return S_OK;
}

STDMETHODIMP CIEAutomationView::XActiveScriptSiteWindow::GetWindow(HWND* phWnd)
{
    METHOD_PROLOGUE_EX_(CIEAutomationView, ActiveScriptSiteWindow)

	if (!(pThis))
		return E_FAIL;

	*phWnd = pThis->GetSafeHwnd();
	if (*phWnd)
		return S_OK;
	else
		return E_FAIL;
}
/////////////////////////////////////////////////////////////////////////////
// CIEAutomationView message handlers

BOOL CIEAutomationView::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	//Knowledge Base  
	//FIX: CHtmlView Does Not Repaint Correctly in an AppWizard-Generated MDI Application	cs.style |= WS_CLIPCHILDREN;
	cs.style |= WS_CLIPCHILDREN;
	return CHtmlView::PreCreateWindow(cs);
}

void CIEAutomationView::OnInitialUpdate() 
{
	//TODO: This code navigates to a popular spot on the web.
	//Change the code to go where you'd like.
	GoHome();
}
//////////////////////////////////////////////////
//Knowledge Base  
//BUG: CHtmlView Leaks Memory by Not Releasing BSTRs in Several Methods
// Implementation
//new code
CString CIEAutomationView::GetFullName() const
{
    ASSERT(m_pBrowserApp != NULL);

    BSTR bstr;
    m_pBrowserApp->get_FullName(&bstr);
    CString retVal(bstr);

    SysFreeString(bstr); // Added this line to prevent leak.
    return retVal;
}

CString CIEAutomationView::GetType() const
{
    ASSERT(m_pBrowserApp != NULL);

    BSTR bstr;
    m_pBrowserApp->get_Type(&bstr);

    CString retVal(bstr);

    SysFreeString(bstr); // Added this line to prevent leak.
    return retVal;
}

CString CIEAutomationView::GetLocationName() const
{
    ASSERT(m_pBrowserApp != NULL);

    BSTR bstr;
    m_pBrowserApp->get_LocationName(&bstr);
    CString retVal(bstr);

    SysFreeString(bstr); // Added this line to prevent leak.
    return retVal;
}
 
CString CIEAutomationView::GetLocationURL() const
{
    ASSERT(m_pBrowserApp != NULL);

    BSTR bstr;
    m_pBrowserApp->get_LocationURL(&bstr);
    CString retVal(bstr);

    SysFreeString(bstr); // Added this line to prevent leak.
    return retVal;
}

void CIEAutomationView::Navigate(LPCTSTR lpszURL, DWORD dwFlags /* = 0 */,
	LPCTSTR lpszTargetFrameName /* = NULL */ ,
	LPCTSTR lpszHeaders /* = NULL */, LPVOID lpvPostData /* = NULL */,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -