📄 form1.cs
字号:
namespace HTMLSourceViewer
{
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Windows.Forms;
public class GetWebPageSource
{
public GetWebPageSource() { }
public GetWebPageSource(string webAddress)
{
this.webAddress = webAddress;
}
public string GetSource()
{
StringBuilder strSource = new StringBuilder("");
try
{
WebRequest WReq = WebRequest.Create(this.webAddress);
WebResponse WResp = WReq.GetResponse();
// get the stream of data
StreamReader sr = new StreamReader(WResp.GetResponseStream(), Encoding.ASCII);
string strTemp = "";
while ((strTemp = sr.ReadLine()) != null)
{
strSource.Append(strTemp + "\r\n");
}
sr.Close();
}
catch (WebException WebExcp)
{
MessageBox.Show(WebExcp.Message, "Error", MessageBoxButtons.OK);
}
return strSource.ToString();
}
// Accessor method
public void setWebAddress(string strNewAddress)
{
this.webAddress = strNewAddress;
}
// private variables
private string webAddress;
}
}
namespace HTMLSourceViewer
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Remoting;
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private const string HTTP = "http://"; // scheme identifier
private string strSource = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.Color.LightGoldenrodYellow;
this.label1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 24);
this.label1.TabIndex = 0;
this.label1.Text = "网页地址:";
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.textBox1.BackColor = System.Drawing.Color.LightGoldenrodYellow;
this.textBox1.ForeColor = System.Drawing.Color.Brown;
this.textBox1.Location = new System.Drawing.Point(112, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(408, 25);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "http://www.yahoo.com";
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// button1
//
this.button1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.button1.BackColor = System.Drawing.Color.LightGoldenrodYellow;
this.button1.ForeColor = System.Drawing.Color.Brown;
this.button1.Location = new System.Drawing.Point(528, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 32);
this.button1.TabIndex = 2;
this.button1.Text = "源码";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.button2.BackColor = System.Drawing.Color.LightGoldenrodYellow;
this.button2.ForeColor = System.Drawing.Color.Brown;
this.button2.Location = new System.Drawing.Point(584, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 32);
this.button2.TabIndex = 3;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.BackColor = System.Drawing.Color.LightGoldenrodYellow;
this.label2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 24);
this.label2.TabIndex = 4;
this.label2.Text = "源代码:";
//
// textBox2
//
this.textBox2.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.textBox2.BackColor = System.Drawing.Color.LightGoldenrodYellow;
this.textBox2.ForeColor = System.Drawing.Color.Brown;
this.textBox2.Location = new System.Drawing.Point(16, 88);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox2.Size = new System.Drawing.Size(616, 368);
this.textBox2.TabIndex = 5;
this.textBox2.Text = "HTML Source Code";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.BackColor = System.Drawing.Color.Goldenrod;
this.ClientSize = new System.Drawing.Size(648, 463);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.label2,
this.button2,
this.button1,
this.textBox1,
this.label1});
this.Name = "Form1";
this.Text = "网页源码浏览程序";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char chr = e.KeyChar;
//13 = Enter key
if (chr == 13)
{
GetPageSource();
}
}
private void button1_Click(object sender, System.EventArgs e)
{
GetPageSource();
}
public void GetPageSource()
{
string strAddress = textBox1.Text.Trim();
strAddress = strAddress.ToLower();
if ( strAddress.Length <= HTTP.Length)
{
MessageBox.Show("Enter a web address", "Web Address Field", MessageBoxButtons.OK);
textBox1.Text = HTTP;
}
else if (!strAddress.StartsWith(HTTP))
{
MessageBox.Show("You have Entered the wrong Protocol.", "Wrong Scheme Identifier", MessageBoxButtons.OK);
textBox1.Text = HTTP;
}
else
{
// create the GetWebPageSource object
HTMLSourceViewer.GetWebPageSource objGS = new HTMLSourceViewer.GetWebPageSource();
objGS.setWebAddress(strAddress);
strSource = objGS.GetSource();
if (strSource.Length > 1)
{
showSource();
}
}
}
public void showSource()
{
Form1.ActiveForm.Height = 608;
textBox2.Text = strSource;
label2.Visible = true;
textBox2.Visible = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -