C# WebBrowser使用 网络(二)

WebBrowser 简单操作

Form 代码

 1    public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 9         {
10             if (e.KeyChar == (char)13)
11             {
12                 webBrowser1.Navigate(textBox1.Text);
13             }
14         }
15 
16         private void Form1_Load(object sender, EventArgs e)
17         {
18             buttonBack.Enabled = false;
19             buttonForward.Enabled = false;
20             buttonStop.Enabled = false;
21 
22             this.webBrowser1.CanGoBackChanged += (s, k) => { if (webBrowser1.CanGoBack)  buttonBack.Enabled = true; else buttonBack.Enabled = false; };
23             this.webBrowser1.CanGoForwardChanged += (s, k) => { if (webBrowser1.CanGoForward) buttonForward.Enabled = true; else buttonForward.Enabled = false; };
24             this.webBrowser1.DocumentTitleChanged += (s, k) => { this.Text = webBrowser1.DocumentTitle.ToString(); };
25         }
26          
27         private void buttonBack_Click(object sender, EventArgs e)
28         {
29             webBrowser1.GoBack();
30             textBox1.Text = webBrowser1.Url.ToString();
31         }
32 
33         private void buttonForward_Click(object sender, EventArgs e)
34         {
35             webBrowser1.GoForward();
36             textBox1.Text = webBrowser1.Url.ToString();
37         }
38 
39         private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
40         {
41             this.Text = webBrowser1.DocumentTitle.ToString();
42             textBox1.Text = webBrowser1.Url.ToString();
43         }
44 
45         private void buttonStop_Click(object sender, EventArgs e)
46         {
47             webBrowser1.Stop();
48         }
49 
50         private void buttonHome_Click(object sender, EventArgs e)
51         {
52             webBrowser1.GoHome();
53             textBox1.Text = webBrowser1.Url == null ? "" : webBrowser1.Url.ToString();
54         }
55 
56         private void buttonRefresh_Click(object sender, EventArgs e)
57         {
58             webBrowser1.Refresh();
59         }
60 
61         private void buttonSubmit_Click(object sender, EventArgs e)
62         {
63             webBrowser1.Navigate(textBox1.Text);
64         }
65 
66         private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
67         {
68             buttonStop.Enabled = true;
69         }
70 
71         private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
72         {
73             textBox2.Text = webBrowser1.DocumentText;
74             buttonStop.Enabled = false;
75             if (webBrowser1.CanGoBack)
76                 buttonBack.Enabled = true;
77             else
78                 buttonBack.Enabled = false;
79 
80             if (webBrowser1.CanGoForward)
81                 buttonForward.Enabled = true;
82             else
83                 buttonForward.Enabled = false;
84         }
85 
86         private void button1_Click(object sender, EventArgs e)
87         {
88             webBrowser1.Print();
89         }
90     }
View Code

Designer 代码

  1  partial class Form1
  2     {
  3         /// <summary>
  4         /// Required designer variable.
  5         /// </summary>
  6         private System.ComponentModel.IContainer components = null;
  7 
  8         /// <summary>
  9         /// Clean up any resources being used.
 10         /// </summary>
 11         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
 12         protected override void Dispose(bool disposing)
 13         {
 14             if (disposing && (components != null))
 15             {
 16                 components.Dispose();
 17             }
 18             base.Dispose(disposing);
 19         }
 20 
 21         #region Windows Form Designer generated code
 22 
 23         /// <summary>
 24         /// Required method for Designer support - do not modify
 25         /// the contents of this method with the code editor.
 26         /// </summary>
 27         private void InitializeComponent()
 28         {
 29             this.textBox1 = new System.Windows.Forms.TextBox();
 30             this.buttonSubmit = new System.Windows.Forms.Button();
 31             this.buttonRefresh = new System.Windows.Forms.Button();
 32             this.buttonHome = new System.Windows.Forms.Button();
 33             this.buttonStop = new System.Windows.Forms.Button();
 34             this.buttonForward = new System.Windows.Forms.Button();
 35             this.buttonBack = new System.Windows.Forms.Button();
 36             this.webBrowser1 = new System.Windows.Forms.WebBrowser();
 37             this.textBox2 = new System.Windows.Forms.TextBox();
 38             this.button1 = new System.Windows.Forms.Button();
 39             this.SuspendLayout();
 40             // 
 41             // textBox1
 42             // 
 43             this.textBox1.Location = new System.Drawing.Point(14, 43);
 44             this.textBox1.Name = "textBox1";
 45             this.textBox1.Size = new System.Drawing.Size(480, 21);
 46             this.textBox1.TabIndex = 14;
 47             this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
 48             // 
 49             // buttonSubmit
 50             // 
 51             this.buttonSubmit.Location = new System.Drawing.Point(513, 17);
 52             this.buttonSubmit.Name = "buttonSubmit";
 53             this.buttonSubmit.Size = new System.Drawing.Size(75, 45);
 54             this.buttonSubmit.TabIndex = 13;
 55             this.buttonSubmit.Text = "Submit";
 56             this.buttonSubmit.UseVisualStyleBackColor = true;
 57             this.buttonSubmit.Click += new System.EventHandler(this.buttonSubmit_Click);
 58             // 
 59             // buttonRefresh
 60             // 
 61             this.buttonRefresh.Location = new System.Drawing.Point(338, 17);
 62             this.buttonRefresh.Name = "buttonRefresh";
 63             this.buttonRefresh.Size = new System.Drawing.Size(75, 21);
 64             this.buttonRefresh.TabIndex = 12;
 65             this.buttonRefresh.Text = "Refresh";
 66             this.buttonRefresh.UseVisualStyleBackColor = true;
 67             this.buttonRefresh.Click += new System.EventHandler(this.buttonRefresh_Click);
 68             // 
 69             // buttonHome
 70             // 
 71             this.buttonHome.Location = new System.Drawing.Point(257, 17);
 72             this.buttonHome.Name = "buttonHome";
 73             this.buttonHome.Size = new System.Drawing.Size(75, 21);
 74             this.buttonHome.TabIndex = 11;
 75             this.buttonHome.Text = "Home";
 76             this.buttonHome.UseVisualStyleBackColor = true;
 77             this.buttonHome.Click += new System.EventHandler(this.buttonHome_Click);
 78             // 
 79             // buttonStop
 80             // 
 81             this.buttonStop.Location = new System.Drawing.Point(176, 17);
 82             this.buttonStop.Name = "buttonStop";
 83             this.buttonStop.Size = new System.Drawing.Size(75, 21);
 84             this.buttonStop.TabIndex = 10;
 85             this.buttonStop.Text = "Stop";
 86             this.buttonStop.UseVisualStyleBackColor = true;
 87             this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
 88             // 
 89             // buttonForward
 90             // 
 91             this.buttonForward.Location = new System.Drawing.Point(95, 17);
 92             this.buttonForward.Name = "buttonForward";
 93             this.buttonForward.Size = new System.Drawing.Size(75, 21);
 94             this.buttonForward.TabIndex = 9;
 95             this.buttonForward.Text = "Forward";
 96             this.buttonForward.UseVisualStyleBackColor = true;
 97             this.buttonForward.Click += new System.EventHandler(this.buttonForward_Click);
 98             // 
 99             // buttonBack
100             // 
101             this.buttonBack.Location = new System.Drawing.Point(14, 17);
102             this.buttonBack.Name = "buttonBack";
103             this.buttonBack.Size = new System.Drawing.Size(75, 21);
104             this.buttonBack.TabIndex = 8;
105             this.buttonBack.Text = "Back";
106             this.buttonBack.UseVisualStyleBackColor = true;
107             this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
108             // 
109             // webBrowser1
110             // 
111             this.webBrowser1.Location = new System.Drawing.Point(0, 67);
112             this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 18);
113             this.webBrowser1.Name = "webBrowser1";
114             this.webBrowser1.Size = new System.Drawing.Size(815, 312);
115             this.webBrowser1.TabIndex = 15;
116             this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
117             this.webBrowser1.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.webBrowser1_Navigated);
118             this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);
119             // 
120             // textBox2
121             // 
122             this.textBox2.Location = new System.Drawing.Point(0, 386);
123             this.textBox2.Multiline = true;
124             this.textBox2.Name = "textBox2";
125             this.textBox2.Size = new System.Drawing.Size(807, 223);
126             this.textBox2.TabIndex = 16;
127             // 
128             // button1
129             // 
130             this.button1.Location = new System.Drawing.Point(419, 17);
131             this.button1.Name = "button1";
132             this.button1.Size = new System.Drawing.Size(75, 21);
133             this.button1.TabIndex = 12;
134             this.button1.Text = "Print";
135             this.button1.UseVisualStyleBackColor = true;
136             this.button1.Click += new System.EventHandler(this.button1_Click);
137             // 
138             // Form1
139             // 
140             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
141             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
142             this.ClientSize = new System.Drawing.Size(819, 619);
143             this.Controls.Add(this.textBox2);
144             this.Controls.Add(this.webBrowser1);
145             this.Controls.Add(this.textBox1);
146             this.Controls.Add(this.buttonSubmit);
147             this.Controls.Add(this.button1);
148             this.Controls.Add(this.buttonRefresh);
149             this.Controls.Add(this.buttonHome);
150             this.Controls.Add(this.buttonStop);
151             this.Controls.Add(this.buttonForward);
152             this.Controls.Add(this.buttonBack);
153             this.Name = "Form1";
154             this.Text = "Form1";
155             this.Load += new System.EventHandler(this.Form1_Load);
156             this.ResumeLayout(false);
157             this.PerformLayout();
158 
159         }
160 
161         #endregion
162 
163         private System.Windows.Forms.TextBox textBox1;
164         private System.Windows.Forms.Button buttonSubmit;
165         private System.Windows.Forms.Button buttonRefresh;
166         private System.Windows.Forms.Button buttonHome;
167         private System.Windows.Forms.Button buttonStop;
168         private System.Windows.Forms.Button buttonForward;
169         private System.Windows.Forms.Button buttonBack;
170         private System.Windows.Forms.WebBrowser webBrowser1;
171         private System.Windows.Forms.TextBox textBox2;
172         private System.Windows.Forms.Button button1;
173     }
174 }
View Code

 

通过流 读取页面源代码

 1            WebClient client = new WebClient();
 2            Stream str= client.OpenRead("http://www.baidu.com");
 3            StreamReader sr = new StreamReader(str);
 4             
 5            string line;
 6            while ((line=sr.ReadLine())!=null)
 7            {
 8                listBox1.Items.Add(line);
 9            }
10            str.Close(); 

 

打印方法

1       webBrowser1.Print();

 

posted @ 2016-11-29 15:34  指间的徘徊  阅读(1131)  评论(0编辑  收藏  举报