0
点赞
收藏
分享

微信扫一扫

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

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


举报

相关推荐

0 条评论