国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Visual C# 打造 "瀏覽器"

2024-07-21 02:24:21
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • visual c# 打造 “瀏覽器”


    --------------------------------------------------------------------------------


    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#編寫出功能更強大,適應性更強的軟件來,但編寫的過程又十分的簡單。


    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 廊坊市| 噶尔县| 朝阳区| 三台县| 蚌埠市| 宁阳县| 利辛县| 兴文县| 正蓝旗| 涟源市| 平远县| 海丰县| 山西省| 双峰县| 宜昌市| 桓台县| 临汾市| 泰来县| 札达县| 新乡市| 南和县| 屏边| 张掖市| 自治县| 三台县| 峨山| 涞源县| 农安县| 安徽省| 固镇县| 吉木乃县| 永清县| 东港市| 民县| 鹤峰县| 阿拉善盟| 哈巴河县| 卢氏县| 白朗县| 礼泉县| 肇源县|