Домой Network Web Browser and HTML source: Справочник по C#

Web Browser and HTML source: Справочник по C#

682
0







Form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WebBrowser
{
/// 
/// Summary description for Form1.
/// 
public class Form1 : System.Windows.Forms.Form
{
private object empty;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnSource;
private System.Windows.Forms.TextBox txtHTML;
private System.Windows.Forms.ComboBox cboURL;
private System.Windows.Forms.Button btnGO;
/// 
/// Required designer variable.
/// 
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// 
/// Clean up any resources being used.
/// 
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// 
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// 
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnSource = new System.Windows.Forms.Button();
this.btnGO = new System.Windows.Forms.Button();
this.cboURL = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtHTML = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.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.groupBox1.Controls.Add(this.btnSource);
this.groupBox1.Controls.Add(this.btnGO);
this.groupBox1.Controls.Add(this.cboURL);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.axWebBrowser1);
this.groupBox1.Location = new System.Drawing.Point(8, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(456, 288);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// btnSource
//
this.btnSource.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.btnSource.Location = new System.Drawing.Point(349, 16);
this.btnSource.Name = "btnSource";
this.btnSource.Size = new System.Drawing.Size(80, 21);
this.btnSource.TabIndex = 4;
this.btnSource.Text = "Get Source";
this.btnSource.Click += new System.EventHandler(this.btnSource_Click);
//
// btnGO
//
this.btnGO.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.btnGO.Location = new System.Drawing.Point(305, 16);
this.btnGO.Name = "btnGO";
this.btnGO.Size = new System.Drawing.Size(40, 21);
this.btnGO.TabIndex = 3;
this.btnGO.Text = "Go";
this.btnGO.Click += new System.EventHandler(this.btnGO_Click);
//
// cboURL
//
this.cboURL.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.cboURL.Location = new System.Drawing.Point(81, 16);
this.cboURL.Name = "cboURL";
this.cboURL.Size = new System.Drawing.Size(216, 21);
this.cboURL.TabIndex = 2;
//
// label1
//
this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.label1.Location = new System.Drawing.Point(17, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 21);
this.label1.TabIndex = 1;
this.label1.Text = "Enter URL:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// axWebBrowser1
//
this.axWebBrowser1.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.axWebBrowser1.ContainingControl = this;
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(8, 45);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(440, 235);
this.axWebBrowser1.TabIndex = 0;
//
// groupBox2
//
this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox2.Controls.Add(this.txtHTML);
this.groupBox2.ForeColor = System.Drawing.Color.Blue;
this.groupBox2.Location = new System.Drawing.Point(8, 296);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(456, 200);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "View HTML Source";
//
// txtHTML
//
this.txtHTML.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtHTML.Location = new System.Drawing.Point(8, 16);
this.txtHTML.Multiline = true;
this.txtHTML.Name = "txtHTML";
this.txtHTML.ReadOnly = true;
this.txtHTML.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtHTML.Size = new System.Drawing.Size(440, 176);
this.txtHTML.TabIndex = 0;
this.txtHTML.Text = "";
this.txtHTML.WordWrap = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(472, 502);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.MinimumSize = new System.Drawing.Size(480, 536);
this.Name = "Form1";
this.Text = "Web Browser in C#";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// 
/// The main entry point for the application.
/// 
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
empty = System.Reflection.Missing.Value;
axWebBrowser1.Navigate("about:blank", ref empty, ref empty, ref empty, ref empty);
}
private void btnGO_Click(object sender, System.EventArgs e)
{
txtHTML.Text ="";
if (cboURL.Text =="")
{
MessageBox.Show("Please enter URL!");
}
else
{
try
{
addURL();
Cursor.Current = Cursors.WaitCursor;
axWebBrowser1.Navigate(appendURL(), ref empty, ref empty, ref empty, ref empty);
Cursor.Current = Cursors.Default;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void addURL()
{
string tmp = cboURL.Text;
int myindex = cboURL.FindStringExact(tmp);
if (myindex != -1)
{
return;
}
else
{
cboURL.Items.Add(tmp);
}
}
private string appendURL()
{
string completeURL="";
bool test = cboURL.Text.StartsWith("http://");
if (test == false)
{
System.Text.StringBuilder sbURL = new System.Text.StringBuilder(25);
sbURL.Append("http://");
sbURL.Append(cboURL.Text);
completeURL = sbURL.ToString();
}
else
{
completeURL = cboURL.Text;
}
return completeURL;
}
private void btnSource_Click(object sender, System.EventArgs e)
{
if (cboURL.Text == "")
{
MessageBox.Show("Please enter URL!");
}
else
{
try
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2) axWebBrowser1.Document;
txtHTML.Text= ((mshtml.HTMLDocumentClass)(doc)).documentElement.outerHTML;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}

Взято с http://csharp4newbies.com
Исходник: WebBrowser.zip

ЧИТАТЬ ТАКЖЕ:  Получение списка доступных в сети SQL серверов: Справочник по C#

Web Browser and HTML source: Справочник по C#

0.00 (0%) 0 votes

ОСТАВЬТЕ ОТВЕТ

Пожалуйста, введите ваш комментарий!
пожалуйста, введите ваше имя здесь