这篇文章主要讲解了“如何用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#做浏览器源程序这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!