c#操作axwebbrowser的一个简单示例


最近在做一个程序中要求在WinForm中插一个浏览器的控件,在网上一找,找到了AxWebBrowser这个控件,它是一个COM的控件,所以先要往工具箱中添加这个控件,使得它能像其他可视化控件一样变得容易操作。添加完之后,你会在工具箱的“常规”箱中看到一个新增的控件,我们就能开始往WinForm中添加了! 
一看这个控件的方法和属性还是很多的,都是作为一个浏览器所应该有的,如GoBack(),GoFoward(),Stop(),Refresh(),Home()等等,可以看到在一般浏览器的顶端会有这些按钮,我们只要再加个ToolBar,加上相应的按钮,写好程序,就可以实现这些功能了。 

网上其实已经有很多用这个控件实现一个网页浏览器的程序,我找了一个先贴在这里: 

××××××××××××××××××××××××××××××××××××××××××××××××××××××××× 

通过转换后组件来实现浏览器的一些基本功能: 
浏览器的主要功能就是能够到指定的地址浏览信息,当然在具体的浏览中还有一些基本的功能,譬如:“前进”、“后退”、“停止”、“刷新”、“主页”等,这些功能都可以通过“AxWebBrowser”组件来实现。下面就来具体介绍: 
< I > .浏览指定的地址: 
在程序中,网址是填写在组件“textbox1”中的,“浏览指定地址”功能是通过程序的按钮“转到”来实现的。下面是按钮“转到”按动后的程序代m: 
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 ; 

< II > .浏览器的“前进”、“后退”、“停止”、“刷新”、“主页”功能: 
在“AxWebBrowser”组件中对这些功能都有一个具体的方法来与之对应,具体如下面代m: 
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 ( ) ; 



< III > .当然掌握了上面的知识,你就可以用Visual C#做出一个基本的浏览器了,但下面这些也是不可缺少的,因为下面这些代m将使得你做的浏览器更专业。下面代m的作用是使得浏览界面随着窗体的变化而变化,按钮和文本框也要随着窗体的变化而变化。 
button1.Anchor = ( AnchorStyles.Top | AnchorStyles.Right ) ; 
//定位“转到”按钮组件与窗体的上、右边框保持一致 
textBox1.Anchor = ( ( AnchorStyles.Top | AnchorStyles.Left ) 
| AnchorStyles.Right ) ; 
//定位地址文本框组件与窗体的上、左、右边框保持一致 
axWebBrowser1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom ) 
| AnchorStyles.Left ) 
| AnchorStyles.Right ) ; 
//定位浏览器组件与窗体的上、下、左、右边框保持一致 
源程序代m(brower.cs) 
了解有了上面的这些,就可以比较容易编写一个属于自己的浏览器了,下面是用Visual C#做的浏览器源程序代m,他具备了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 ; 


总结 
至此一个功能相对完备的“浏览器”就算完成了,其实用Visual C#做“浏览器”的过程,也就是Visual C#中使用COM组件的过程。掌握了COM组件在Visual C#使用方法,就可以利用Visual C#编写出功能更强大,适应性更强的软件来,但编写的过程又十分的简单

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值