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

📄 browsetestview.cpp

📁 evc浏览器编程示例
💻 CPP
字号:
// BrowseTestView.cpp : implementation of the CBrowseTestView class
//

#include "stdafx.h"
#include "Winuser.h"
#include "BrowseTest.h"
#include <mshtmdid.h> // AMBIENT_DLCONTROL
#include <exdispid.h>
#include <atlbase.h>
#include "AtlConv.h"
#include "BrowseTestDoc.h"
#include "BrowseTestView.h"

#include "htmlhelp.h"
#include "debug.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBrowseTestView

IMPLEMENT_DYNCREATE(CBrowseTestView, CView)

BEGIN_MESSAGE_MAP(CBrowseTestView, CView)
	//{{AFX_MSG_MAP(CBrowseTestView)
	ON_WM_CREATE()
	ON_COMMAND(ID_BROWSE, OnBrowse)
	ON_COMMAND(ID_BROWSER_INIT, OnBrowserInit)
	ON_COMMAND(FILE_OPEN_CHM, OnOpenChm)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBrowseTestView construction/destruction

CBrowseTestView::CBrowseTestView()
{
	// TODO: add construction code here
	m_pBrowser = NULL;
	m_pObject = NULL;

	m_initilized = FALSE;
}

CBrowseTestView::~CBrowseTestView()
{
}

BOOL CBrowseTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBrowseTestView drawing

void CBrowseTestView::OnDraw(CDC* pDC)
{
	CBrowseTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CBrowseTestView diagnostics

#ifdef _DEBUG
void CBrowseTestView::AssertValid() const
{
	CView::AssertValid();
}

void CBrowseTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CBrowseTestDoc* CBrowseTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBrowseTestDoc)));
	return (CBrowseTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBrowseTestView message handlers

int CBrowseTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	return 0;
}

void CBrowseTestView::OnBrowserInit() 
{
	HRESULT hr;
	CRect m_rtText;
	// TODO: Add your command handler code here
	//CoInitializeEx(NULL, COINIT_MULTITHREADED);

	// TODO: Add your specialized creation code here
	/*m_rtText = CRect(50, 50, 320, 185); */
	GetClientRect(&m_rtText);
#if 1
	if (! m_wndBrowser.CreateControl(CLSID_WebBrowser, NULL, WS_VISIBLE | WS_CHILD, 
	m_rtText, this, AFX_IDW_PANE_FIRST))
	{
		AfxMessageBox(_T("m_wndBrowser.CreateControl ERROR"));
		DestroyWindow();
		return;
	}
	LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown();
#else
	LPUNKNOWN lpUnk = NULL;
    hr = CoCreateInstance(CLSID_WebBrowser, NULL,
                              CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
                              IID_IUnknown, (LPVOID *)(&lpUnk));
#endif
#if 0
	hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowser);
	if (! SUCCEEDED(hr))
	{
		m_pBrowser = NULL;
		m_wndBrowser.DestroyWindow();
		DestroyWindow();
		AfxMessageBox(_T("lpUnk->QueryInterface ERROR"));
		return ;
	}
#else
	hr = lpUnk->QueryInterface(IID_IOleObject, (LPVOID *)(&m_pObject));
	if (! SUCCEEDED(hr))
	{
		m_pObject = NULL;
		AfxMessageBox(_T("lpUnk->QueryInterface:IID_IOleObject ERROR"));
		DestroyWindow();
		return;
	}
    DWORD dwFlags;
    hr = m_pObject->GetMiscStatus(DVASPECT_CONTENT, &dwFlags);
    if (FAILED(hr))
	{
		m_pObject = NULL;
		AfxMessageBox(_T("pObject->GetMiscStatus ERROR"));
		DestroyWindow();
		return;
	}
    if (dwFlags & OLEMISC_SETCLIENTSITEFIRST)
    {
        IOleClientSite *pClientSite = NULL;
		hr = QueryInterface(IID_IOleClientSite, (LPVOID *)(&pClientSite));
        hr = m_pObject->SetClientSite(pClientSite);
        pClientSite->Release();
        if (FAILED(hr))
		{
			m_pObject = NULL;
			AfxMessageBox(_T("pObject->SetClientSite ERROR"));
			DestroyWindow();
			return;
		}
    }


    hr = m_pObject->DoVerb(OLEIVERB_UIACTIVATE,NULL,this,0,m_wndBrowser.m_hWnd,&m_rtText);
    if (FAILED(hr))
	{
		m_pObject = NULL;
		m_pBrowser = NULL;
		AfxMessageBox(_T("m_pObject->DoVerb ERROR"));
		m_wndBrowser.DestroyWindow();
		DestroyWindow();
		return;
	}
    hr = m_pObject->QueryInterface(IID_IWebBrowser2, (void **)&m_pBrowser);
	if(FAILED(hr))
	{
		m_pObject = NULL;
		m_pBrowser = NULL;
		AfxMessageBox(_T("m_pObject->QueryInterface:IID_IWebBrowser2 ERROR"));
		m_wndBrowser.DestroyWindow();
		DestroyWindow();
		return;
	}

	InitEvents();
	//m_wndBrowser.SetWindowText(_T("Browse IE!"));
	m_wndBrowser.ShowWindow(TRUE);
#endif
	RegisterCHMMIME();
}
void CBrowseTestView::HideScroll(void)
{
	CString csdisp;
	IDispatch* pDocDisp = NULL;
	HRESULT hr = m_pBrowser->get_Document(&pDocDisp);	
	if(SUCCEEDED(hr) && pDocDisp!=NULL)  
	{  
		IHTMLDocument2*	pDoc = NULL;  
		hr = pDocDisp->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pDoc);
		if(SUCCEEDED(hr))  
		{
			IHTMLElement* pElement = NULL;
                     hr = pDoc->get_body(&pElement);
			if(SUCCEEDED(hr))
			{
				IHTMLBodyElement* pBodyElement = NULL;	
				hr = pElement->QueryInterface(IID_IHTMLBodyElement,reinterpret_cast<void**>(&pBodyElement));	
				if(SUCCEEDED(hr))  
				{  
					pBodyElement->put_scroll(::SysAllocString(L"no"));	
					pBodyElement->Release();
				}  
				pElement->Release();					
			}
			pDoc->Release();	
		}
		pDocDisp->Release();			
	}   
}
void CBrowseTestView::ScrollUp(void)
{

}
void CBrowseTestView::ScrollDown(void)
{

}

void CBrowseTestView::InitEvents(void)
{
	HRESULT hr;
	IConnectionPointContainer* pCPContainer;
	DWebBrowserEvents  *pEvents = NULL;
	hr = m_pBrowser->QueryInterface(IID_IConnectionPointContainer, (LPVOID *)&pCPContainer);
	if (SUCCEEDED(hr))
	{
		hr = pCPContainer->FindConnectionPoint(DIID_DWebBrowserEvents2, &m_pConnectionPoint);
		if (SUCCEEDED(hr))
		{
#if 1
			hr = QueryInterface(DIID_DWebBrowserEvents2, (LPVOID *)(&pEvents));
			if (SUCCEEDED(hr))
			{
				hr = m_pConnectionPoint->Advise(pEvents, &m_dwCookie);
				if (FAILED(hr))
				{
					AfxMessageBox(_T("Failed to Advise"));
				}
				else
				{
					//AfxMessageBox(_T("Advise ok"));
				}
			}

			else
			{
				AfxMessageBox(_T("Failed to QueryInterface:DIID_DWebBrowserEvents2"));
			}
#else
			hr = m_pConnectionPoint->Advise(this, &m_dwCookie);
			if (FAILED(hr))
			{
				//AfxMessageBox(_T("Failed to Advise"));
			}
			else
			{
				//AfxMessageBox(_T("Advise ok"));
			}
#endif
		}		
		else
		{
			AfxMessageBox(_T("Failed to QueryInterface:DIID_DWebBrowserEvents2"));
		}		
		pCPContainer->Release();
#if 0
		pEvents->Release();
#endif
	}
	else
	{
		AfxMessageBox(_T("Failed to QueryInterface:IID_IConnectionPointContainer"));
	}
}

void  CBrowseTestView::Navigate(CString  strURL)
{
	COleVariant     empty;
	BSTR bstrURL = strURL.AllocSysString();
	m_pBrowser->Navigate (bstrURL, COleVariant((long) 0, VT_I4), empty, empty, empty);
	m_wndBrowser.ShowWindow(TRUE);
}
void CBrowseTestView::OnBrowse() 
{
	// TODO: Add your command handler code here
#if 0
	CString filename;
	CFileDialog  dlg(TRUE, NULL, NULL);	
	if(dlg.DoModal() == IDCANCEL)
	{
		return;
	}
	AfxMessageBox(dlg.GetPathName());
	Navigate(dlg.GetPathName());
#else
	CString filename;
	filename = _T("chm:\\Storage Card\\CHM\\htm\\_ppc_2.htm");
	Navigate(filename);
#endif
	
}

#ifndef __STAND_BROWSER__
STDMETHODIMP CBrowseTestView::GetHostInfo(DOCHOSTUIINFO *pInfo)
{
	
	pInfo->cbSize = sizeof(DOCHOSTUIINFO);
	//pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER|DOCHOSTUIFLAG_FLAT_SCROLLBAR;
	pInfo->dwFlags |= DOCHOSTUIFLAG_SCROLL_NO;

	return S_OK;

}

STDMETHODIMP CBrowseTestView::Invoke (
                              DISPID dispidMember,
                              REFIID riid,
                              LCID lcid,
                              WORD wFlags,
                              DISPPARAMS FAR* pdparams,
                              VARIANT FAR* pvarResult,
                              EXCEPINFO FAR* pexcepinfo,
                              UINT FAR* puArgErr
                              )
{
	RETAILMSG(1,(_T("Invoke:%d\r\n"),dispidMember));
    switch (dispidMember)
    {
    case DISPID_AMBIENT_DLCONTROL:
	if(pvarResult)
	{
		V_VT(pvarResult) = VT_I4;
		// always set these three unless they should be disabled
		pvarResult->lVal = DLCTL_DLIMAGES|DLCTL_VIDEOS|DLCTL_BGSOUNDS;
		
		// put the browser in download only mode
		// pvarResult->lVal |= DLCTL_DOWNLOADONLY;
	}
	break;

    case DISPID_STATUSTEXTCHANGE:
        {
        }
		//AfxMessageBox(_T("DISPID_STATUSTEXTCHANGE"));
        break;
        
    case DISPID_SETSECURELOCKICON:
        {
        }
        break;

    case DISPID_PROGRESSCHANGE:
        {
        }
        break;

    // notification for file download
    case DISPID_FILEDOWNLOAD:
		//AfxMessageBox(_T("DISPID_FILEDOWNLOAD"));
	break;


    case DISPID_NAVIGATECOMPLETE2:
        break;

    case DISPID_COMMANDSTATECHANGE:
		//AfxMessageBox(_T("DISPID_COMMANDSTATECHANGE"));
        break;

    case DISPID_TITLECHANGE:
            if (pdparams && pdparams->rgvarg[0].vt == VT_BSTR)
            {
                TCHAR szTitle[85];
                int len = wcslen(pdparams->rgvarg[0].bstrVal);
                    
                //_tcsncpy(_szTitle, pdparams->rgvarg[0].bstrVal, MAX_URL-1);
                _tcsncpy(szTitle, pdparams->rgvarg[0].bstrVal, 80);
                if(len > 80)
                    _tcscat(szTitle, L"...");

                SetWindowText(szTitle);
            }
            break;

    case DISPID_ONQUIT:
        PostMessage(WM_CLOSE, 0, 0L);
        break;

    case DISPID_DOWNLOADBEGIN:
        break;
    case DISPID_DOWNLOADCOMPLETE:
		break;
    case DISPID_DOCUMENTCOMPLETE:
        break;

    case DISPID_NEWWINDOW2:
        //pdparams->rgvarg[1].boolVal = VARIANT_TRUE;
        //break;        
        //return HandleNewWindow2(NULL, pdparams);
        //
		break;


    case DISPID_PROPERTYCHANGE:
		break;
    case DISPID_BEFORENAVIGATE2:
        break;
    default:
		//CString csPrint;
		//csPrint.Format(_T("Invoke:%d"),dispidMember);
		//AfxMessageBox(csPrint);
        return DISP_E_MEMBERNOTFOUND;
    }
    return S_OK;
}


STDMETHODIMP CBrowseTestView::moveTo( LONG x, LONG y)
{
	RETAILMSG(1,(_T("moveTo:%d,%d\r\n"),x,y));
    SetWindowPos(NULL, x, y, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
    return S_OK;
}

STDMETHODIMP CBrowseTestView::moveBy( LONG x, LONG y) 
{
    RECT rcWindow;
	RETAILMSG(1,(_T("moveBy:%d,%d\r\n"),x,y));
    GetWindowRect(&rcWindow);

    SetWindowPos(NULL, rcWindow.left + x, rcWindow.top + y, 0, 0,
                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
    return S_OK;
}


STDMETHODIMP CBrowseTestView::resizeTo( LONG x, LONG y) 
{
                // We do not want the size to be less then 100 for top level windows in browser
    RETAILMSG(1,(_T("resizeTo:%d,%d\r\n"),x,y));
	if (x < 100)
        x = 100;

    if (y < 100)
        y = 100;

    SetWindowPos(NULL, 0, 0, x, y, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);

    return S_OK;             
}

STDMETHODIMP CBrowseTestView::resizeBy( LONG x, LONG y) 
{
    RECT rcWindow;
    long w, h;
	RETAILMSG(1,(_T("resizeBy:%d,%d\r\n"),x,y));
    GetWindowRect(&rcWindow);

    w = rcWindow.right - rcWindow.left + x;
    h = rcWindow.bottom - rcWindow.top + y;

    if (w < 100)
        w = 100;

    if (h < 100)
        h = 100;

    SetWindowPos(NULL, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
    return S_OK;
}
#endif

void CBrowseTestView::CHMProtocolCallbackStub(void *pContext, void *pParam, DWORD dwParam)
{
	CBrowseTestView *pBrowseTestWnd = (CBrowseTestView *)pContext;
	if(pBrowseTestWnd)
	{
		pBrowseTestWnd->CHMProtocolCallback(pParam, dwParam);
	}
}

void CBrowseTestView::CHMProtocolCallback(void *pParam, DWORD dwParam)
{
	LPWSTR pwsUrl = (LPWSTR)pParam;
	if(pwsUrl)
	{
		AfxMessageBox(pwsUrl);
	}
}

void CBrowseTestView::RegisterCHMMIME(void)
{
	HINSTANCE hInstUrlMon = NULL;
	HRESULT hr;	
	if(!m_pCHMProtocol)
	{
		m_pCHMProtocol = new CCHMProtocolCF(CBrowseTestView::CHMProtocolCallbackStub, (void*)this);
	}

	if(m_pCHMProtocol)
	{
		m_pCHMProtocol->QueryInterface(IID_IClassFactory, (LPVOID *)&m_pFactory);
		CoInternetGetSession(0, &m_pInternetSession, 0);
		if(m_pInternetSession && m_pFactory)
		{
			hr = m_pInternetSession->RegisterNameSpace(m_pFactory, CLSID_CHMProtocol, _T("file"), 0, NULL,0);   
			hr = m_pInternetSession->RegisterNameSpace(m_pFactory, CLSID_CHMProtocol, _T("chm"), 0, NULL,0);   
			if(hr == S_OK)
			{
				//AfxMessageBox(_T("CLSID_CHMProtocol register ok!"));
			}
		}
	}
}

void CBrowseTestView::UnRegisterCHMMIME(void)
{
	if(m_pInternetSession)
	{
		m_pInternetSession->UnregisterNameSpace(m_pFactory, _T("chm"));
		m_pInternetSession->UnregisterNameSpace(m_pFactory, _T("file"));
		m_pInternetSession->Release();
		m_pInternetSession = NULL;
	}
	if(m_pFactory)
	{
		m_pFactory->Release();
		m_pFactory = NULL;
	}
	if (m_pCHMProtocol)
	{
		m_pCHMProtocol->Release();
		delete m_pCHMProtocol;
		m_pCHMProtocol = NULL;
	}
}

void CBrowseTestView::OnOpenChm() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE,NULL, NULL, OFN_EXPLORER | OFN_LONGNAMES | OFN_PATHMUSTEXIST, _T("ALL(*.*)|*.*|"), this);
	if (dlg.DoModal() == IDOK)
	{
		CString FileName = dlg.GetPathName();
		FileName = _T("chm:") + FileName;
		Navigate(FileName);
	}
}

⌨️ 快捷键说明

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