您的位置:首页 > 编程语言 > C#

使用C#编写BHO实现残障人群上网冲浪无障碍

2010-12-17 17:13 330 查看
原文发表于作者博客 http://www.nod6.net——以c#和Objective-C为主题的网站。



先看一个效果图:





位于下方的工具栏即是我们编写的BHO组件。它本质上是一个DLL,是由IE浏览器加载的,它的生命周期=浏览器进程的生命周期。更多关于BHO的信息,请大家自行Google。IE8是多进程浏览器,因此没打开一个选项卡,或者每打开一个新的窗口就会启动这样一个BHO组件。那么,IE是如何知道要加载哪些BHO呢?答案是:注册表。注册表是Windows最重要的配置数据库,BHO的配置也不例外,它位于注册表的如下位置:

Software/Microsoft/Windows/CurrentVersion/Explorer/Browser Helper Objects

因此,我们只需要编写一个bat文件注册即可:

C:/Windows/Microsoft.NET/Framework/v4.0.30319/regasm /codebase “bin/Debug/MyBHO.dll”

那么,接下来我们关注下BHO本身,如何编写一个BHO组件呢?很简单,只需4步即可搞定:

1、编写一个接口:

using System;
using System.Runtime.InteropServices;
namespace MyBHO
{
    [
    ComVisible(true),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
    Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
    ]
    public interface IObjectWithSite
    {
        [PreserveSig]
        int SetSite([MarshalAs(UnmanagedType.IUnknown)]Object site);
        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);
    }
}


2、实现该接口

[
ComVisible(true),
Guid("B4BC397F-B346-4A79-972F-1B4D6D81DA1E"),
ClassInterface(ClassInterfaceType.None)
]
public class FirstBHO : IObjectWithSite
{
       //...
}


3、生成证书,并将证书应用到该BHO项目

“C:/Program Files/Microsoft SDKs/Windows/v7.0A/bin/NETFX 4.0 Tools/sn” -k MyBHO.key

4、注册安装。
其中第2步,需要重点关注SetSite方法,如下:

public int SetSite(object site)
{
	if (site != null)
	{
		_webBrowser = site as WebBrowser;
		if (_webBrowser != null)
		{
			DocumentComplete += _form.OnDocumentComplete;
			_form.Gray += new EventHandler(_form_Gray);
			_form.Reload += new EventHandler(_form_Reload);
			_form.Restore += new EventHandler(_form_Restore);
			_form.ZoomIn += new EventHandler(_form_ZoomIn);
			_form.Zoom += new BHOForm.ZoomDelegate(_form_Zoom);
			_form.Invert += new EventHandler(_form_Invert);
			_form.XRay += new EventHandler(_form_XRay);
			_form.Speech += new EventHandler(_form_Speech);
			_form.StopSpeech += new EventHandler(_form_StopSpeech);
			_webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(webBrowser_DocumentComplete);
		}
	}
	else
	{
		if (_webBrowser != null)
		{
			_webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(webBrowser_DocumentComplete);
			_webBrowser = null;
		}
	}
	return 0;
}

至于界面,只需要新建一个Form就可以了,Form里没有任何业务逻辑(业务逻辑在FirstBHO中),只是一个人机接口而已。
至于Form和业务逻辑类FirstBHO的交互,是通过事件实现的。下面给出Form的代码:

using System;
using System.Windows.Forms;
 
namespace MyBHO
{
    public partial class BHOForm : Form
    {
        public delegate void ZoomDelegate(object sender, ZoomEventArgs e);
 
        public event EventHandler ZoomIn;
        public event EventHandler Gray;
        public event EventHandler Reload;
        public event EventHandler Restore;
        public event EventHandler Invert;
        public event ZoomDelegate Zoom;
        public event EventHandler XRay;
        public event EventHandler Speech;
        public event EventHandler StopSpeech;
 
        public BHOForm()
        {
            InitializeComponent();
        }
 
        public void OnDocumentComplete(object sender, BHOEventArgs e)
        {
            if (!this.Visible)
            {
                this.Show();
            }
 
            this.textBox1.Text = e.Data as string;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            ZoomIn(this, new EventArgs());
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            Gray(this, new EventArgs());
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            Reload(this, new EventArgs());
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            Restore(this, new EventArgs());
        }
 
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Zoom(this, new ZoomEventArgs { ZoomValue = trackBar1.Value * 10 });
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            Invert(this, new EventArgs());
        }
 
        private void button6_Click(object sender, EventArgs e)
        {
            XRay(this, new EventArgs());
        }
 
        private void BHOForm_Load(object sender, EventArgs e)
        {
            Height = 80;
            textBox1.Visible = false;
            CalcWinLocation();
        }
 
        private void CalcWinLocation()
        {
            Top = Screen.PrimaryScreen.WorkingArea.Height - Height - 10;
            Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
        }
 
 
        private void button7_Click(object sender, EventArgs e)
        {
            Speech(this, new EventArgs());
        }
 
        private void button8_Click(object sender, EventArgs e)
        {
            StopSpeech(this, new EventArgs());
        }
 
        private void BHOForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (Height == 80)
            {
                Height = 300;
                textBox1.Visible = true;
            }
            else if (Height == 300)
            {
                Height = 80;
                textBox1.Visible = false;
            }
            CalcWinLocation();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐