Chinaunix
标题:
调试通过的一个WebBrowser
[打印本页]
作者:
5204669
时间:
2008-12-11 18:56
标题:
调试通过的一个WebBrowser
using System; using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WebBrowser
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuFile;
private System.Windows.Forms.MenuItem mnuFileSaveAs;
private System.Windows.Forms.Panel panel_add;
private System.Windows.Forms.Panel panel_tool;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnGo;
public AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.Button btnForward;
private System.Windows.Forms.Button btnBack;
private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Button btnHome;
public System.Windows.Forms.TextBox txtUrl;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button btnStop;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuFileSaveAs = new System.Windows.Forms.MenuItem();
this.panel_add = new System.Windows.Forms.Panel();
this.txtUrl = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.panel_tool = new System.Windows.Forms.Panel();
this.btnForward = new System.Windows.Forms.Button();
this.btnBack = new System.Windows.Forms.Button();
this.btnRefresh = new System.Windows.Forms.Button();
this.btnHome = new System.Windows.Forms.Button();
this.btnGo = new System.Windows.Forms.Button();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.btnStop = new System.Windows.Forms.Button();
this.panel_add.SuspendLayout();
this.panel_tool.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFile});
//
// mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFileSaveAs});
this.mnuFile.Text = "文件";
//
// mnuFileSaveAs
//
this.mnuFileSaveAs.Index = 0;
this.mnuFileSaveAs.Text = "另存为...";
this.mnuFileSaveAs.Click += new System.EventHandler(this.mnuFileSaveAs_Click);
//
// panel_add
//
this.panel_add.Controls.Add(this.btnGo);
this.panel_add.Controls.Add(this.txtUrl);
this.panel_add.Controls.Add(this.label1);
this.panel_add.Dock = System.Windows.Forms.DockStyle.Top;
this.panel_add.Location = new System.Drawing.Point(0, 32);
this.panel_add.Name = "panel_add";
this.panel_add.Size = new System.Drawing.Size(368, 32);
this.panel_add.TabIndex = 10;
//
// txtUrl
//
this.txtUrl.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtUrl.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtUrl.Location = new System.Drawing.Point(48, 8);
this.txtUrl.Name = "txtUrl";
this.txtUrl.Size = new System.Drawing.Size(248, 19);
this.txtUrl.TabIndex = 11;
this.txtUrl.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 12;
this.label1.Text = "地址";
//
// panel_tool
//
this.panel_tool.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel_tool.Controls.Add(this.btnStop);
this.panel_tool.Controls.Add(this.btnForward);
this.panel_tool.Controls.Add(this.btnBack);
this.panel_tool.Controls.Add(this.btnRefresh);
this.panel_tool.Controls.Add(this.btnHome);
this.panel_tool.Dock = System.Windows.Forms.DockStyle.Top;
this.panel_tool.Location = new System.Drawing.Point(0, 0);
this.panel_tool.Name = "panel_tool";
this.panel_tool.Size = new System.Drawing.Size(368, 32);
this.panel_tool.TabIndex = 9;
//
// btnForward
//
this.btnForward.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnForward.Image = ((System.Drawing.Image)(resources.GetObject("btnForward.Image")));
this.btnForward.Location = new System.Drawing.Point(40, 0);
this.btnForward.Name = "btnForward";
this.btnForward.Size = new System.Drawing.Size(30, 30);
this.btnForward.TabIndex = 10;
this.btnForward.Click += new System.EventHandler(this.btnForward_Click);
//
// btnBack
//
this.btnBack.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnBack.Image = ((System.Drawing.Image)(resources.GetObject("btnBack.Image")));
this.btnBack.Location = new System.Drawing.Point(8, 0);
this.btnBack.Name = "btnBack";
this.btnBack.Size = new System.Drawing.Size(30, 30);
this.btnBack.TabIndex = 11;
this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
//
// btnRefresh
//
this.btnRefresh.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnRefresh.Image = ((System.Drawing.Image)(resources.GetObject("btnRefresh.Image")));
this.btnRefresh.Location = new System.Drawing.Point(104, 0);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.Size = new System.Drawing.Size(30, 30);
this.btnRefresh.TabIndex = 12;
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
//
// btnHome
//
this.btnHome.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnHome.Image = ((System.Drawing.Image)(resources.GetObject("btnHome.Image")));
this.btnHome.Location = new System.Drawing.Point(136, 0);
this.btnHome.Name = "btnHome";
this.btnHome.Size = new System.Drawing.Size(30, 30);
this.btnHome.TabIndex = 9;
this.btnHome.Click += new System.EventHandler(this.btnHome_Click);
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(304, 8);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(48, 23);
this.btnGo.TabIndex = 13;
this.btnGo.Text = "转到";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// axWebBrowser1
//
this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(0, 64);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(368, 225);
this.axWebBrowser1.TabIndex = 11;
this.axWebBrowser1.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(this.axWebBrowser1_NewWindow2);
//
// btnStop
//
this.btnStop.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnStop.Image = ((System.Drawing.Image)(resources.GetObject("btnStop.Image")));
this.btnStop.Location = new System.Drawing.Point(72, 0);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(30, 30);
this.btnStop.TabIndex = 13;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// Form1
//
this.AcceptButton = this.btnGo;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 289);
this.Controls.Add(this.axWebBrowser1);
this.Controls.Add(this.panel_add);
this.Controls.Add(this.panel_tool);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "浏览器";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
this.panel_add.ResumeLayout(false);
this.panel_tool.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.saveFileDialog1.Filter="网页(*.html;*.htm)|*.html;*.htm|文本文件(*.txt)|*.txt";
this.saveFileDialog1.FilterIndex=1;
this.saveFileDialog1.OverwritePrompt=true;
this.btnHome.PerformClick();
}
private void btnBack_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.GoBack();
this.ShowUrl();
}
private void ShowUrl()
{
this.txtUrl.Text=this.axWebBrowser1.LocationURL;
}
private void btnForward_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.GoForward();
this.ShowUrl();
}
private void btnStop_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.Stop();
}
private void btnRefresh_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.Refresh();
}
private void btnHome_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.GoHome();
this.ShowUrl();
}
private void btnGo_Click(object sender, System.EventArgs e)
{
//if(e.KeyCode==Keys.Enter)
string url = this.txtUrl.Text.Trim();
object refmissing = System.Reflection.Missing.Value;
this.axWebBrowser1.Navigate(url,ref refmissing,ref refmissing,ref refmissing,ref refmissing);
}
private void mnuFileSaveAs_Click(object sender, System.EventArgs e)
{
if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
{
System.Net.WebClient webClient=new System.Net.WebClient();
webClient.DownloadFile(this.axWebBrowser1.LocationURL,this.saveFileDialog1.FileName);
}
}
private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
Form1 f=new Form1();
f.Show();
e.ppDisp = f.axWebBrowser1.Application;
f.axWebBrowser1.RegisterAsBrowser = true;
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WebBrowser
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuFile;
private System.Windows.Forms.MenuItem mnuFileSaveAs;
private System.Windows.Forms.Panel panel_add;
private System.Windows.Forms.Panel panel_tool;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnGo;
public AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.Button btnForward;
private System.Windows.Forms.Button btnBack;
private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Button btnHome;
public System.Windows.Forms.TextBox txtUrl;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button btnStop;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuFileSaveAs = new System.Windows.Forms.MenuItem();
this.panel_add = new System.Windows.Forms.Panel();
this.txtUrl = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.panel_tool = new System.Windows.Forms.Panel();
this.btnForward = new System.Windows.Forms.Button();
this.btnBack = new System.Windows.Forms.Button();
this.btnRefresh = new System.Windows.Forms.Button();
this.btnHome = new System.Windows.Forms.Button();
this.btnGo = new System.Windows.Forms.Button();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.btnStop = new System.Windows.Forms.Button();
this.panel_add.SuspendLayout();
this.panel_tool.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFile});
//
// mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFileSaveAs});
this.mnuFile.Text = "文件";
//
// mnuFileSaveAs
//
this.mnuFileSaveAs.Index = 0;
this.mnuFileSaveAs.Text = "另存为...";
this.mnuFileSaveAs.Click += new System.EventHandler(this.mnuFileSaveAs_Click);
//
// panel_add
//
this.panel_add.Controls.Add(this.btnGo);
this.panel_add.Controls.Add(this.txtUrl);
this.panel_add.Controls.Add(this.label1);
this.panel_add.Dock = System.Windows.Forms.DockStyle.Top;
this.panel_add.Location = new System.Drawing.Point(0, 32);
this.panel_add.Name = "panel_add";
this.panel_add.Size = new System.Drawing.Size(368, 32);
this.panel_add.TabIndex = 10;
//
// txtUrl
//
this.txtUrl.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtUrl.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtUrl.Location = new System.Drawing.Point(48, 8);
this.txtUrl.Name = "txtUrl";
this.txtUrl.Size = new System.Drawing.Size(248, 19);
this.txtUrl.TabIndex = 11;
this.txtUrl.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 12;
this.label1.Text = "地址";
//
// panel_tool
//
this.panel_tool.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel_tool.Controls.Add(this.btnStop);
this.panel_tool.Controls.Add(this.btnForward);
this.panel_tool.Controls.Add(this.btnBack);
this.panel_tool.Controls.Add(this.btnRefresh);
this.panel_tool.Controls.Add(this.btnHome);
this.panel_tool.Dock = System.Windows.Forms.DockStyle.Top;
this.panel_tool.Location = new System.Drawing.Point(0, 0);
this.panel_tool.Name = "panel_tool";
this.panel_tool.Size = new System.Drawing.Size(368, 32);
this.panel_tool.TabIndex = 9;
//
// btnForward
//
this.btnForward.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnForward.Image = ((System.Drawing.Image)(resources.GetObject("btnForward.Image")));
this.btnForward.Location = new System.Drawing.Point(40, 0);
this.btnForward.Name = "btnForward";
this.btnForward.Size = new System.Drawing.Size(30, 30);
this.btnForward.TabIndex = 10;
this.btnForward.Click += new System.EventHandler(this.btnForward_Click);
//
// btnBack
//
this.btnBack.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnBack.Image = ((System.Drawing.Image)(resources.GetObject("btnBack.Image")));
this.btnBack.Location = new System.Drawing.Point(8, 0);
this.btnBack.Name = "btnBack";
this.btnBack.Size = new System.Drawing.Size(30, 30);
this.btnBack.TabIndex = 11;
this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
//
// btnRefresh
//
this.btnRefresh.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnRefresh.Image = ((System.Drawing.Image)(resources.GetObject("btnRefresh.Image")));
this.btnRefresh.Location = new System.Drawing.Point(104, 0);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.Size = new System.Drawing.Size(30, 30);
this.btnRefresh.TabIndex = 12;
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
//
// btnHome
//
this.btnHome.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnHome.Image = ((System.Drawing.Image)(resources.GetObject("btnHome.Image")));
this.btnHome.Location = new System.Drawing.Point(136, 0);
this.btnHome.Name = "btnHome";
this.btnHome.Size = new System.Drawing.Size(30, 30);
this.btnHome.TabIndex = 9;
this.btnHome.Click += new System.EventHandler(this.btnHome_Click);
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(304, 8);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(48, 23);
this.btnGo.TabIndex = 13;
this.btnGo.Text = "转到";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// axWebBrowser1
//
this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(0, 64);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(368, 225);
this.axWebBrowser1.TabIndex = 11;
this.axWebBrowser1.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(this.axWebBrowser1_NewWindow2);
//
// btnStop
//
this.btnStop.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnStop.Image = ((System.Drawing.Image)(resources.GetObject("btnStop.Image")));
this.btnStop.Location = new System.Drawing.Point(72, 0);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(30, 30);
this.btnStop.TabIndex = 13;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// Form1
//
this.AcceptButton = this.btnGo;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 289);
this.Controls.Add(this.axWebBrowser1);
this.Controls.Add(this.panel_add);
this.Controls.Add(this.panel_tool);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "浏览器";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
this.panel_add.ResumeLayout(false);
this.panel_tool.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.saveFileDialog1.Filter="网页(*.html;*.htm)|*.html;*.htm|文本文件(*.txt)|*.txt";
this.saveFileDialog1.FilterIndex=1;
this.saveFileDialog1.OverwritePrompt=true;
this.btnHome.PerformClick();
}
private void btnBack_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.GoBack();
this.ShowUrl();
}
private void ShowUrl()
{
this.txtUrl.Text=this.axWebBrowser1.LocationURL;
}
private void btnForward_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.GoForward();
this.ShowUrl();
}
private void btnStop_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.Stop();
}
private void btnRefresh_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.Refresh();
}
private void btnHome_Click(object sender, System.EventArgs e)
{
this.axWebBrowser1.GoHome();
this.ShowUrl();
}
private void btnGo_Click(object sender, System.EventArgs e)
{
//if(e.KeyCode==Keys.Enter)
string url = this.txtUrl.Text.Trim();
object refmissing = System.Reflection.Missing.Value;
this.axWebBrowser1.Navigate(url,ref refmissing,ref refmissing,ref refmissing,ref refmissing);
}
private void mnuFileSaveAs_Click(object sender, System.EventArgs e)
{
if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
{
System.Net.WebClient webClient=new System.Net.WebClient();
webClient.DownloadFile(this.axWebBrowser1.LocationURL,this.saveFileDialog1.FileName);
}
}
private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
Form1 f=new Form1();
f.Show();
e.ppDisp = f.axWebBrowser1.Application;
f.axWebBrowser1.RegisterAsBrowser = true;
}
}
}
调试通过的一个WebBrowser
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u/28528/showart_1714371.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2