Csharp/CSharp Tutorial/GUI Windows Forms/WebBrowser

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A simple Browser

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class WebBrowserDemo
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        toolStripProgressBar1.Maximum = (int)e.MaximumProgress;
        toolStripProgressBar1.Value = (int) e.CurrentProgress;
    }
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
     
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.statusStrip1 = new System.Windows.Forms.StatusStrip();
        this.toolStripProgressBar1 = new System.Windows.Forms.ToolStripProgressBar();
        this.webBrowser1 = new System.Windows.Forms.WebBrowser();
        this.statusStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // statusStrip1
        // 
        this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.toolStripProgressBar1});
        this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;
        this.statusStrip1.Location = new System.Drawing.Point(0, 488);
        this.statusStrip1.Name = "statusStrip1";
        this.statusStrip1.Size = new System.Drawing.Size(695, 22);
        this.statusStrip1.TabIndex = 0;
        this.statusStrip1.Text = "statusStrip1";
        // 
        // toolStripProgressBar1
        // 
        this.toolStripProgressBar1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
        this.toolStripProgressBar1.Name = "toolStripProgressBar1";
        this.toolStripProgressBar1.Size = new System.Drawing.Size(100, 15);
        this.toolStripProgressBar1.Text = "toolStripProgressBar1";
        // 
        // webBrowser1
        // 
        this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.webBrowser1.Location = new System.Drawing.Point(0, 0);
        this.webBrowser1.Name = "webBrowser1";
        this.webBrowser1.Size = new System.Drawing.Size(695, 488);
        this.webBrowser1.Url = new System.Uri("http://www.nfex.ru", System.UriKind.Absolute);
        this.webBrowser1.ProgressChanged += new System.Windows.Forms.WebBrowserProgressChangedEventHandler(this.webBrowser1_ProgressChanged);
        this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(695, 510);
        this.Controls.Add(this.webBrowser1);
        this.Controls.Add(this.statusStrip1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.statusStrip1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.StatusStrip statusStrip1;
    private System.Windows.Forms.ToolStripProgressBar toolStripProgressBar1;
    private System.Windows.Forms.WebBrowser webBrowser1;
}

Browser with Url entry box

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
public class BrowserWithUrlEntryBox
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void goButton_Click(object sender, EventArgs e)
    {
        ftpBrowser.Navigate(addressBox.Text);
    }
}
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.IContainer components = null;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.ftpBrowser = new System.Windows.Forms.WebBrowser();
        this.label1 = new System.Windows.Forms.Label();
        this.addressBox = new System.Windows.Forms.TextBox();
        this.goButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // ftpBrowser
        // 
        this.ftpBrowser.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.ftpBrowser.Location = new System.Drawing.Point(16, 42);
        this.ftpBrowser.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.ftpBrowser.Name = "ftpBrowser";
        this.ftpBrowser.Size = new System.Drawing.Size(917, 524);
        this.ftpBrowser.TabIndex = 4;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(15, 11);
        this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(60, 17);
        this.label1.TabIndex = 1;
        this.label1.Text = "Address";
        // 
        // addressBox
        // 
        this.addressBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.addressBox.Location = new System.Drawing.Point(77, 7);
        this.addressBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.addressBox.Name = "addressBox";
        this.addressBox.Size = new System.Drawing.Size(572, 22);
        this.addressBox.TabIndex = 2;
        // 
        // goButton
        // 
        this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.goButton.Location = new System.Drawing.Point(659, 5);
        this.goButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.goButton.Name = "goButton";
        this.goButton.Size = new System.Drawing.Size(100, 28);
        this.goButton.TabIndex = 3;
        this.goButton.Text = "Go";
        this.goButton.Click += new System.EventHandler(this.goButton_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(949, 581);
        this.Controls.Add(this.goButton);
        this.Controls.Add(this.addressBox);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.ftpBrowser);
        this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    #endregion
    private System.Windows.Forms.WebBrowser ftpBrowser;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox addressBox;
    private System.Windows.Forms.Button goButton;
}

Online Book Browser

/*
Mastering Visual C#.Net
# Paperback: 800 pages
# Publisher: Sybex; 1st edition (August 20, 2002)
# Language: English
# ISBN-10: 0782129110
# ISBN-13: 978-0782129113
*/
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
class Book
{
    public string BookTitle;
    public string BookURL;
    public Book( string title, string url)
    {
        BookTitle = title;
        BookURL = url;
    }
    public override string ToString()
    {
        return BookTitle;
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void bookCheckButton_Click(object sender, EventArgs e)
    {
        GetWebPage("http://www.apress.ru/book/forthcoming.html");
    }
    private MatchCollection GetBookDetailsFromWebPage(string webPage)
    {
        // Use a regex here to find all the book info
        Regex newBooksRegEx =
            new Regex(
            "<a href=\"(/book/bookDisplay\\.html\\?bID=[0-9]+)\">([^<]+)</a>",
            RegexOptions.Singleline);
        return newBooksRegEx.Matches(webPage);
    }
    private void AddBooksToListBox(MatchCollection books)
    {
        foreach (Match bookMatch in books)
        {
            bookList.Items.Add(
                new Book(bookMatch.Groups[2].Value, bookMatch.Groups[1].Value)
            );
        }
    }
    
    private void GetWebPage(string url)
    {
        WebClient web = new WebClient();
        web.DownloadStringCompleted += 
            new DownloadStringCompletedEventHandler(DownloadComplete);
        web.DownloadProgressChanged += 
            new DownloadProgressChangedEventHandler(ProgressChanged);
        bookList.Items.Clear();
        web.DownloadStringAsync(new System.Uri(url));
    }

    private void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        downloadProgress.Value = e.ProgressPercentage;
    }
    private void DownloadComplete(object sender, DownloadStringCompletedEventArgs e)
    {
            downloadProgress.Value = 100;
            if (!e.Cancelled)
            {
                string newBooksPage = e.Result;
                AddBooksToListBox(
                    GetBookDetailsFromWebPage(newBooksPage)
                );
            }
    }
    private void bookList_DoubleClick(object sender, EventArgs e)
    {
        if (bookList.SelectedIndex != -1)
        {
            Book selectedBook = (Book)bookList.SelectedItem;
            browser.Navigate("http://www.apress.ru/" + selectedBook.BookURL, true);
        }
    }
}
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.IContainer components = null;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.bookList = new System.Windows.Forms.ListBox();
        this.bookCheckButton = new System.Windows.Forms.Button();
        this.browser = new System.Windows.Forms.WebBrowser();
        this.downloadProgress = new System.Windows.Forms.ProgressBar();
        this.SuspendLayout();
        // 
        // bookList
        // 
        this.bookList.FormattingEnabled = true;
        this.bookList.Location = new System.Drawing.Point(12, 40);
        this.bookList.Name = "bookList";
        this.bookList.Size = new System.Drawing.Size(410, 355);
        this.bookList.Sorted = true;
        this.bookList.TabIndex = 0;
        this.bookList.DoubleClick += new System.EventHandler(this.bookList_DoubleClick);
        // 
        // bookCheckButton
        // 
        this.bookCheckButton.Location = new System.Drawing.Point(304, 11);
        this.bookCheckButton.Name = "bookCheckButton";
        this.bookCheckButton.Size = new System.Drawing.Size(118, 23);
        this.bookCheckButton.TabIndex = 1;
        this.bookCheckButton.Text = "Check for books";
        this.bookCheckButton.Click += new System.EventHandler(this.bookCheckButton_Click);
        // 
        // browser
        // 
        this.browser.Location = new System.Drawing.Point(0, 0);
        this.browser.Name = "browser";
        this.browser.Size = new System.Drawing.Size(282, 34);
        this.browser.TabIndex = 0;
        this.browser.Visible = false;
        // 
        // downloadProgress
        // 
        this.downloadProgress.Location = new System.Drawing.Point(12, 401);
        this.downloadProgress.Name = "downloadProgress";
        this.downloadProgress.Size = new System.Drawing.Size(410, 23);
        this.downloadProgress.TabIndex = 2;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(436, 447);
        this.Controls.Add(this.downloadProgress);
        this.Controls.Add(this.browser);
        this.Controls.Add(this.bookCheckButton);
        this.Controls.Add(this.bookList);
        this.Name = "Form1";
        this.Text = "Find forthcoming Apress Books";
        this.ResumeLayout(false);
    }
    #endregion
    private System.Windows.Forms.ListBox bookList;
    private System.Windows.Forms.Button bookCheckButton;
    private System.Windows.Forms.WebBrowser browser;
    private System.Windows.Forms.ProgressBar downloadProgress;
}
public class OnlineBookBrowser
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

Simpel Web Browser with forward, backward button

using System;
using System.Windows.Forms;
public class SimpleWebBrowser : Form
{
    public SimpleWebBrowser()
    {
        InitializeComponent();
        webBrowser1.Navigate("http://www.nfex.ru");
    }
    private void goButton_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate(textURL.Text);
    }
    private void homeButton_Click(object sender, EventArgs e)
    {
        webBrowser1.GoHome();
    }
    private void backButton_Click(object sender, EventArgs e)
    {
        webBrowser1.GoBack();
    }
    private void forwarButton_Click(object sender, EventArgs e)
    {
        webBrowser1.GoForward();
    }
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        textURL.Text = webBrowser1.Url.ToString();
        if (webBrowser1.CanGoBack)
        {
            backButton.Enabled = true;
        }
        else
        {
            backButton.Enabled = false;
        }
        if (webBrowser1.CanGoForward)
        {
            forwarButton.Enabled = true;
        }
        else
        {
            forwarButton.Enabled = false;
        }
    }
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new SimpleWebBrowser());
    }
    private void InitializeComponent()
    {
        this.webBrowser1 = new System.Windows.Forms.WebBrowser();
        this.goButton = new System.Windows.Forms.Button();
        this.textURL = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.backButton = new System.Windows.Forms.Button();
        this.homeButton = new System.Windows.Forms.Button();
        this.forwarButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // webBrowser1
        // 
        this.webBrowser1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.webBrowser1.Location = new System.Drawing.Point(-2, 2);
        this.webBrowser1.Name = "webBrowser1";
        this.webBrowser1.Size = new System.Drawing.Size(685, 190);
        this.webBrowser1.TabIndex = 3;
        this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
        // 
        // goButton
        // 
        this.goButton.Location = new System.Drawing.Point(435, 216);
        this.goButton.Name = "goButton";
        this.goButton.Size = new System.Drawing.Size(48, 23);
        this.goButton.TabIndex = 1;
        this.goButton.Text = "Go";
        this.goButton.Click += new System.EventHandler(this.goButton_Click);
        // 
        // textURL
        // 
        this.textURL.Location = new System.Drawing.Point(240, 217);
        this.textURL.Name = "textURL";
        this.textURL.Size = new System.Drawing.Size(189, 20);
        this.textURL.TabIndex = 2;
        this.textURL.Text = "http://www.nfex.ru";
        // 
        // label1
        // 
        this.label1.Location = new System.Drawing.Point(206, 221);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(31, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "Go to:";
        // 
        // backButton
        // 
        this.backButton.Enabled = false;
        this.backButton.Location = new System.Drawing.Point(227, 249);
        this.backButton.Name = "backButton";
        this.backButton.Size = new System.Drawing.Size(75, 23);
        this.backButton.TabIndex = 0;
        this.backButton.Text = "<< Back";
        this.backButton.Click += new System.EventHandler(this.backButton_Click);
        // 
        // homeButton
        // 
        this.homeButton.Location = new System.Drawing.Point(308, 249);
        this.homeButton.Name = "homeButton";
        this.homeButton.Size = new System.Drawing.Size(75, 23);
        this.homeButton.TabIndex = 0;
        this.homeButton.Text = "Home";
        this.homeButton.Click += new System.EventHandler(this.homeButton_Click);
        // 
        // forwarButton
        // 
        this.forwarButton.Enabled = false;
        this.forwarButton.Location = new System.Drawing.Point(389, 249);
        this.forwarButton.Name = "forwarButton";
        this.forwarButton.Size = new System.Drawing.Size(75, 23);
        this.forwarButton.TabIndex = 0;
        this.forwarButton.Text = "Forward >>";
        this.forwarButton.Click += new System.EventHandler(this.forwarButton_Click);
        // 
        // SimpleWebBrowser
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(684, 303);
        this.Controls.Add(this.forwarButton);
        this.Controls.Add(this.homeButton);
        this.Controls.Add(this.backButton);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.textURL);
        this.Controls.Add(this.goButton);
        this.Controls.Add(this.webBrowser1);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.WebBrowser webBrowser1;
    private System.Windows.Forms.Button goButton;
    private System.Windows.Forms.TextBox textURL;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button backButton;
    private System.Windows.Forms.Button homeButton;
    private System.Windows.Forms.Button forwarButton;
}