您的位置:首页 > 移动开发 > 微信开发

C#趣味小程序(6)——动态工具栏

2006-10-03 14:14 761 查看
问题描述:
是将MenuStrip中的所有DropDownItems列出来,供用户选择,被选中的项将在ToolStrip被显示出来,并且可以使用与其对应的DropDownItem的事件.
其工作流程可以分解如下图所示:



图1.1流程图
实际上这个图所描述的正是我们上面说的一样,只是换作图形表示而已,但这样一来程序的结构就变得清晰起来了。
实际上这个图所描述的正是我们上面说的一样,只是换作图形表示而已,但这样一来程序的结构就变得清晰起来了。可以看出,整个问题实际上被分解成了三个部分,即遍历菜单栏所有可用的DropDownItems,提供用户的选择界面,和为工具栏动态添Items及其事件。提供用户的选择界面这部分内容没什么技术含量,可以用任何一种你喜欢的方式实现,没什么好说的。下面我将重点说一下遍历菜单栏所有可用的DropDownItems,和为工具栏动态添Items及其事件这两部分。
一、 遍历菜单栏所有可用的DropDownItems
菜单栏的结构实际上是典型的“森林”,而每一个下拉菜单(DropDownItem)都是一棵树。面对这种问题最好的解决方式就是递归。很多人提到递归就头大,其实对于了解递归的人来说递归很简单。尤其是树的遍历就更加的简单了,它有固有的模式,一般先对结束条件进行判断,然后递归遍历各个子树,就这么简单。对于本题DropDownItem的函数可以这样来写:
private ToolStripMenuItem Items(ToolStripMenuItem tsItem)
{ //结束条件判定
if (tsItem.DropDownItems.Count <= 0) return tsItem;
else
{ //遍历各子树
foreach (ToolStripMenuItem mItem in tsItem.DropDownItems)
{ //递归遍历mItem子树
Items(mItem);
}
}
return null;
}
函数首先判断节点(就是一个DropDownItem)是否含有子节点,如果它不含有子节点就return它(这就是结束条件判断)。我们这样做完全是因为我们菜单使用习惯(所有的菜单功能都是在叶子节点实现功能的),当然我们也可以将非叶子节点也return回去。但是最好不要这么干,因为它会令我们产生很多困扰(不信邪的朋友可以试试)。下一步再分别遍历该节点的每一个子树
上面是遍历某个DropDownItem和它的子节点,下面遍历整个菜单栏:
……
//这是遍历菜单栏,可以放在任何你需要的地方,
//注: menuStrip1是我菜单栏的名字
foreach (ToolStripMenuItem mItem in this.menuStrip1.Items)
{ //递归遍历各个DropDownItem
Items(mItem);
}
//
……
上面就是遍历菜单的全部代码。这当然还没完,我们需要将遍历菜单得到的各个菜单项存入容器,以备下面的程序使用。什么容器都可以,而我选择了List<>。要实现这个功能:
首先,我们需要增加一个容器 lMenuItems
……
//增加一个全局变量,它是一个容器
List<ToolStripMenuItem> lMenuItems;
……
然后,在遍历菜单栏程序段中添加代码
//这是遍历菜单栏,可以放在任何你需要的地方,
//menuStrip1是我菜单栏的名字
lMenuItems = new List<ToolStripMenuItem>();
foreach (ToolStripMenuItem mItem in this.menuStrip1.Items)
{ //递归遍历各个DropDownItem
Items(mItem);
}
//
……
将DropDownItem的递归遍历函数修改成下面这个样子
private ToolStripMenuItem Items(ToolStripMenuItem tsItem)
{ //结束条件判定
if (tsItem.DropDownItems.Count <= 0) return tsItem;
else
{ //遍历各子树
ToolStripMenuItem value = null;//临时变量
foreach (ToolStripMenuItem mItem in tsItem.DropDownItems)
{ //递归遍历mItem子树
value = Items(mItem);
if (value != null && lMenuItems != null)
lMenuItems.Add(value);
}
}
return null;
}
好,初战告捷。跳过如何提供用户的选择界面部分不提,直接进入重点。

二、 为工具栏动态添Items及其事件
给工具栏动态添加Items其实是一件非常简单的事,就像有些人说的new一个,再Add()进去就搞定了。关键的问题不在这里,如何使各个工具栏的Item的事件可以与DropDownItem的事件一一对应才是问题的关键。各个DropDownItem都可以拥有自己的事件,各个事件的名称不同,而且事件是不能直接拿事件赋值的,如果无法实现事件的动态赋值,菜单栏动态添加也就没戏了。如果没有好多解决方法,程序开发到这里只能是僵局。

1. 关于事件的调整
首先事件是可以调用事件的,并且几个控件可以共用一个事件。这就为问题的解决提供了可能性。让所有的可用菜单项都使用一个事件,或者说让这个事件统领其他的菜单事件。我们新建一个事件,并将这个事件命名为Menu_Click,它来统领整个Menu的所有Click事件:
private void Menu_Click(object sender, EventArgs e)
{ …… }
第二步,将原来所有DropDownItems的事件(菜单项事件)都替换成Menu_Click。
小知识: 事件参数中的sender事实上就是发出事件的控件(或源),但因为它的类型是object,所有在使用前要对其进行强制类型转换。比如:有一个sender的实际类型是ToolStripItem,我需要获得sender的Name属性值就需要:
string sender_Name = ((ToolStripItem)sender).Name;
应用上面特点,在事件处理函数Menu_Click中就不难使用switch或者if/else来区分事件的来源,并分别调用他们自己的事件处理函数了。
2. 动态添加
解决了事件的问题,工具栏的动态添加实际上就是一句话的事
toolStrip1.Items.Add(new ToolStripButton(
<某DropDownItem>.Text, //DropDownItem显示的文字
<某DropDownItem>.Image, //DropDownItem图标
new System.EventHandler(Menu_Click), //DropDownItem事件
<某DropDownItem>.Name //DropDownItem名称
));
下面是一个简单的实例:(请将代码粘贴到你新建项目中对应得位置)
/*******************/
/*Form1.Designer.cs*/
/*******************/
namespace dynamicToolbar
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.first = new System.Windows.Forms.ToolStripMenuItem();
this.f1 = new System.Windows.Forms.ToolStripMenuItem();
this.f11 = new System.Windows.Forms.ToolStripMenuItem();
this.f12 = new System.Windows.Forms.ToolStripMenuItem();
this.f2 = new System.Windows.Forms.ToolStripMenuItem();
this.f3 = new System.Windows.Forms.ToolStripMenuItem();
this.f31 = new System.Windows.Forms.ToolStripMenuItem();
this.secondToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.s1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.s2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.s3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.s31ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.s311ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.s32ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.thirdToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.t1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.t2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.set = new System.Windows.Forms.Button();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.first,
this.secondToolStripMenuItem,
this.thirdToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// first
//
this.first.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.f1,
this.f2,
this.f3});
this.first.Name = "first";
this.first.Size = new System.Drawing.Size(47, 20);
this.first.Text = "first";
this.first.Click += new System.EventHandler(this.Menu_Click);
//
// f1
//
this.f1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.f11,
this.f12});
this.f1.Name = "f1";
this.f1.Size = new System.Drawing.Size(82, 22);
this.f1.Text = "f1";
//
// f11
//
this.f11.Name = "f11";
this.f11.Size = new System.Drawing.Size(88, 22);
this.f11.Text = "f11";
this.f11.Click += new System.EventHandler(this.Menu_Click);
//
// f12
//
this.f12.Name = "f12";
this.f12.Size = new System.Drawing.Size(88, 22);
this.f12.Text = "f12";
this.f12.Click += new System.EventHandler(this.Menu_Click);
//
// f2
//
this.f2.Name = "f2";
this.f2.Size = new System.Drawing.Size(82, 22);
this.f2.Text = "f2";
this.f2.Click += new System.EventHandler(this.Menu_Click);
//
// f3
//
this.f3.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.f31});
this.f3.Name = "f3";
this.f3.Size = new System.Drawing.Size(82, 22);
this.f3.Text = "f3";
//
// f31
//
this.f31.Name = "f31";
this.f31.Size = new System.Drawing.Size(88, 22);
this.f31.Text = "f31";
this.f31.Click += new System.EventHandler(this.Menu_Click);
//
// secondToolStripMenuItem
//
this.secondToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.s1ToolStripMenuItem,
this.s2ToolStripMenuItem,
this.s3ToolStripMenuItem});
this.secondToolStripMenuItem.Name = "secondToolStripMenuItem";
this.secondToolStripMenuItem.Size = new System.Drawing.Size(53, 20);
this.secondToolStripMenuItem.Text = "second";
//
// s1ToolStripMenuItem
//
this.s1ToolStripMenuItem.Name = "s1ToolStripMenuItem";
this.s1ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);
this.s1ToolStripMenuItem.Text = "s1";
this.s1ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);
//
// s2ToolStripMenuItem
//
this.s2ToolStripMenuItem.Name = "s2ToolStripMenuItem";
this.s2ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);
this.s2ToolStripMenuItem.Text = "s2";
this.s2ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);
//
// s3ToolStripMenuItem
//
this.s3ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.s31ToolStripMenuItem,
this.s32ToolStripMenuItem});
this.s3ToolStripMenuItem.Name = "s3ToolStripMenuItem";
this.s3ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);
this.s3ToolStripMenuItem.Text = "s3";
//
// s31ToolStripMenuItem
//
this.s31ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.s311ToolStripMenuItem});
this.s31ToolStripMenuItem.Name = "s31ToolStripMenuItem";
this.s31ToolStripMenuItem.Size = new System.Drawing.Size(88, 22);
this.s31ToolStripMenuItem.Text = "s31";
//
// s311ToolStripMenuItem
//
this.s311ToolStripMenuItem.Name = "s311ToolStripMenuItem";
this.s311ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
this.s311ToolStripMenuItem.Text = "s311";
this.s311ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);
//
// s32ToolStripMenuItem
//
this.s32ToolStripMenuItem.Name = "s32ToolStripMenuItem";
this.s32ToolStripMenuItem.Size = new System.Drawing.Size(88, 22);
this.s32ToolStripMenuItem.Text = "s32";
this.s32ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);
//
// thirdToolStripMenuItem
//
this.thirdToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.t1ToolStripMenuItem,
this.t2ToolStripMenuItem});
this.thirdToolStripMenuItem.Name = "thirdToolStripMenuItem";
this.thirdToolStripMenuItem.Size = new System.Drawing.Size(47, 20);
this.thirdToolStripMenuItem.Text = "third";
//
// t1ToolStripMenuItem
//
this.t1ToolStripMenuItem.Name = "t1ToolStripMenuItem";
this.t1ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);
this.t1ToolStripMenuItem.Text = "t1";
this.t1ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);
//
// t2ToolStripMenuItem
//
this.t2ToolStripMenuItem.Name = "t2ToolStripMenuItem";
this.t2ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);
this.t2ToolStripMenuItem.Text = "t2";
this.t2ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);
//
// set
//
this.set.Location = new System.Drawing.Point(106, 185);
this.set.Name = "set";
this.set.Size = new System.Drawing.Size(75, 23);
this.set.TabIndex = 3;
this.set.Text = "set->Add";
this.set.UseVisualStyleBackColor = true;
this.set.Click += new System.EventHandler(this.btnSet_Click);
//
// toolStrip1
//
this.toolStrip1.Location = new System.Drawing.Point(0, 24);
this.toolStrip1.Name = "toolStrip2";
this.toolStrip1.Size = new System.Drawing.Size(292, 25);
this.toolStrip1.TabIndex = 4;
this.toolStrip1.Text = "toolStrip2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.toolStrip1);
this.Controls.Add(this.set);
this.Controls.Add(this.menuStrip1);
this.Name = "Form1";
this.Text = "Form1";
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.ToolStripMenuItem first;
private System.Windows.Forms.ToolStripMenuItem f1;
private System.Windows.Forms.ToolStripMenuItem f11;
private System.Windows.Forms.ToolStripMenuItem f12;
private System.Windows.Forms.ToolStripMenuItem f2;
private System.Windows.Forms.ToolStripMenuItem f3;
private System.Windows.Forms.ToolStripMenuItem f31;
private System.Windows.Forms.ToolStripMenuItem secondToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem s1ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem s2ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem s3ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem s31ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem s311ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem s32ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem thirdToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem t1ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem t2ToolStripMenuItem;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.Button set;
}
}
/*******************/
/* Form1.cs */
/*******************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace dynamicToolbar
{
public partial class Form1 : Form
{ //容器
private List<ToolStripMenuItem> lMenuItems;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{ //加载菜单项到容器
lMenuItems = new List<ToolStripMenuItem>();
foreach (ToolStripMenuItem mItem in this.menuStrip1.Items)
{
Items(mItem);
}
}

#region 菜单事件
private void Menu_Click(object sender, EventArgs e)
{
string itemName = ((ToolStripItem)sender).Name;
switch (itemName[0])
{
case 'f': first_Click(sender, e); break;
case 's': second_Click(sender, e); break;
case 't': third_Click(sender, e); break;
}
}
private void third_Click(object sender, EventArgs e)
{
MessageBox.Show("这是菜单“Third”的事件处理函数");
}
private void second_Click(object sender, EventArgs e)
{
MessageBox.Show("这是菜单“Second”的事件处理函数");
}
private void first_Click(object sender, EventArgs e)
{
string itemName = ((ToolStripItem)sender).Name;
string showString = null;
switch (itemName)
{
case "f11": showString = "菜单项“" + this.f11.Name + "”被按下了"; break;
case "f12": showString = "菜单项“" + this.f12.Name + "”被按下了"; break;
case "f2": showString = "菜单项“" + this.f2.Name + "”被按下了"; break;
case "f31": showString = "菜单项“" + this.f31.Name + "”被按下了"; break;
default: break;
}
if (showString != null)
MessageBox.Show(showString);
}
#endregion

//递归遍历DropDownItem函数
private ToolStripMenuItem Items(ToolStripMenuItem tsItem)
{ //结束条件判定
if (tsItem.DropDownItems.Count <= 0) return tsItem;
else
{ //遍历各子树
ToolStripMenuItem value = null;//临时变量
foreach (ToolStripMenuItem mItem in tsItem.DropDownItems)
{
value = Items(mItem);//递归遍历mItem子树
if (value != null && lMenuItems != null)
lMenuItems.Add(value);
}
}
return null;
}//递归遍历DropDownItem函数<END>

//动态设置工具栏函数
private void btnSet_Click(object sender, EventArgs e)
{
seting fset = new seting();
foreach (ToolStripMenuItem mitem in lMenuItems)
{
if (mitem.Name != new ToolStripSeparator().Name)
fset.listBox1.Items.Add(mitem.Name);
}
if (fset.ShowDialog() == DialogResult.OK)
{
foreach (string mn in fset.listBox1.SelectedItems)
{ //如果toolStrip1还没有加载,加载之
if (toolStrip1 == null)
{
toolStrip1 = new ToolStrip();
toolStrip1.Top = menuStrip1.Bottom + 1;
toolStrip1.Left = menuStrip1.Left;
toolStrip1.Name = "toolStrip1";
toolStrip1.Size = new System.Drawing.Size(292, 25);
toolStrip1.TabIndex = 2;
toolStrip1.Text = "toolStrip1";

Controls.Add(this.toolStrip1);//将工具栏添加到窗口上
}
//找到与列表框中对应的DropDownItem,添加之
int i = 0;
while (i < lMenuItems.Count && mn != lMenuItems[i].Name)
{ i++; }
if (i < lMenuItems.Count)
{
//给ToolStrip动态添加
toolStrip1.Items.Add(new ToolStripButton(
lMenuItems[i].Text,
lMenuItems[i].Image,
new System.EventHandler(Menu_Click),
lMenuItems[i].Name
)
);
lMenuItems.Remove(lMenuItems[i]);//把已经添加过的从列表中去除
}
i = 0;
}
}
}//动态设置工具栏函数<END>

}
}
/********************/
/*seting.Designer.cs*/
/********************/
namespace dynamicToolbar
{
partial class seting
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 238);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "add";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(209, 238);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "取消";
this.button2.UseVisualStyleBackColor = true;
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(12, 12);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
this.listBox1.Size = new System.Drawing.Size(268, 220);
this.listBox1.TabIndex = 3;
//
// seting
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "seting";
this.Text = "seting";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public System.Windows.Forms.ListBox listBox1;
}
}
/********************/
/*seting.Designer.cs*/
/********************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace dynamicToolbar
{
public partial class seting : Form
{
public seting()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息