文本编辑器

                                          动态创建菜单(*)

•菜单项不仅可以静态设计,也可以动态的增加。
–设计父菜单项,在Load事件中 ToolStripItem item = 工具ToolStripMenuItem.DropDownItems.Add("1");编写for循环添加10项
–响应菜单事件,item.Click += new EventHandler(item_Click);
–如何点击菜单的时候知道点击的是哪个菜单?sender就是被点击的菜单项:ToolStripItem item = sender as ToolStripItem。在添加菜单项的时候将菜单的Tag设置为序号,在item_Click从菜单项的Tag中读取即可。
 
View Code
1 namespace 文本编辑器接口
2 {
3     public interface IEditorPlugin
4     {
5         string Name { get; }
6         string Execute(string oldText);
7     }
8 }
View Code
 1 namespace 文本编辑器之全部大写
 2 {
 3     //Plugin:插件。Addin:插件。
 4     public class ToUpperEditorPlugin:IEditorPlugin
 5     {
 6         #region IEditorPlugin 成员
 7 
 8         public string Execute(string oldText)
 9         {
10             return oldText.ToUpper();
11         }
12 
13         public string Name
14         {
15             get {return "转换为大写"; }
16         }
17 
18         #endregion
19     }
20 }
View Code
 1 namespace 文本编辑器之全部小写
 2 {
 3     public class ToLowerEditoPlugin:IEditorPlugin
 4     {
 5         #region IEditorPlugin 成员
 6 
 7         public string Execute(string oldText)
 8         {
 9             return oldText.ToLower();
10         }
11 
12         public string Name
13         {
14             get { return "全部小写"; }
15         }
16 
17         #endregion
18     }
19 }
View Code
 1 namespace 文本编辑器主程序
 2 {
 3     [Serializable]
 4     public partial class Form1 : Form
 5     {
 6         public Form1()
 7         {
 8             InitializeComponent();
 9         }
10 
11         private void Form1_Load(object sender, EventArgs e)
12         {
13            // ToolStripMenuItem mi1 = new ToolStripMenuItem("测试");
14            // 工具ToolStripMenuItem.DropDownItems.Add(mi1);
15            // mi1.Click += new EventHandler(mi1_Click);
16            //// mi1.Tag = typeof;
17 
18            // ToolStripMenuItem mi2 = new ToolStripMenuItem("测试2");
19            // 工具ToolStripMenuItem.DropDownItems.Add(mi2);
20            // mi2.Click += new EventHandler(mi1_Click);
21 
22             //Assembly.GetExecutingAssembly().Location
23             string[] files =
24                 Directory.GetFiles(@"C:\editordll", "*.dll");
25             foreach (string file in files)
26             {
27                 Assembly asm = Assembly.LoadFile(file);
28                 Type[] types = asm.GetExportedTypes();
29                 
30                 foreach (Type type in types)
31                 {
32                     Type typeIEditorPlugin = 
33                         typeof(IEditorPlugin);
34                     //判断遍历的类型是否实现了IEditorPlugin接口
35                     if (typeIEditorPlugin.IsAssignableFrom(type)
36                         &&!type.IsAbstract)//非抽象的类型
37                     {
38                         IEditorPlugin editorPlugin = 
39                             (IEditorPlugin)Activator.CreateInstance(type);
40                        //动态创建插件对应的菜单项
41                         ToolStripMenuItem mi1 = new ToolStripMenuItem(editorPlugin.Name);
42                         工具ToolStripMenuItem.DropDownItems.Add(mi1);
43                         mi1.Click += new EventHandler(mi1_Click);
44                         mi1.Tag = editorPlugin;
45                     }
46                 }
47             }
48         }
49 
50         void mi1_Click(object sender, EventArgs e)
51         {
52             ToolStripMenuItem mi = sender as ToolStripMenuItem;
53             IEditorPlugin editorPlugin = (IEditorPlugin)mi.Tag;
54             textBox1.Text = editorPlugin.Execute(textBox1.Text);
55         }
56     }
57 }

 在C:\editordll文件夹下可以添加任何实现了IEditorPlugin接口的程序集(即插件);

410号

posted @ 2013-03-18 19:24  Big.Eagle  阅读(198)  评论(0)    收藏  举报