通过遍历XML文件动态加载菜单,顺便利用WebBrowser控件实现一个简单的桌面浏览器
效果如下:
代码如下:
XMLFile1.xml
<?xml version="1.0" standalone="yes"?>
<ds1>
<dtbl1>
<Name>搜狐播客</Name>
<URl>http://blog.sohu.com/</URl>
</dtbl1>
<dtbl1>
<Name>网易博客</Name>
<URl>http://blog.163.com/</URl>
</dtbl1>
<dtbl1>
<Name>新浪博客</Name>
<URl>http://weibo.com</URl>
</dtbl1>
<!--C#正则表达式-->
<dtbl2>
<Name>淘宝</Name>
<URL>http://www.taobao.com</URL>
</dtbl2>
<dtbl2>
<Name>一号店</Name>
<URL>http://www.yhd.com</URL>
</dtbl2>
<dtbl2>
<Name>京东</Name>
<URL>http://www.jd.com</URL>
</dtbl2>
</ds1>
From1.Designer.cs
namespace Dynamic_loading_menu
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.menus1 = new System.Windows.Forms.ToolStripMenuItem();
this.menus2 = new System.Windows.Forms.ToolStripMenuItem();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.menus1,
this.menus2});
this.menuStrip1.Location = new System.Drawing.Point(, );
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(, );
this.menuStrip1.TabIndex = ;
this.menuStrip1.Text = "menuStrip1";
//
// menus1
//
this.menus1.Image = global::Dynamic_loading_menu.Properties.Resources.folder;
this.menus1.Name = "menus1";
this.menus1.Size = new System.Drawing.Size(, );
this.menus1.Text = "博客";
//
// menus2
//
this.menus2.Image = global::Dynamic_loading_menu.Properties.Resources.folder;
this.menus2.Name = "menus2";
this.menus2.Size = new System.Drawing.Size(, );
this.menus2.Text = "电商";
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(, );
this.webBrowser1.MinimumSize = new System.Drawing.Size(, );
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(, );
this.webBrowser1.TabIndex = ;
this.webBrowser1.Url = new System.Uri("http://www.baidu.com", System.UriKind.Absolute);
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
this.webBrowser1.NewWindow += new System.ComponentModel.CancelEventHandler(this.webBrowser1_NewWindow);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem menus1;
private System.Windows.Forms.ToolStripMenuItem menus2;
private System.Windows.Forms.WebBrowser webBrowser1;
}
}
From1.cs
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; namespace Dynamic_loading_menu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
#region 动态加载菜单 DataSet ds = new DataSet();
ds.ReadXml(Application.StartupPath + "\\XMLFile1.xml"); foreach (DataRow drow in ds.Tables[].Rows)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = drow[].ToString();
item.Tag = drow[];
item.Click += new EventHandler(item_Click);
menus1.DropDownItems.Add(item);
}
foreach (DataRow drow in ds.Tables[].Rows)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = drow[].ToString();
item.Tag = drow[];
item.Click += new EventHandler(item_Click);
menus2.DropDownItems.Add(item);
}
#endregion
}
private void item_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender; this.webBrowser1.Navigate(item.Tag.ToString()); }
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//将所有的链接的目标,指向本窗体
foreach (HtmlElement archor in this.webBrowser1.Document.Links)
{
archor.SetAttribute("target", "_self");
}
//将所有的FORM的提交目标,指向本窗体
foreach (HtmlElement form in this.webBrowser1.Document.Forms)
{
form.SetAttribute("target", "_self");
}
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
} }
}