Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C#

The most complete C# Webbrowser wrapper control

Rate me:
Please Sign up or sign in to vote.
4.90/5 (141 votes)
28 May 2007CPOL21 min read 3.1M   40K   511  
A C# (.NET 2.0) control which creates, hosts, and offers advanced customization such as dragdrop, file downloads, HTTP/S header viewing, and much more.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IfacesEnumsStructsClasses;

namespace DemoApp
{
    public partial class frmPopup : Form
    {

        public frmPopup()
        {
            InitializeComponent();
        }

        public void AssignBrowserObject(ref object obj)
        {
            obj = this.cEXWB1.WebbrowserObject;
        }

        private void frmPopup_Load(object sender, EventArgs e)
        {
            this.cEXWB1.RegisterAsBrowser = true;
            this.cEXWB1.WBDragDrop += new csExWB.WBDropEventHandler(cEXWB1_WBDragDrop);
            this.cEXWB1.NavToBlank();
        }

        void cEXWB1_WBDragDrop(object sender, csExWB.WBDropEventArgs e)
        {
            if (e.dataobject.ContainsText())
                AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_WBDragDrop\r\n" + e.dataobject.GetText());
            else if (e.dataobject.ContainsFileDropList())
            {
                System.Collections.Specialized.StringCollection files = e.dataobject.GetFileDropList();
                if (files.Count > 1)
                    MessageBox.Show("Can not do multi file drop!");
                else
                {
                    this.cEXWB1.Navigate(files[0]);
                }
            }
        }

        private void frmPopup_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                this.cEXWB1.NavToBlank();
                e.Cancel = true;
                this.Hide();
            }
        }

        /// <summary>
        /// Sets the DL flags of the browser to be as restrictive as possible
        /// </summary>
        public void SetupAsSandBox()
        {
            this.cEXWB1.WBDOCDOWNLOADCTLFLAG = (int)(
                DOCDOWNLOADCTLFLAG.NO_SCRIPTS | 
                DOCDOWNLOADCTLFLAG.NO_JAVA | 
                DOCDOWNLOADCTLFLAG.NOFRAMES | 
                DOCDOWNLOADCTLFLAG.NO_DLACTIVEXCTLS | 
                DOCDOWNLOADCTLFLAG.NO_RUNACTIVEXCTLS | 
                DOCDOWNLOADCTLFLAG.PRAGMA_NO_CACHE | 
                DOCDOWNLOADCTLFLAG.SILENT | 
                DOCDOWNLOADCTLFLAG.NO_BEHAVIORS | 
                DOCDOWNLOADCTLFLAG.NO_CLIENTPULL);
        }

        private void cEXWB1_StatusTextChange(object sender, csExWB.StatusTextChangeEventArgs e)
        {
            lblStatus.Text = e.text;
        }

        private void cEXWB1_TitleChange(object sender, csExWB.TitleChangeEventArgs e)
        {
            this.Text = e.title;
        }

        private void cEXWB1_WBDocHostShowUIShowMessage(object sender, csExWB.DocHostShowUIShowMessageEventArgs e)
        {
            AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_WBDocHostShowUIShowMessage\r\n" + e.text);
        }

        private void cEXWB1_WindowClosing(object sender, csExWB.WindowClosingEventArgs e)
        {
            this.cEXWB1.NavToBlank();
            e.Cancel = true;
            this.Hide();
        }

        private void cEXWB1_WindowSetHeight(object sender, csExWB.WindowSetHeightEventArgs e)
        {
            if( (!this.Visible) || (this.WindowState == FormWindowState.Maximized) ||
                (this.WindowState == FormWindowState.Minimized) || (e.height < 0) )
                return;
            this.Height = e.height;
        }

        private void cEXWB1_WindowSetWidth(object sender, csExWB.WindowSetWidthEventArgs e)
        {
            if ((!this.Visible) || (this.WindowState == FormWindowState.Maximized) ||
                (this.WindowState == FormWindowState.Minimized) || (e.width < 0))
                return;
            this.Width = e.width;
        }

        private void cEXWB1_ScriptError(object sender, csExWB.ScriptErrorEventArgs e)
        {
            AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_ScriptError");
            e.continueScripts = true;
        }

        private void cEXWB1_NavigateError(object sender, csExWB.NavigateErrorEventArgs e)
        {
            AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_NavigateError");
            e.Cancel = true;
        }

        private void cEXWB1_NewWindow2(object sender, csExWB.NewWindow2EventArgs e)
        {
            try
            {
                AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_NewWindow2");
                e.Cancel = true;
            }
            catch (Exception ee)
            {
                AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_NewWindow2\r\n" + ee.ToString());
            }
        }

        private void cEXWB1_NewWindow3(object sender, csExWB.NewWindow3EventArgs e)
        {
            try
            {
                e.Cancel = true;
            }
            catch (Exception ee)
            {
                AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_NewWindow3\r\n" + ee.ToString());
            }
        }

        private void cEXWB1_WBEvaluteNewWindow(object sender, csExWB.EvaluateNewWindowEventArgs e)
        {
            e.Cancel = true;
        }
        
        private void cEXWB1_BeforeNavigate2(object sender, csExWB.BeforeNavigate2EventArgs e)
        {
            AllForms.m_frmLog.AppendToLog("frmPopup_BeforeNavigate2\r\n" + e.url);
        }

        private void cEXWB1_WindowSetLeft(object sender, csExWB.WindowSetLeftEventArgs e)
        {
            if ((!this.Visible) || (this.WindowState == FormWindowState.Maximized) ||
                (this.WindowState == FormWindowState.Minimized) || (e.left < 0))
                return;
            this.Left = e.left;
        }

        private void cEXWB1_WindowSetTop(object sender, csExWB.WindowSetTopEventArgs e)
        {
            if ((!this.Visible) || (this.WindowState == FormWindowState.Maximized) ||
                (this.WindowState == FormWindowState.Minimized) || (e.top < 0))
                return;
            this.Top = e.top;
        }

        private void cEXWB1_WBSecurityProblem(object sender, csExWB.SecurityProblemEventArgs e)
        {
            AllForms.m_frmLog.AppendToLog("frmPopup_cEXWB1_WBSecurityProblem");
            
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions