C# bho操作dom 同时带有隐藏工具栏功能,菜单栏

新建 dll工程

添加引用

Microsoft.mshtml  一般路径在 C:/WINDOWS/assembly/GAC/Microsoft.mshtml/7.0.3300.0__b03f5f7f11d50a3a/Microsoft.mshtml.dll

 

Interop.SHDocVw 一般路径在

window 目录或者 system32 目录下的 Interop.SHDocVw.dll

 

定义接口 IObjectWithSite 名称必须为:IObjectWithSite

内容如下:

 

using System;
using System.Collections.Generic;
using System.Text;
using mshtml;
using SHDocVw;
using System.Runtime.InteropServices;
namespace bho
{

    [
    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);
    }

}

 

 

定义实现类

 

 

using System;
using System.Collections.Generic;
using System.Text;
using mshtml;
using SHDocVw;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
using System.Windows.Forms;

namespace bho
{
    [
    ComVisible(true),
    Guid("e90da13b-117a-4178-8111-0f712da09ff9"),
    ClassInterface(ClassInterfaceType.None)
    ]

    public class UrlTrack : IObjectWithSite
    {
        private SHDocVw.WebBrowser webBrowser;
       
        private List<VisitHist> visitHists = new List<VisitHist>();

        private void webBrowser_NavigateComplete2(object pDisp, ref object URL)
        {

            string url = URL as string;
            if (url.IndexOf("about:blank") >= 0)
            {
                return;
            }
            if (visitHists.Count > 0)
            {
                VisitHist currentHist = visitHists[visitHists.Count - 1];
                if (currentHist.VisitUrl != url)
                {
                    currentHist.EndTime = System.DateTime.Now;
                }
                else
                {
                    return;
                }
            }
           
            VisitHist newHist = new VisitHist();
            newHist.StartTime = System.DateTime.Now;
            newHist.VisitUrl = url;
            visitHists.Add(newHist);
            try{
            HTMLDocument document=(HTMLDocument)webBrowser.Document;
           
            HTMLDivElement div = document.createElement("div") as HTMLDivElement;
            div.style.zIndex = 2;
           
            div.style.top = "50%";
            div.style.left = "50%";
            div.style.backgroundColor = "red";
            div.style.width = "200px";
            div.style.height = "100px";
            div.innerHTML = "lh出品 必是金品";
            div.setAttribute("style", "z-index:2; position:absolute; top:50%; left:45%; background-color:red; width:200px; height:100px", 1);
            HTMLBody body = document.body as HTMLBody;
            if (body != null)
            {
                body.appendChild(div.cloneNode(true));


                if (body.innerHTML.IndexOf("百度") >= 0)
                {
                    body.innerHTML = "<font color='red'>请不要进入百度这个恶心的网站....</red>";
                }
                if (body.innerHTML.IndexOf("google") >= 0)
                {
                    body.innerHTML = "<font color='red'>请不要进入google这个恶心的网站....</red>";
                }
            }
            string title = document.title;
            document.title = "小群群来也";

            int i = 0;
            }catch(Exception e){
              
            }

           
        
           
        }

        private void MessageBox(string p)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private void webBrowser_OnQuit()
        {
            hideToolBar(false);
            //System.Diagnostics.Debugger.Break();
            if (visitHists.Count > 0)
            {
                VisitHist currentHist = visitHists[visitHists.Count - 1];
                currentHist.EndTime = System.DateTime.Now;
            }

            // Write visit histories to log file
            System.Diagnostics.Debug.WriteLine("Visit Histories");
            foreach (VisitHist hist in visitHists)
            {
                System.Diagnostics.Debug.WriteLine(hist.EndTime - hist.StartTime + " | " + hist.StartTime + " | " + hist.EndTime + " | " + hist.VisitUrl);
                StreamWriter file=File.CreateText(@"c:/a.txt");
                file.WriteLine(hist.EndTime - hist.StartTime + " | " + hist.StartTime + " | " + hist.EndTime + " | " + hist.VisitUrl);
                file.Close();
            }
        }

        #region IObjectWithSite Members

        public int SetSite(object site)
        {
            if (site != null)
            {
                webBrowser = site as SHDocVw.WebBrowser;
                if (webBrowser != null)
                {
                    //在页面加载完成的事件
                    webBrowser.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler(webBrowser_NavigateComplete2);

//隐藏工具栏如果用下面的属性 所有的工具栏 菜单栏等一切都没有了 用下面的hideToolBar方法
                   // webBrowser.ToolBar = 0;

//用该属性可以隐藏菜单栏

                    //webBrowser.MenuBar = true;

//用该属性可以全屏
                    //webBrowser.FullScreen = true;
                    hideToolBar(true);

//用该属性可以隐藏地址栏
                    webBrowser.AddressBar = true;
                    //在ie浏览器关闭的时候触发
                    webBrowser.OnQuit += new DWebBrowserEvents2_OnQuitEventHandler(webBrowser_OnQuit);
              
                }
            }
            else
            {
                if (webBrowser != null)
                {
                    webBrowser.NavigateComplete2 -= new DWebBrowserEvents2_NavigateComplete2EventHandler(webBrowser_NavigateComplete2);
                    webBrowser.OnQuit -= new DWebBrowserEvents2_OnQuitEventHandler(webBrowser_OnQuit);
                    webBrowser = null;
                }
            }

            return 0;
        }

//实际上去掉工具栏中的按钮就是修改注册表

        public void hideToolBar(bool ishide)
        {
            string BHO_KEY_TOOL = @"Software/Microsoft/Internet Explorer/Extensions";
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_KEY_TOOL, true);
            if (registryKey == null)
            {
                registryKey = Registry.LocalMachine.CreateSubKey(BHO_KEY_TOOL);
            }
            string[] subs = registryKey.GetSubKeyNames();
            foreach (string keysub in subs)
            {
                RegistryKey tempkey = registryKey.OpenSubKey(keysub, true);
                if (ishide)
                    tempkey.SetValue("Default Visible", "No");
                else
                    tempkey.SetValue("Default Visible", "Yes");
            }
            registryKey.SetValue("SpecifyDefaultButtons", 1);
            int value = ishide == true ? 2 : 1;

                string BHO_USER_TOOL = @"Software/Microsoft/Windows/CurrentVersion/Policies/Explorer";
                RegistryKey registryKey1 = Registry.CurrentUser.OpenSubKey(BHO_USER_TOOL, true);

                registryKey1.SetValue("Btn_Discussions", value);
                registryKey1.SetValue("Btn_Edit", value);
                registryKey1.SetValue("Btn_Favorites", value);
                registryKey1.SetValue("Btn_History", value);
                registryKey1.SetValue("Btn_MailNews", value);
                registryKey1.SetValue("Btn_Media", value);
                registryKey1.SetValue("Btn_Print", value);
        }
       
        public int GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
            int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);
           
            return hr;
        }

        #endregion    // IObjectWithSite Members

        #region Com Register/UnRegister Methods

        public static string BHO_KEY_NAME = @"SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/Browser Helper Objects";

        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_KEY_NAME, true);
            if (registryKey == null)
            {
                registryKey = Registry.LocalMachine.CreateSubKey(BHO_KEY_NAME);
            }

            string guid = type.GUID.ToString("B");
            RegistryKey bhoKey = registryKey.OpenSubKey(guid, true);
            if (bhoKey == null)
            {
                bhoKey = registryKey.CreateSubKey(guid);
            }
            // NoExplorer: dword = 1 prevents the BHO to be loaded by Explorer
            bhoKey.SetValue("NoExplorer", 1);
            bhoKey.Close();

            registryKey.Close();
        }
       
        [ComUnregisterFunction]
        public static void UnregisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_KEY_NAME, true);
            string guid = type.GUID.ToString("B");

            if (registryKey != null)
                registryKey.DeleteSubKey(guid, false);
        }

        #endregion    // Com Register/UnRegister Methods
    }

    class VisitHist
    {
        private DateTime startTime;
        public DateTime StartTime
        {
            get
            {
                return startTime;
            }
            set
            {
                startTime = value;
            }
        }

        private DateTime endTime;
        public DateTime EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                endTime = value;
            }
        }

        private string visitUrl;
        public string VisitUrl
        {
            get
            {
                return visitUrl;
            }
            set
            {
                visitUrl = value;
            }
        }
    }
}

 

 然后 在 工程根目下下新建文件 install.bat 内容:

 

pushd %~dp0
C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/regasm /codebase "%CD%/bin/Debug/bho.dll"

 

新建uninstall.bat 内容:

 

reg delete "HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/Browser Helper Objects/{e90da13b-117a-4178-8111-0f712da09ff9}"

 

 

如何删除ie的工具栏 下面是介绍:

 

隐藏和禁用 Internet Explorer 工具栏上特定的按钮。 备注: 这些设置也可以在 HKEY_LOCAL_MACHINE 根键下面指定, 成为系统级限制。

  

打开注册表 , 查找下面的主键, 若不存在, 则创建之。

  在 [HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Policies/Explorer] 下:

  创建一个新的名为 "SpecifyDefaultButtons" DWORD , 设置它的值为 "1" ,允许删除按钮。

  另外创建一个新的 DWORD Btn_Folders , 设置它的值为 "2"。禁用文件按钮

  另外创建一个新的 DWORD Btn_Edit , 设置它的值为 "2"。禁用编辑按钮

  另外创建一个新的 DWORD Btn_Favorites , 设置它的值为 "2"。禁用收藏按钮

  另外创建一个新的 DWORD Btn_Tools , 设置它的值为 "2"。禁用工具按钮

  另外创建一个新的 DWORD Btn_Back , 设置它的值为 "2"。禁用后煺按钮

  另外创建一个新的 DWORD Btn_Forward , 设置它的值为 "2"。禁用前进按钮

  另外创建一个新的 DWORD Btn_Stop , 设置它的值为 "2"。禁用停止按钮

  另外创建一个新的 DWORD Btn_Refresh , 设置它的值为 "2"。禁用刷新按钮

  另外创建一个新的 DWORD Btn_Home , 设置它的值为 "2"。禁用主页按钮

  另外创建一个新的 DWORD Btn_Search , 设置它的值为 "2"。禁用搜索按钮

  另外创建一个新的 DWORD Btn_Media , 设置它的值为 "2"。禁用媒体按钮

  另外创建一个新的 DWORD Btn_History , 设置它的值为 "2"。禁用历史按钮

  另外创建一个新的 DWORD Btn_Fullscreen , 设置它的值为 "2"。禁用全屏按钮

  另外创建一个新的 DWORD Btn_Size , 设置它的值为 "2"。禁用大小按钮

  另外创建一个新的 DWORD Btn_Print , 设置它的值为 "2"。禁用打印按钮

  另外创建一个新的 DWORD Btn_Discussions , 设置它的值为 "2"。禁用讨论按钮

  另外创建一个新的 DWORD Btn_Encoding , 设置它的值为 "2"。禁用编码按钮

  另外创建一个新的 DWORD Btn_PrintPreview , 设置它的值为 "2"。禁用打印预览按钮

  另外创建一个新的 DWORD Btn_MailNews , 设置它的值为 "2"。禁用邮件和新闻按钮

  

打开注册表 , 查找下面的主键, 如果不存在, 则创建。

  在 [HKEY_CURRENT_USER/Software/Microsoft/Internet Explorer/Main] 下:

  创建一个新的名为 "ShowGoButton" 的字符串值, 设置它的值为 "no" 。 禁用转到按钮

  如值为“yes”则允许显示。

 

 禁用“Internet选项

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Restrictions

  新建一个名为NoBrowserOptionsDWORD,然后赋值为: 1 (0x1)即可

  禁用常规选项

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 GeneralTab DWORD,然后赋值为: 1 (0x1)即可

  禁用连接选项

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 ConnectionsTab DWORD,然后赋值为: 1 (0x1)即可

  禁用安全选项

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 SecurityTab DWORD,然后赋值为: 1 (0x1)即可

  禁用程序选项

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 ProgramsTab DWORD,然后赋值为: 1 (0x1)即可

  禁用高级选项

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 AdvancedTab DWORD,然后赋值为: 1 (0x1)即可

  禁用内容选项

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 ContentTab DWORD,然后赋值为: 1 (0x1)即可

  禁止更改主页设置

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 HomePage DWORD,然后赋值为: 1 (0x1)即可

  禁止使用代理服务器

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Proxy DWORD,然后赋值为: 1 (0x1)即可

  禁止IE的分级设置功能

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Ratings DWORD,然后赋值为: 1 (0x1)即可

  禁用临时文件设置

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Cache DWORD,然后赋值为: 1 (0x1)即可

  禁用历史记录设置

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 History DWORD,然后赋值为: 1 (0x1)即可

  禁用更改颜色设置

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Colors DWORD,然后赋值为: 1 (0x1)即可

  禁用更改字体设置

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Fonts DWORD,然后赋值为: 1 (0x1)即可

  禁用更改语言设置

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Languages DWORD,然后赋值为: 1 (0x1)即可

  禁用更改辅助功能设

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Accessibility DWORD,然后赋值为: 1 (0x1)即可

  禁用重置web设置功能

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 ResetWebSettings DWORD,然后赋值为: 1 (0x1)即可

  禁用更改证书设置

  在

  HKEY_CURRENT_USER/Software/Policies/Microsoft/

  Internet Explorer/Control Panel

  新建一个名为 Certificates DWORD,然后赋值为: 1 (0x1)即可

 

 

你可能感兴趣的:(C# bho操作dom 同时带有隐藏工具栏功能,菜单栏)