张志松
记录工作点滴,留下人生轨迹。(zezese@163.com)
posts - 68,  comments - 11,  trackbacks - 0

头文件:

#pragma once

//事件回调接口,可以是接口也可以是类

//如果是接口就要在调用者类里面实现所有的事件
//有时我们只需要实现一两个事件,所以这里用类来实现事件回调接口

//把所有的事件都放在这个里面

class CWebBrowserEventHandler
{
protected:

 friend class CWebBrowser;  //让事件源可以调用事件回调函数
 
 CWebBrowserEventHandler(){}; //保护构造函数,只能继承,不能实例化

 //具体的事件回调函数
 
 //第一个参数为事件源,只要是为了区分具体的事件源

 virtual void WebBrowser_TitleChange(CWebBrowser& Sender, LPCTSTR lpszText){}
 virtual void WebBrowser_DocumentComplete(CWebBrowser& Sender, LPCTSTR lpszURL){}
};

class CWebBrowser : public CHtmlView
{
 DECLARE_DYNCREATE(CWebBrowser)
 DECLARE_MESSAGE_MAP()

public:

 CWebBrowser();         
 virtual ~CWebBrowser();

 //设置或者取消事件回调接口
 void SetEventHandler(CWebBrowserEventHandler* lpEventHandler = NULL);

 BOOL IsContextMenuEnabled() const { return m_bContextMenuEnabled; }
 void EnableContextMenu(BOOL newVal = TRUE) { m_bContextMenuEnabled = newVal; }

protected:

 virtual void PostNcDestroy();
 virtual BOOL PreTranslateMessage(MSG* pMsg);

 afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message);
 afx_msg void OnDestroy();

protected:

 //CHtmlView已经用虚函数实现事件的响应
 
 //具体是如何实现事件的响应可以查看CHtmlView的源码

 //作为例子,这里实现两个简单的事件

 virtual void OnTitleChange(LPCTSTR lpszText);
 virtual void OnDocumentComplete(LPCTSTR lpszURL);

protected:

 BOOL m_bContextMenuEnabled;

 //这里只有一个事件回调接口(其实可以用数组或链表来保存多个事件回调接口)
 //但大都情况下一个事件回调接口就已经足够了

 CWebBrowserEventHandler* m_lpEventHandler;
};

实现文件:

// WebBrowser.cpp : 实现文件
//

#include "stdafx.h"
#include ".\WebBrowser.h"

// CWebBrowser

IMPLEMENT_DYNCREATE(CWebBrowser, CHtmlView)

CWebBrowser::CWebBrowser()
: m_lpEventHandler(NULL)
, m_bContextMenuEnabled(TRUE)
{
}

CWebBrowser::~CWebBrowser()
{
}

BEGIN_MESSAGE_MAP(CWebBrowser, CHtmlView)
 ON_WM_DESTROY()
 ON_WM_MOUSEACTIVATE()
END_MESSAGE_MAP()


void CWebBrowser::SetEventHandler(CWebBrowserEventHandler* lpEventHandler /*= NULL*/)
{
 m_lpEventHandler = lpEventHandler;
}

// CWebBrowser 消息处理程序

void CWebBrowser::OnTitleChange(LPCTSTR lpszText)
{
 if (m_lpEventHandler != NULL)
 {
  m_lpEventHandler->WebBrowser_TitleChange(*this, lpszText);
 }

 CHtmlView::OnTitleChange(lpszText);
}

void CWebBrowser::OnDocumentComplete(LPCTSTR lpszURL)
{
 if (m_lpEventHandler != NULL)
 {
  m_lpEventHandler->WebBrowser_DocumentComplete(*this, lpszURL);
 }

 CHtmlView::OnDocumentComplete(lpszURL);
}


void CWebBrowser::OnDestroy()
{
 m_pBrowserApp.Release();

 m_wndBrowser.DestroyWindow();

 CWnd::OnDestroy();
}

int CWebBrowser::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
 return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}

void CWebBrowser::PostNcDestroy()
{
 //防止自删除

 //CHtmlView::PostNcDestroy();
}

BOOL CWebBrowser::PreTranslateMessage(MSG* pMsg)
{
 if (WM_RBUTTONUP == pMsg->message || WM_CONTEXTMENU == pMsg->message)
 {
  if (!IsContextMenuEnabled()) //屏蔽右键菜单和键盘菜单键
  {
   return TRUE;
  }
 }

 return CHtmlView::PreTranslateMessage(pMsg);
}


具体使用:

1.让调用者实现事件回调接口。

class CTestWebBrowserDlg : public CDialog, public CWebBrowserEventHandler

2.设置事件回调接口。

m_WebBrowser.SetEventHandler(this);

3.重写响应的事件回调函数。

void CTestWebBrowserDlg::WebBrowser_TitleChange(CWebBrowser& Sender, LPCTSTR lpszText)
{
 SetWindowText(lpszText);
}

在VS2003+XP下测试通过。

posted on 2011-01-09 13:50 张志松 阅读(1191) 评论(0)  编辑 收藏 引用 所属分类: VC/MFC

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

转载

搜索

  •  

最新评论

阅读排行榜

评论排行榜