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();
}
}
}