您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何用C#做瀏覽器源程序”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何用C#做瀏覽器源程序”吧!
C#做瀏覽器源程序
了解C#源程序代碼,就可以比較容易編寫一個屬于自己的瀏覽器了,下面是用Visual C#做瀏覽器源程序代碼,他具備了IE瀏覽器的一些常用的功能。
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using AxSHDocVw ;
public class Form1 : Form
{
private ToolBar toolBar1 ;
private ToolBarButton tb1 ;
private ToolBarButton tb2 ;
private ToolBarButton tb3 ;
private ToolBarButton tb4 ;
private ToolBarButton tb5 ;
private Label label1 ;
private TextBox textBox1 ;
private Button button1 ;
private AxWebBrowser axWebBrowser1 ;
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
InitializeComponent ( ) ;
}
//清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
//初始化窗體中的各個組件
private void InitializeComponent ( )
{
tb1 = new ToolBarButton ( ) ;
tb2 = new ToolBarButton ( ) ;
tb3 = new ToolBarButton ( ) ;
toolBar1 = new ToolBar ( ) ;
tb4 = new ToolBarButton ( ) ;
tb5 = new ToolBarButton ( ) ;
button1 = new Button ( ) ;
textBox1 = new TextBox ( ) ;
axWebBrowser1 = new AxWebBrowser ( ) ;
label1 = new Label ( ) ;
( ( System.ComponentModel.ISupportInitialize )
( this.axWebBrowser1 ) ).BeginInit ( ) ;this.SuspendLayout ( ) ;
tb1.Text = "后退" ;
tb2.Text = "前進" ;
tb3.Text = "停止" ;
tb4.Text = "刷新" ;
tb5.Text = "主頁" ;
toolBar1.Appearance = ToolBarAppearance.Flat ;
toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
//在工具欄中加入按鈕
toolBar1.Buttons.Add ( tb1 ) ;
toolBar1.Buttons.Add ( tb2 ) ;
toolBar1.Buttons.Add ( tb3 ) ;
toolBar1.Buttons.Add ( tb4 ) ;
toolBar1.Buttons.Add ( tb5 ) ;
toolBar1.DropDownArrows = true ;
toolBar1.Name = "toolBar1" ;
toolBar1.ShowToolTips = true ;
toolBar1.Size = new System.Drawing.Size ( 612 , 39 ) ;
toolBar1.TabIndex = 0 ;
toolBar1.ButtonClick += new ToolBarButtonClickEventHandler ( toolBar1_ButtonClick ) ;
//定位“轉到”按鈕組件與窗體的上、右邊框保持一致
button1.Anchor = ( AnchorStyles.Top | AnchorStyles.Right ) ;
button1.DialogResult = DialogResult.OK ;
button1.Location = new System.Drawing.Point ( 544 , 45 ) ;
button1.Name = "button1" ;
button1.Size = new System.Drawing.Size ( 40 , 23 ) ;
button1.TabIndex = 3 ;
button1.Text = "轉到" ;
button1.Click += new System.EventHandler ( button1_Click ) ;
//定位地址文本框組件與窗體的上、左、右邊框保持一致
textBox1.Anchor = ( ( AnchorStyles.Top | AnchorStyles.Left )
| AnchorStyles.Right ) ;
textBox1.Location = new System.Drawing.Point ( 64 , 47 ) ;
textBox1.Name = "textBox1" ;
textBox1.Size = new System.Drawing.Size ( 464 , 21 ) ;
textBox1.TabIndex = 2 ;
textBox1.Text = "" ;
//定位瀏覽器組件與窗體的上、下、左、右邊框保持一致
axWebBrowser1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom )
| AnchorStyles.Left )
| AnchorStyles.Right ) ;
axWebBrowser1.Enabled = true ;
axWebBrowser1.Location = new System.Drawing.Point ( 0 , 72 ) ;
axWebBrowser1.Size = new System.Drawing.Size ( 608 , 358 ) ;
axWebBrowser1.TabIndex = 4 ;
label1.Location = new System.Drawing.Point ( 16 , 48 ) ;
label1.Name = "label1" ;
label1.Size = new System.Drawing.Size ( 48 , 16 ) ;
label1.TabIndex = 1 ;
label1.Text = "地址:" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 612 , 433 ) ;
this.Controls.Add ( axWebBrowser1 ) ;
this.Controls.Add ( button1 ) ;
this.Controls.Add ( textBox1 ) ;
this.Controls.Add ( label1 ) ;
this.Controls.Add ( toolBar1 ) ;
this.FormBorderStyle = FormBorderStyle.FixedSingle ;
this.Name = "Form1" ;
this.Text = "visual C#做瀏覽器" ;
( ( System.ComponentModel.ISupportInitialize )
( this.axWebBrowser1 ) ).EndInit ( ) ;this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
//實現瀏覽器主要功能
private void toolBar1_ButtonClick
( object sender , ToolBarButtonClickEventArgs e ){
//瀏覽器中的“后退”
if ( e.Button == tb1 )
{
axWebBrowser1.GoBack ( ) ;
}
//瀏覽器中的“前進”
if ( e.Button == tb2 )
{
axWebBrowser1.GoForward ( ) ;
}
//瀏覽器中的“停止”
if ( e.Button == tb3 )
{
axWebBrowser1.Stop ( ) ;
}
//瀏覽器中的“刷新”
if ( e.Button == tb4 )
{
axWebBrowser1.Refresh ( ) ;
}
//瀏覽器中的“主頁”
if ( e.Button == tb5 )
{
axWebBrowser1.GoHome ( ) ;
}
}
//瀏覽指定的Web地址
private void button1_Click ( object sender , System.EventArgs e )
{
System.Object nullObject = 0 ;
string str = "" ;
System.Object nullObjStr = str ;
Cursor.Current = Cursors.WaitCursor ;
axWebBrowser1.Navigate ( textBox1.Text , ref nullObject ,
ref nullObjStr , ref nullObjStr , ref nullObjStr ) ;Cursor.Current = Cursors.Default ;
}
}
編譯源程序和編譯后的執行程序的運行界面在經過如下命令編譯后,就可以得到可以自己的瀏覽器了
感謝各位的閱讀,以上就是“如何用C#做瀏覽器源程序”的內容了,經過本文的學習后,相信大家對如何用C#做瀏覽器源程序這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。