visual c#是微软推出的新一代程序开发语言,visual 
                  c#实现许多功能是通过调用.net框架为其中所有.net程序开发语言提供的一个公用的软件包——.net framework 
                  sdk。在这个软件包中提供了大量并且十分丰富的类库,可以说,没有这个软件开发包,visual 
                  c#就寸步难行,无法编写哪怕一个功能十分的程序。但这样也会出现一个问题,如果在.net framework 
                  sdk软件包中没有涉及到的功能,而在其他的第三方的com组件中却提供了,那么这些组件是否可以被visual 
                  c#使用。答案是:直接使用是不可以的,但这些com组件经过一定转换后就可以。这种转换就是非受管代码(unmanaged 
                  code)到受管代码(managed code)的转换。因为这些com组件一般都是非受管代码(unmanaged 
                  code),而编译visual c#文件时候要使用的类库却只能为受管代码(managed 
                  code),这就是说要在visual 
                  c#中使用那些非受管代码组件,就必须把这些非受管代码组件转换成受管代码组件。在.net框架中专门提供了一个程序“aximp.exe”来实现由com组件到winform组件的转换。那么这个文件在哪里?假设你安装.net 
                  framework sdk在“c”盘,那么在“c:/program 
                  files/microsoft.net/frameworksdk/bin”目录中就可以找到这个文件。如果你安装.net 
                  framework sdk在其他目录或者磁盘,依照上述的目录顺序就可以找到这个文件了。 
                  下面用visual c#来做一个“浏览器”,看看在visual c#是如何使用com组件的。 
                  一.本文程序设计和运行的软件环境 
                  (1).微软公司视窗2000服务器版 
                  (2)..net framework sdk beta 2 
                  二.程序设计的思路以及关键步骤的解决方法 
                  (1).把转换com组件为winform组件: 
                  其实实现这种转换十分的简单,我们知道微软web浏览器com组件名称为“shdocvw.dll”,由于我们使用的是视窗2000,所以这个文件是存放在“c:/winnt/system32”目录中,如果你使用的是视窗98或者是视窗me,那么此组件存放的位置是“c:/windows/system”。“aximp.exe”文件后面有许多的参数,你可以通过“aximp 
                  /?”来了解,在本文中只使用下列简单的命令就可成功转换: 
                  aximp c:/winnt/system32/shdocvw.dll 
                  运行上述命令后就可以实现转换,并在当前目录中产生“shdocvw.dll”和“axshdocvw.dll”二个文件。具体如下图: 
                  图01:转换com组件为winform组件 
                  (2).在程序中使用转换后组件: 
                  在“axshdocvw.dll”中定义了命名空间“axshdocvw”,在此命名空间中定义了一个“axwebbrowser”组件,这个组件中有若干个方法和属性,visual 
                  c#就是通过这些方法和属性来实现浏览器的一些基本功能的。使用此浏览器组件和使用其他的winform组件的过程是一样的,首先要导入命名空间,然后在程序中继承此命名空间中定义的浏览器组件,最后设定这个继承后的组件的属性和方法。具体如下: 
                  < i > .导入命名空间,具体代码如下: 
                  using axshdocvw ; 
                  < ii> . 继承此命名空间中定义的浏览器组件,具体代码如下: 
                  private axwebbrowser axwebbrowser1 ; 
                  (3).通过转换后组件来实现浏览器的一些基本功能: 
                  浏览器的主要功能就是能够到指定的地址浏览信息,当然在具体的浏览中还有一些基本的功能,譬如:“前进”、“后退”、“停止”、“刷新”、“主页”等,这些功能都可以通过“axwebbrowser”组件来实现。下面就来具体介绍: 
                  < i > .浏览指定的地址: 
                  在程序中,网址是填写在组件“textbox1”中的,“浏览指定地址”功能是通过程序的按钮“转到”来实现的。下面是按钮“转到”按动后的程序代码: 
                  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”组件中对这些功能都有一个具体的方法来与之对应,具体如下面代码: 
                  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#做出一个基本的浏览器了,但下面这些也是不可缺少的,因为下面这些代码将使得你做的浏览器更专业。下面代码的作用是使得浏览界面随着窗体的变化而变化,按钮和文本框也要随着窗体的变化而变化。 
                  button1.anchor = ( anchorstyles.top | anchorstyles.right ) ; 
                  //定位“转到”按钮组件与窗体的上、右边框保持一致 
                  textbox1.anchor = ( ( anchorstyles.top | anchorstyles.left ) 
                  | anchorstyles.right ) ; 
                  //定位地址文本框组件与窗体的上、左、右边框保持一致 
                  axwebbrowser1.anchor = ( ( ( anchorstyles.top | 
                  anchorstyles.bottom ) 
                  | anchorstyles.left ) 
                  | anchorstyles.right ) ; 
                  //定位浏览器组件与窗体的上、下、左、右边框保持一致 
                  三.源程序代码(brower.cs) 
                  了解有了上面的这些,就可以比较容易编写一个属于自己的浏览器了,下面是用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 ; 
                  } 
                  } 
                  四.编译源程序和编译后的执行程序的运行界面 
                  在经过如下命令编译后,就可以得到可以自己的浏览器了 
                  csc /t:winexe /r:axshdocvw.dll /r:shdocvw.dll /r:system.dll 
                  /r:system.windows.forms.dll /r:system.drawing.dll brower.cs 
                  图02:用visual c#做的“浏览器”的运行界面 
                  五.总结 
                  至此一个功能相对完备的“浏览器”就算完成了,其实用visual c#做“浏览器”的过程,也就是visual 
                  c#中使用com组件的过程。掌握了com组件在visual c#使用方法,就可以利用visual 
                  c#编写出功能更强大,适应性更强的软件来,但编写的过程又十分的简单。 
新闻热点
疑难解答