PlazaBrowser.h
<pre name="code" class="cpp">#ifndef PLAZA_BROWSER_HEAD_FILE
#define PLAZA_BROWSER_HEAD_FILE
#pragma once
#include <afxhtml.h>
//
//浏览控件
class CPlazaBrowser : public CHtmlView
{
//函数定义
public:
//构造函数
CPlazaBrowser();
//析构函数
virtual ~CPlazaBrowser();
//重载函数
protected:
//绑定函数
virtual VOID PreSubclassWindow();
//消耗处理
virtual VOID PostNcDestroy();
//功能函数
public:
//创建函数
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT & rect, CWnd * pParentWnd, UINT nID, CCreateContext * pContext=NULL);
//消息函数
public:
//关闭事件
VOID OnWindowClosing(BOOL IsChildWindow, BOOL * bCancel);
DECLARE_MESSAGE_MAP()
DECLARE_EVENTSINK_MAP()
};
#endif
PlazaBrowser.cpp
<pre name="code" class="cpp">#include "stdafx.h"
#include "PlazaBrowser.h"
#include <exdispid.h>
//
BEGIN_MESSAGE_MAP(CPlazaBrowser, CHtmlView)
END_MESSAGE_MAP()
BEGIN_EVENTSINK_MAP(CPlazaBrowser, CHtmlView)
ON_EVENT(CPlazaBrowser, AFX_IDW_PANE_FIRST, DISPID_WINDOWCLOSING, OnWindowClosing,VTS_BOOL VTS_PBOOL)
END_EVENTSINK_MAP()
//构造函数
CPlazaBrowser::CPlazaBrowser()
{
}
//析构函数
CPlazaBrowser::~CPlazaBrowser()
{
}
//绑定函数
VOID CPlazaBrowser::PreSubclassWindow()
{
__super::PreSubclassWindow();
//设置背景
SetClassLong(m_hWnd,GCL_HBRBACKGROUND,NULL);
return;
}
//消耗处理
VOID CPlazaBrowser::PostNcDestroy()
{
return;
}
//创建函数
BOOL CPlazaBrowser::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT & rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
//设置变量
m_pCreateContext=pContext;
//创建窗口
if (CView::Create(lpszClassName,lpszWindowName,dwStyle,rect,pParentWnd,nID,pContext)==FALSE)
{
return FALSE;
}
//获取大小
CRect rcClient;
GetClientRect(&rcClient);
//创建窗口
if (m_wndBrowser.CreateControl(CLSID_WebBrowser,lpszWindowName,WS_VISIBLE|WS_CHILD,rcClient,this,AFX_IDW_PANE_FIRST)==FALSE)
{
DestroyWindow();
return FALSE;
}
//查询接口
LPUNKNOWN lpUnknown=m_wndBrowser.GetControlUnknown();
lpUnknown->QueryInterface(IID_IWebBrowser2,(VOID **)&m_pBrowserApp);
//屏蔽js错误提示
m_pBrowserApp->put_Silent(VARIANT_TRUE);
return TRUE;
}
//关闭事件
void CPlazaBrowser::OnWindowClosing(BOOL IsChildWindow, BOOL *bCancel)
{
//禁止关闭
if ((IsChildWindow==FALSE)&&(bCancel!=NULL))
{
*bCancel=TRUE;
}
return;
}
<pre name="code" class="cpp"> //创建控件
CPlazaBrowser m_PlazaBrowser; //浏览控件
CRect rcCreate(0,0,0,0);
m_PlazaBrowser.Create(NULL,NULL,WS_CHILD|WS_VISIBLE,rcCreate,this,1001,NULL);
m_PlazaBrowser.SetWindowPos(NULL,0,0,400,300,SWP_SHOWWINDOW);
m_PlazaBrowser.Navigate(TEXT("http://192.168.2.254/ie.html"),NULL,NULL,NULL,NULL);