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

简介: C#编程-139:制作自己的浏览器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
namespace WebBrowserTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //记事本需要的变量
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string Iparam);
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        public const uint WM_SETTEXT = 0X00C;//0x00F5
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenPage();
        }
        //打开网页
        void OpenPage()
        {
            if (txtAddress.Text.Length > 0)
            {
                webBrowser1.Navigate(txtAddress.Text.Trim(), false);
            }
            else
            {
                MessageBox.Show("请输入网址");
            }
        }
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            if (webBrowser1.Document.ActiveElement != null)
            {
                string address = webBrowser1.Document.ActiveElement.GetAttribute("href");
                webBrowser1.Navigate(address);
                txtAddress.Text = address;
            }
        }
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            if (webBrowser1.CanGoBack)
            {
                //tsWebBrowser.Items[0].Enabled = true;
                tsbBack.Enabled = true;
            }
            else
            {
                tsbBack.Enabled = false;
            }
            if (webBrowser1.CanGoForward)
            {
                tsWebBrowser.Items[1].Enabled = true;
            }
            else
            {
                tsWebBrowser.Items[1].Enabled = false;
            }
        }
        private void tsWebBrowser_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                if (e.ClickedItem.Name == "tsbBack")
                {
                    webBrowser1.GoBack();
                }
                if (e.ClickedItem.Name == "tsbForward")
                {
                    webBrowser1.GoForward();
                }
                if (e.ClickedItem.Name == "tsbRefresh")
                {
                    webBrowser1.Refresh();
                }
                if (e.ClickedItem.Name == "tsbHome")
                {
                    webBrowser1.GoHome();
                }
                if (e.ClickedItem.Name == "tsbStop")
                {
                    webBrowser1.Stop();
                }
                if (e.ClickedItem.Name == "tsbExit")
                {
                    if (MessageBox.Show("确认退出?", "退出对话框", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        Application.Exit();
                    }
                }
                if (e.ClickedItem.Name == "tsbViewSource")
                {
  WebRequest wrq = WebRequest.Create(txtAddress.Text);
                    WebResponse wrs = wrq.GetResponse();
                    StreamReader sr = new StreamReader(wrs.GetResponseStream(), Encoding.Default);
                    string page = "";
                    string code = null;
                    while ((code = sr.ReadLine()) != null)
                    {
                        page += code;
                    }
                    System.Diagnostics.Process pro = new System.Diagnostics.Process();
                    pro.StartInfo.UseShellExecute = false;
                    pro.StartInfo.FileName = "notepad.exe";//获取要启动的记事本
                    //不适用操作系统外壳启动程序进程
                    pro.StartInfo.RedirectStandardInput = true;//读取
                    pro.StartInfo.RedirectStandardOutput = true;//将应用程序写入到流中
                    pro.Start();//启动
                    if (pro != null)
                    {
                        //调用API,传递数据
                        while (pro.MainWindowHandle == IntPtr.Zero)
                        {
                            pro.Refresh();
                        }
                        IntPtr vHandle = FindWindowEx(pro.MainWindowHandle, IntPtr.Zero, "Edit", null);
                        //传递数据给记事本
                        SendMessage(vHandle, WM_SETTEXT, 0, page);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void txtAddress_KeyPress(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;
            if (key == 13)//回车
            {
                OpenPage();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            txtAddress.Text = @"http://www.baidu.com";
            OpenPage();
        }
    }
}
相关文章
修改浏览器里网页头部小图标傻瓜式教程
修改浏览器里网页头部小图标傻瓜式教程
639 0
修改浏览器里网页头部小图标傻瓜式教程
|
9月前
|
Web App开发 JSON Unix
浏览器:好用的浏览器插件,亲测好用
浏览器:好用的浏览器插件,亲测好用
119 0
|
JavaScript
浏览器下Xpath实用技巧
浏览器下Xpath实用技巧
269 0
浏览器下Xpath实用技巧
启动浏览器打开网页
启动浏览器打开网页
169 0
|
Web App开发 JavaScript 开发者
【软件技巧】【截图】浏览器自带的全网页截图工具
【软件技巧】【截图】浏览器自带的全网页截图工具
307 0
【软件技巧】【截图】浏览器自带的全网页截图工具
C#编程-139:制作自己的浏览器
C#编程-139:制作自己的浏览器
187 0
|
Web App开发
自定义浏览器网页背景是什么体验?
怎么将Chrome浏览器的网页背景设置成自己喜欢的图片背景?一起写个插件来实现一键切换浏览器背景吧。
196 0
自定义浏览器网页背景是什么体验?
html+css实战3-了解-初识-浏览器
html+css实战3-了解-初识-浏览器
80 0
html+css实战3-了解-初识-浏览器
html+css实战93-浏览器效果
html+css实战93-浏览器效果
93 0
html+css实战93-浏览器效果
|
Web App开发 JavaScript Java
推荐一个好用的浏览器记笔记工具
不知道大家平常在浏览网页的过程中,对比较重要的内容,有没有记笔记的习惯。强哥在阅读比如SpringBoot等项目的官方文档,或者是看到一些比较好的博客文章时,都比较喜欢做一些笔记。
推荐一个好用的浏览器记笔记工具