构建插件式的应用程序框架(七)----基本服务(ZT)

本文介绍了ToolStripService的设计与实现过程,包括如何管理菜单、工具栏和停靠工具栏等组件。详细展示了如何通过分配Key来获取和管理这些组件,以及在不同位置添加工具栏的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

既然做好了框架,我们就希望为某个目标服务,我们要提供一些基本的服务,方便用户继续扩展他的功能。首先想到的功能就是,菜单,工具栏的管理,接下来我们要实现一些更流行的功能,比如停靠工具栏等等。
       如何实现这些服务呢?我们希望我们的插件在运行时可以获得应用程序本身的菜单,工具条,停靠工具栏等等,然后向他们添加项目,比如加入一个菜单项,添加一个工具栏按钮。为了在运行时获得某个菜单或者工具栏,我们要为每一个菜单后者工具栏分配一个Key,然后放到一个词典中,当需要的时候,我们通过这个key来获得实例。对于这个Key呢,在我的例子比较简单就是他的名字,我们来看看ToolStripService的代码:
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
None.gif
namespace PluginFramework
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class ToolStripService:IToolStripService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private IApplication application = null;
InBlock.gif        
private Dictionary<String, ToolStrip> toolStrips = new Dictionary<string, ToolStrip>();
InBlock.gif
InBlock.gif        
public ToolStripService(IApplication application)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.application = application;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IToolStripService Members#region IToolStripService Members
InBlock.gif
InBlock.gif        
public System.Windows.Forms.ToolStrip GetToolStrip(string toolStripName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ToolStrip toolStrip 
= null;
InBlock.gif            
if (toolStrips.ContainsKey(toolStripName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                toolStrip 
= toolStrips[toolStripName];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return toolStrip;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void AddToolStrip(string toolStripName, System.Windows.Forms.ToolStrip toolStrip)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (toolStrips.ContainsKey(toolStripName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"The tool strip name has existed!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                toolStrips[toolStripName] 
= toolStrip;
InBlock.gif                
//如果没有指定toolstrip在哪个面板,择默认加到顶部
InBlock.gif
                if (application.TopToolPanel != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    application.TopToolPanel.Controls.Add(toolStrip);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void AddToolStrip(string toolStripName, System.Windows.Forms.ToolStrip toolStrip, ToolStripDockState option)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (toolStrips.ContainsKey(toolStripName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"The tool strip name has existed!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                toolStrips[toolStripName] 
= toolStrip;
InBlock.gif                
switch (option)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case ToolStripDockState.Left:
InBlock.gif                        
if (application.LeftToolPanel != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            application.LeftToolPanel.Controls.Add(toolStrip);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
break;
InBlock.gif                    
case ToolStripDockState.Right:
InBlock.gif                        
if (application.RightToolPanel!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            application.RightToolPanel.Controls.Add(toolStrip);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
break;
InBlock.gif                    
case ToolStripDockState.Top:
InBlock.gif                        
if (application.TopToolPanel != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            application.TopToolPanel.Controls.Add(toolStrip);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
break;
InBlock.gif                    
case ToolStripDockState.Bottom:
InBlock.gif                        
if (application.BottomToolPanel != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            application.BottomToolPanel.Controls.Add(toolStrip);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void RemoveToolStrip(string toolStripName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ToolStrip toolStrip 
= GetToolStrip(toolStripName);
InBlock.gif            
if (toolStrip != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (application.TopToolPanel != null && application.TopToolPanel.Controls.Contains(toolStrip))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    application.TopToolPanel.Controls.Remove(toolStrip);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (application.BottomToolPanel != null && application.BottomToolPanel.Controls.Contains(toolStrip))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    application.BottomToolPanel.Controls.Remove(toolStrip);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (application.LeftToolPanel != null && application.LeftToolPanel.Controls.Contains(toolStrip))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    application.LeftToolPanel.Controls.Remove(toolStrip);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (application.RightToolPanel != null && application.RightToolPanel.Controls.Contains(toolStrip))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    application.RightToolPanel.Controls.Remove(toolStrip);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            toolStrips.Remove(toolStripName);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

      对于视图或者是停靠工具栏来说,最好是不要直接在词典中放入实例,而是应该将对象的类型放入到词典中,因为,视图和停靠工具栏本身都是从Form派生而来,所以,当视图或者是停靠工具栏被关闭的时候,对象就被销毁了,而对象的创建在是插件的Load方法里完成的,我们不可能再去调用插件的Load方法,这样给我们的使用带来了不便,所以我们应该注册类型,然后在Service中实现一个Show方法是比较合理的,这里为了演示方便,我就直接在Load里面实例化了,并把实例放到了词典里。
      下边这个图例里显示了插件加入的停靠工具栏,工具栏,一个新的菜单“View”和View菜单的子菜单:
      
       

转载于:https://www.cnblogs.com/chinhr/archive/2007/06/18/787417.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值