您当前的位置: 首页 >  c#

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#编程-139:制作自己的浏览器

彭世瑜 发布时间:2017-08-21 23:07:20 ,浏览量:1

C#编程-139:制作自己的浏览器
  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. using System.Net;
  10. using System.IO;
  11. using System.Runtime.InteropServices;
  12.  
  13. namespace WebBrowserTest
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         //记事本需要的变量
  22.         [DllImport("User32.dll")]
  23.         public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string Iparam);
  24.         [DllImport("User32.dll")]
  25.         public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  26.         public const uint WM_SETTEXT = 0X00C;//0x00F5
  27.  
  28.         private void btnOpen_Click(object sender, EventArgs e)
  29.         {
  30.             OpenPage();
  31.         }
  32.         //打开网页
  33.         void OpenPage()
  34.         {
  35.             if (txtAddress.Text.Length > 0)
  36.             {
  37.                 webBrowser1.Navigate(txtAddress.Text.Trim(), false);
  38.             }
  39.             else
  40.             {
  41.                 MessageBox.Show("请输入网址");
  42.             }
  43.         }
  44.  
  45.         private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
  46.         {
  47.             e.Cancel = true;
  48.             if (webBrowser1.Document.ActiveElement != null)
  49.             {
  50.                 string address = webBrowser1.Document.ActiveElement.GetAttribute("href");
  51.                 webBrowser1.Navigate(address);
  52.                 txtAddress.Text = address;
  53.             }
  54.         }
  55.  
  56.         private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  57.         {
  58.             if (webBrowser1.CanGoBack)
  59.             {
  60.                 //tsWebBrowser.Items[0].Enabled = true;
  61.                 tsbBack.Enabled = true;
  62.             }
  63.             else
  64.             {
  65.                 tsbBack.Enabled = false;
  66.             }
  67.             if (webBrowser1.CanGoForward)
  68.             {
  69.                 tsWebBrowser.Items[1].Enabled = true;
  70.             }
  71.             else
  72.             {
  73.                 tsWebBrowser.Items[1].Enabled = false;
  74.             }
  75.         }
  76.  
  77.         private void tsWebBrowser_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  78.         {
  79.             try
  80.             {
  81.                 if (e.ClickedItem.Name == "tsbBack")
  82.                 {
  83.                     webBrowser1.GoBack();
  84.                 }
  85.                 if (e.ClickedItem.Name == "tsbForward")
  86.                 {
  87.                     webBrowser1.GoForward();
  88.                 }
  89.                 if (e.ClickedItem.Name == "tsbRefresh")
  90.                 {
  91.                     webBrowser1.Refresh();
  92.                 }
  93.                 if (e.ClickedItem.Name == "tsbHome")
  94.                 {
  95.                     webBrowser1.GoHome();
  96.                 }
  97.                 if (e.ClickedItem.Name == "tsbStop")
  98.                 {
  99.                     webBrowser1.Stop();
  100.                 }
  101.                 if (e.ClickedItem.Name == "tsbExit")
  102.                 {
  103.                     if (MessageBox.Show("确认退出?", "退出对话框", MessageBoxButtons.OKCancel) == DialogResult.OK)
  104.                     {
  105.                         Application.Exit();
  106.                     }
  107.                 }
  108.                 if (e.ClickedItem.Name == "tsbViewSource")
  109.                 {
  110.   WebRequest wrq = WebRequest.Create(txtAddress.Text);
  111.                     WebResponse wrs = wrq.GetResponse();
  112.                     StreamReader sr = new StreamReader(wrs.GetResponseStream(), Encoding.Default);
  113.                     string page = "";
  114.                     string code = null;
  115.                     while ((code = sr.ReadLine()) != null)
  116.                     {
  117.                         page += code;
  118.                     }
  119.  
  120.                     System.Diagnostics.Process pro = new System.Diagnostics.Process();
  121.                     pro.StartInfo.UseShellExecute = false;
  122.                     pro.StartInfo.FileName = "notepad.exe";//获取要启动的记事本
  123.  
  124.                     //不适用操作系统外壳启动程序进程
  125.                     pro.StartInfo.RedirectStandardInput = true;//读取
  126.                     pro.StartInfo.RedirectStandardOutput = true;//将应用程序写入到流中
  127.                     pro.Start();//启动
  128.                     if (pro != null)
  129.                     {
  130.                         //调用API,传递数据
  131.                         while (pro.MainWindowHandle == IntPtr.Zero)
  132.                         {
  133.                             pro.Refresh();
  134.                         }
  135.                         IntPtr vHandle = FindWindowEx(pro.MainWindowHandle, IntPtr.Zero, "Edit", null);
  136.                         //传递数据给记事本
  137.                         SendMessage(vHandle, WM_SETTEXT, 0, page);
  138.                     }
  139.  
  140.                 }
  141.             }
  142.             catch (Exception ex)
  143.             {
  144.  
  145.                 MessageBox.Show(ex.Message);
  146.             }
  147.         }
  148.         private void txtAddress_KeyPress(object sender, KeyPressEventArgs e)
  149.         {
  150.             char key = e.KeyChar;
  151.             if (key == 13)//回车
  152.             {
  153.                 OpenPage();
  154.             }
  155.         }
  156.  
  157.         private void Form1_Load(object sender, EventArgs e)
  158.         {
  159.             txtAddress.Text = @"http://www.baidu.com";
  160.             OpenPage();
  161.         }
  162.     }
  163. }
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.1213s