Xây dựng Web Browser với C#

VN Web Browser

I.Introduction

Trong bài này mình xin chia sẻ cách viết một Trình Duyệt Web đơn giản với một số tính năng như Trình Duyệt Web, Next, Homepage…..

II.Creating Project And Coding

Các bạn Mở Visual Studio 2010 lên và tạo mới Window Form Appication

image

Tiếp theo các bạn tạo giao diện cho trình duyệt

image

Ở giao diện trên chúng ta dùng một số Control chính như ToolStrip và Add các ToolStrop Control vào(ToolStripButton. ToolStripLabel……), một Control không thể thiếu đó chính là Webbrowser .

Tiếp theo các bạn Double Click vào các Control để tạo sự kiện, Hoặc có thể tạo sự kiện bằng code Behide:

   1: public Form1()

   2: {

   3:     InitializeComponent();

   4:     this.Load += new EventHandler(Form1_Load);

   5: }

   6:  

   7: void Form1_Load(object sender, EventArgs e)

   8: {

   9:     btnNext.Click += new EventHandler(btnNext_Click);

  10:     btnPre.Click += new EventHandler(btnPre_Click);

  11:     btnPrint.Click += new EventHandler(btnPrint_Click);

  12:     btnRefesh.Click += new EventHandler(btnRefesh_Click);

  13:     btnHome.Click += new EventHandler(btnHome_Click);

  14:     btnGo.Click += new EventHandler(btnGo_Click);

  15:     txtAddress.KeyDown += new KeyEventHandler(txtAddress_KeyDown);

  16:     txtAddress.Text = "http://phamnguyen.info";

  17: }

Bây giờ chúng ta cần làm một Hành Vi giúp cho Webbrowser làm việc, các bạn tạo một hàm tên là OpenUrlInBrowser, hàm này có tác dụng giúp chúng ta có thể trình duyệt một trang Web với input là địa chỉ trang web

   1: private void OpenUrlInBrowser(string Address)

   2: {

   3:     if (!Address.StartsWith("http://") && !Address.StartsWith("https://"))

   4:     {

   5:         Address = "http://" + Address;

   6:     }

   7:     try

   8:     {

   9:         webBrowser.Navigate(new Uri(Address));

  10:     }

  11:     catch (System.UriFormatException)

  12:     {

  13:  

  14:         return;

  15:     }

  16: }

Kế tiếp chúng ta xử lý sự kiện cho từng Button trong ứng dụng.

Press Enter để trình duyệt Web

   1: void txtAddress_KeyDown(object sender, KeyEventArgs e)

   2: {

   3:     if (e.KeyCode == Keys.Enter)

   4:     {

   5:         OpenUrlInBrowser(txtAddress.Text);

   6:     }

   7: }

Go To Web

   1: void btnGo_Click(object sender, EventArgs e)

   2: {

   3:     if (!String.IsNullOrEmpty(txtAddress.Text))

   4:     {

   5:         OpenUrlInBrowser(txtAddress.Text);

   6:     }

   7:     else OpenUrlInBrowser("phamnguyen.info");

   8: }

Ngoài ra còn những tính năng khác, các bạn có thêm tham khảo ở đoạn Code sau:

Full Source Code:

   1: using System;

   2: using System.Collections.Generic;

   3: using System.ComponentModel;

   4: using System.Data;

   5: using System.Drawing;

   6: using System.Linq;

   7: using System.Text;

   8: using System.Windows.Forms;

   9:  

  10: namespace WebBrowser

  11: {

  12:     public partial class Form1 : Form

  13:     {

  14:         public Form1()

  15:         {

  16:             InitializeComponent();

  17:             this.Load += new EventHandler(Form1_Load);

  18:         }

  19:  

  20:         void Form1_Load(object sender, EventArgs e)

  21:         {

  22:             btnNext.Click += new EventHandler(btnNext_Click);

  23:             btnPre.Click += new EventHandler(btnPre_Click);

  24:             btnPrint.Click += new EventHandler(btnPrint_Click);

  25:             btnRefesh.Click += new EventHandler(btnRefesh_Click);

  26:             btnHome.Click += new EventHandler(btnHome_Click);

  27:             btnGo.Click += new EventHandler(btnGo_Click);

  28:             txtAddress.KeyDown += new KeyEventHandler(txtAddress_KeyDown);

  29:             txtAddress.Text = "http://phamnguyen.info";

  30:         }

  31:  

  32:         void txtAddress_KeyDown(object sender, KeyEventArgs e)

  33:         {

  34:             if (e.KeyCode == Keys.Enter)

  35:             {

  36:                 OpenUrlInBrowser(txtAddress.Text);

  37:             }

  38:         }

  39:  

  40:         private void OpenUrlInBrowser(string Address)

  41:         {

  42:             if (!Address.StartsWith("http://") && !Address.StartsWith("https://"))

  43:             {

  44:                 Address = "http://" + Address;

  45:             }

  46:             try

  47:             {

  48:                 webBrowser.Navigate(new Uri(Address));

  49:             }

  50:             catch (System.UriFormatException)

  51:             {

  52:  

  53:                 return;

  54:             }

  55:         }

  56:  

  57:         void btnGo_Click(object sender, EventArgs e)

  58:         {

  59:             if (!String.IsNullOrEmpty(txtAddress.Text))

  60:             {

  61:                 OpenUrlInBrowser(txtAddress.Text);

  62:             }

  63:             else OpenUrlInBrowser("phamnguyen.info");

  64:         }

  65:  

  66:         void btnHome_Click(object sender, EventArgs e)

  67:         {

  68:             webBrowser.GoHome();

  69:             

  70:         }

  71:  

  72:         void btnRefesh_Click(object sender, EventArgs e)

  73:         {

  74:             webBrowser.Refresh();

  75:         }

  76:  

  77:         void btnPrint_Click(object sender, EventArgs e)

  78:         {

  79:             webBrowser.ShowPrintPreviewDialog();

  80:         }

  81:  

  82:         void btnPre_Click(object sender, EventArgs e)

  83:         {

  84:             if (webBrowser.CanGoBack)

  85:                 webBrowser.GoBack();

  86:         }

  87:  

  88:         void btnNext_Click(object sender, EventArgs e)

  89:         {

  90:             if (webBrowser.CanGoForward)

  91:                 webBrowser.GoForward();

  92:         }

  93:     }

  94: }

Run Application (Ctrl + F5) để xem kết quả:

image

/*Life Run On Code*/

Source Code Download here !

3 thoughts on “Xây dựng Web Browser với C#

  1. theo hướng dẫn của bạn thì mình đã biên dịch thành công và duyệt được web, nhưng mình muốn làm một trình duyệt với địa chỉ cố định cho mình gán vào, chẳng hạn ý tưởng của mình là trong me.zing.vn chơi game nông trại vui vẻ nó có quảng cáo phía dưới mình không thích vì nó chiếm nhiều không gian và load các file không cần thiết nên mình chỉ muốn mỗi lần mở trình duyệt lên thì vô thẳng nông trại vui vẻ luôn bằng cách mình chép đường dẫn file flash vào duyệt trên ff hay ie đều được nhưng chép vào trình duyệt mới biên dịch thì không chạy được mong bạn giúp đở (Link demo: “http://zf-static.apps.zing.vn/imgcache/ZingFarm83.swf?v=2.15.0&xmldir=http://zf-static.apps.zing.vn/Web/ini51.xml?v=1&flashDir=http://zf-static.apps.zing.vn/imgcache/”)

    Gửi qua mail cho mình khi bạn trả lời nha, mail:nguyentrunghieuct@gmail.com

    Like

    1. chào bạn. do Project này lúc mình cài may lại quên Backup nên để mất rùi. nhưng mình nghĩ nếu bạn muốn chỉ để chơi game flash và khi khởi động lên là vào thẳng links game của bạn, thì bạn nên đặt đường dẫn mặc định trong Source để khi khởi động lên thì nó vào thẳng lun !

      Like

  2. Chào bạn!
    từ web browser này bạn có thể hướng dẫn mình xây dựng thêm 2 chức năng nữa là

    delay a giây sẽ gửi 1 sự kiện click chuột vào tọa độ (x,y) của nội dung trang web.
    delay b giây sẽ gửi 1 sự kiện là 1 phím bấm bất kỳ từ bàn phím đến nội dung trang web.

    Like

Leave a reply to Pham Nguyen Cancel reply