📄 ieautomationview.cpp
字号:
DWORD dwPostDataLen /* = 0 */)
{
CString strURL(lpszURL);
BSTR bstrURL = strURL.AllocSysString();
COleSafeArray vPostData;
if (lpvPostData != NULL)
{
if (dwPostDataLen == 0)
dwPostDataLen = lstrlen((LPCTSTR) lpvPostData);
vPostData.CreateOneDim(VT_UI1, dwPostDataLen, lpvPostData);
}
m_pBrowserApp->Navigate(bstrURL, COleVariant((long) dwFlags, VT_I4), COleVariant(lpszTargetFrameName, VT_BSTR),
vPostData, COleVariant(lpszHeaders, VT_BSTR));
SysFreeString(bstrURL); // Added this line to prevent leak.
}
BOOL CIEAutomationView::LoadFromResource(LPCTSTR lpszResource)
{
HINSTANCE hInstance = AfxGetResourceHandle();
ASSERT(hInstance != NULL);
CString strResourceURL;
BOOL bRetVal = TRUE;
LPTSTR lpszModule = new TCHAR[_MAX_PATH];
if (GetModuleFileName(hInstance, lpszModule, _MAX_PATH))
{
strResourceURL.Format(_T("res://%s/%s"), lpszModule, lpszResource);
Navigate(strResourceURL, 0, 0, 0);
}
else
bRetVal = FALSE;
delete [] lpszModule;
return bRetVal;
}
BOOL CIEAutomationView::LoadFromResource(UINT nRes)
{
HINSTANCE hInstance = AfxGetResourceHandle();
ASSERT(hInstance != NULL);
CString strResourceURL;
BOOL bRetVal = TRUE;
LPTSTR lpszModule = new TCHAR[_MAX_PATH];
if (GetModuleFileName(hInstance, lpszModule, _MAX_PATH))
{
strResourceURL.Format(_T("res://%s/%d"), lpszModule, nRes);
Navigate(strResourceURL, 0, 0, 0);
}
else
bRetVal = FALSE;
delete [] lpszModule;
return bRetVal;
}
//end new code
//////////////////////////////////////////////////
//Knowledge Base
//PRB: WebBrowser Control Disappears When Script Calls window.close()
//////////////////////////////
//new code
void CIEAutomationView::OnParentNotify(UINT message, LPARAM lParam )
{
if ((LOWORD(message) == WM_DESTROY) && ((HWND)lParam == m_wndBrowser))
{
// Close the parent frame window.
GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
}
else
CHtmlView::OnParentNotify(message, lParam );
}
void CIEAutomationView::WindowClosing(VARIANT_BOOL IsChildWindow,
VARIANT_BOOL *Cancel)
{
if(!IsChildWindow){
if(Cancel)
*Cancel=VARIANT_TRUE;
if(AfxMessageBox(IDS_WINDOW_CLOSING,MB_YESNO)==IDYES)
GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
}
}
//end new code
//////////////////////////////
BOOL CIEAutomationView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
// create the view window itself
m_pCreateContext = pContext;
if (!CView::Create(lpszClassName, lpszWindowName,
dwStyle, rect, pParentWnd, nID, pContext))
{
return FALSE;
}
RECT rectClient;
GetClientRect(&rectClient);
// create the control window
// AFX_IDW_PANE_FIRST is a safe but arbitrary ID
if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, lpszWindowName,
WS_VISIBLE | WS_CHILD, rectClient, this, AFX_IDW_PANE_FIRST))
{
DestroyWindow();
return FALSE;
}
LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown();
HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowserApp);
if (!SUCCEEDED(hr))
{
m_pBrowserApp = NULL;
m_wndBrowser.DestroyWindow();
DestroyWindow();
return FALSE;
}
return TRUE;
//return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
// * CImpIDocHostUIHandler::GetHostInfo
// *
// * Purpose: Called at initialization
// *
HRESULT FAR EXPORT CIEAutomationView::GetHostInfo( DOCHOSTUIINFO* pInfo )
{
pInfo->dwFlags = m_dwHostFlags;
//pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER;
pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT;
return S_OK;
}
// * CImpIDocHostUIHandler::ShowUI
// *
// * Purpose: Called when MSHTML.DLL shows its UI
// *
HRESULT FAR EXPORT CIEAutomationView::ShowUI(
DWORD dwID,
IOleInPlaceActiveObject * pActiveObject,
IOleCommandTarget * pCommandTarget,
IOleInPlaceFrame * pFrame,
IOleInPlaceUIWindow * pDoc)
{
// We've already got our own UI in place so just return S_OK
return S_OK;
}
// * CImpIDocHostUIHandler::HideUI
// *
// * Purpose: Called when MSHTML.DLL hides its UI
// *
HRESULT FAR EXPORT CIEAutomationView::HideUI(void)
{
return S_OK;
}
// * CImpIDocHostUIHandler::UpdateUI
// *
// * Purpose: Called when MSHTML.DLL updates its UI
// *
HRESULT FAR EXPORT CIEAutomationView::UpdateUI(void)
{
// MFC is pretty good about updating it's UI in it's Idle loop so I don't do anything here
return S_OK;
}
// * CImpIDocHostUIHandler::EnableModeless
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::EnableModeless
// *
HRESULT FAR EXPORT CIEAutomationView::EnableModeless(BOOL fEnable)
{
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::OnDocWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnDocWindowActivate
// *
HRESULT FAR EXPORT CIEAutomationView::OnDocWindowActivate(BOOL fActivate)
{
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::OnFrameWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnFrameWindowActivate
// *
HRESULT FAR EXPORT CIEAutomationView::OnFrameWindowActivate(BOOL fActivate)
{
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::ResizeBorder
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::ResizeBorder
// *
HRESULT FAR EXPORT CIEAutomationView::ResizeBorder(
LPCRECT prcBorder,
IOleInPlaceUIWindow* pUIWindow,
BOOL fRameWindow)
{
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::ShowContextMenu
// *
// * Purpose: Called when MSHTML.DLL would normally display its context menu
// *
HRESULT FAR EXPORT CIEAutomationView::ShowContextMenu(
DWORD dwID,
POINT* pptPosition,
IUnknown* pCommandTarget,
IDispatch* pDispatchObjectHit)
{
//return S_OK; // We've shown our own context menu. MSHTML.DLL will no longer try to show its own.
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::TranslateAccelerator
// *
// * Purpose: Called from MSHTML.DLL's TranslateAccelerator routines
// *
HRESULT FAR EXPORT CIEAutomationView::TranslateAccelerator(LPMSG lpMsg,
/* [in] */ const GUID __RPC_FAR *pguidCmdGroup,
/* [in] */ DWORD nCmdID)
{
return S_FALSE;
}
// * CImpIDocHostUIHandler::GetOptionKeyPath
// *
// * Purpose: Called by MSHTML.DLL to find where the host wishes to store
// * its options in the registry
// *
HRESULT FAR EXPORT CIEAutomationView::GetOptionKeyPath(BSTR* pbstrKey, DWORD dw)
{
return E_NOTIMPL;
}
STDMETHODIMP CIEAutomationView::GetDropTarget(
/* [in] */ IDropTarget __RPC_FAR *pDropTarget,
/* [out] */ IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget)
{
return E_NOTIMPL;
}
STDMETHODIMP CIEAutomationView::GetExternal(
/* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppDispatch)
{
// return the IDispatch we have for extending the object Model
if(m_spExternalDisp.p){
*ppDispatch=m_spExternalDisp.p;
m_spExternalDisp.p->AddRef();
return S_OK;
}
else{
if(m_domExternal.m_pShellUIHelper){
*ppDispatch=m_domExternal.GetIDispatch(TRUE);
return S_OK;
}
}
return E_NOTIMPL;
}
STDMETHODIMP CIEAutomationView::TranslateUrl(
/* [in] */ DWORD dwTranslate,
/* [in] */ OLECHAR __RPC_FAR *pchURLIn,
/* [out] */ OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut)
{
return E_NOTIMPL;
}
STDMETHODIMP CIEAutomationView::FilterDataObject(
/* [in] */ IDataObject __RPC_FAR *pDO,
/* [out] */ IDataObject __RPC_FAR *__RPC_FAR *ppDORet)
{
return E_NOTIMPL;
}
STDMETHODIMP CIEAutomationView::GetOverrideKeyPath(
/* [out] */ LPOLESTR *pchKey,
/* [in] */ DWORD dw)
{
return E_NOTIMPL;
#pragma message(__LOC__"Uncomment this if u need override default IE Setting?\r\n")
/*
HRESULT hr;
#define CCHMAX 256
size_t cchLength;
if (pchKey){
WCHAR* szMyKey = L"Software\\IEAutoMation\\IESettings";
hr = StringCchLengthW(szMyKey, CCHMAX, &cchLength);
if(FAILED(hr))return hr;
//TODO: Hanlde errors here
*pchKey = (LPOLESTR)CoTaskMemAlloc((cchLength + 1) * sizeof(WCHAR));
if (*pchKey)
hr = StringCchCopyW(*pchKey, cchLength + 1, szMyKey );
//TODO: Hanlde errors here
hr = (*pchKey) ? S_OK : E_OUTOFMEMORY;
if(FAILED(hr)){
if(*pchKey)
CoTaskMemFree(*pchKey);
return hr;
}
}
else
hr = E_INVALIDARG;
return hr;*/
}
BOOL CIEAutomationView::CanAccessExternal()
{
// if the dispatch we have is safe,
// we allow access
if (IsExternalDispatchSafe())
return TRUE;
// the external dispatch is not safe, so we check
// whether the current zone allows for scripting
// of objects that are not safe for scripting
if (m_spHtmlDoc == NULL)
return FALSE;
CComPtr<IInternetHostSecurityManager> spSecMan;
m_spHtmlDoc->QueryInterface(IID_IInternetHostSecurityManager,
(void **) &spSecMan);
if (spSecMan == NULL)
return FALSE;
HRESULT hr = spSecMan->ProcessUrlAction(URLACTION_ACTIVEX_OVERRIDE_OBJECT_SAFETY,
NULL, 0, NULL, 0, 0, PUAF_DEFAULT);
if (hr == S_OK)
return TRUE;
return FALSE;
}
BOOL CIEAutomationView::IsExternalDispatchSafe()
{
return FALSE;
}
void CIEAutomationView::SetExternalDispatch(IDispatch *pdispExternal)
{
m_spExternalDisp = pdispExternal;
}
void CIEAutomationView::SetHostFlags(DWORD dwFlags)
{
m_dwHostFlags = dwFlags;
}
//override to handle document complete for frames
//////////////////////////////////////////////
void CIEAutomationView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
//UNUSED_ALWAYS(pDisp);
ASSERT(V_VT(URL) == VT_BSTR);
CString str(V_BSTR(URL));
OnDocumentComplete(pDisp, str);
}
void CIEAutomationView::OnDocumentComplete(LPDISPATCH pDisp, LPCTSTR lpszUrl)
{
// make sure the main frame has the new URL. This call also stops the animation
((CChildFrame*)GetParentFrame())->SetAddress(lpszUrl);
CString strURL(lpszUrl);
IUnknown* pUnkBrowser = NULL;
IUnknown* pUnkDisp = NULL;
TRACE(_T("Document %s Done\r\n"),lpszUrl);
//is it top level document?
HRESULT hr = m_pBrowserApp->QueryInterface( IID_IUnknown, (void**)&pUnkBrowser);
if ( SUCCEEDED(hr) ){
hr = pDisp->QueryInterface( IID_IUnknown, (void**)&pUnkDisp );
if ( SUCCEEDED(hr) ){
if ( pUnkBrowser == pUnkDisp ){
//top level
m_spHtmlDoc=NULL;
hr=m_pBrowserApp->get_Document(&m_spHtmlDoc);
TRACE(_T("Downloading Complete\r\n"),lpszUrl);
}
pUnkDisp->Release();
}
pUnkBrowser->Release();
}
}
void CIEAutomationView::OnBeforeNavigate2(LPCTSTR /*lpszURL*/, DWORD /*nFlags*/,
LPCTSTR /*lpszTargetFrameName*/, CByteArray& /*baPostedData*/,
LPCTSTR /*lpszHeaders*/, BOOL* /*pbCancel*/)
{
// start the animation so that is plays while the new page is being loaded
((CChildFrame*)GetParentFrame())->StartAnimation();
m_spHtmlDoc=NULL;
}
// This demonstrates how we can use the Navigate2() function to load local files
// including local HTML pages, GIFs, AIFF files, etc.
void CIEAutomationView::OnFileOpen()
{
// TODO: Add your command handler code here
CString str;
str.LoadString(IDS_FILETYPES);
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, str);
if(fileDlg.DoModal() == IDOK)
Navigate2(fileDlg.GetPathName(), 0, NULL);
}
void CIEAutomationView::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel)
{
// Get a pointer to the application object.
CWinApp* pApp = AfxGetApp();
// Get the correct document template.
POSITION pos = pApp->GetFirstDocTemplatePosition();
CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate( pos );
// Create a new frame.
CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(
GetDocument(),
(CFrameWnd*)AfxGetMainWnd() );
// Activate the frame.
pDocTemplate->InitialUpdateFrame( pFrame, NULL );
CIEAutomationView* pView = (CIEAutomationView*)pFrame->GetActiveView();
// Pass pointer of WebBrowser object.
pView->SetRegisterAsBrowser( TRUE );
*ppDisp = pView->GetApplication();
}
void CIEAutomationView::OnTitleChange(LPCTSTR lpszText)
{
// this will change the main frame's title bar
if (m_pDocument != NULL)
m_pDocument->SetTitle(lpszText);
}
//////////////////////////////
// these are all simple one-liners to do simple controlling of the browser
void CIEAutomationView::OnGoBack()
{
GoBack();
}
void CIEAutomationView::OnGoForward()
{
GoForward();
}
void CIEAutomationView::OnGoSearchTheWeb()
{
GoSearch();
}
void CIEAutomationView::OnGoStartPage()
{
GoHome();
}
void CIEAutomationView::OnViewStop()
{
Stop();
}
void CIEAutomationView::OnViewRefresh()
{
Refresh();
}
// these functions control the font size. There is no explicit command in the
// CHtmlView class to do this, but we can do it by using the ExecWB() function.
void CIEAutomationView::OnViewFontsLargest()
{
COleVariant vaZoomFactor(4l);
ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vaZoomFactor, NULL);
}
void CIEAutomationView::OnViewFontsLarge()
{
COleVariant vaZoomFactor(3l);
ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vaZoomFactor, NULL);
}
void CIEAutomationView::OnViewFontsMedium()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -