写Form设计器尝试(六) 实现菜单命令

在写这篇的正文之前,我要衷心感谢微软公司的Jeffrey Tan先生,他居然看懂了我的文理不通的英文提问,并且花费宝贵时间来钻研,帮我解决了一个星期来我百思不得其解的问题。我当时的问题是我写了一些语句来实现标准菜单命令,但是调试的时候怎么也得不到正确的结果,事实上我研究的两个例子用的方法和我的类似,它们却都可以正常执行。我一直没有找到原因,不得不求助支持。

 在“Form设计器尝试() 修改窗体上的控件属性”我提出了怎么删除控件的问题,我拙作的关注者山伟也提出过用什么方法实现控件对齐更简便。所有这些问题的答案是使用MenuCommandService, .net 1.0/1.1的办法是手工写一个实现IMenuCommandService接口的类,将其实例添加到服务容器中,而.net 2.0已经为我们提供了MenuCommandService

 打开主窗体代码界面,为Form1添加一个私有变量:

private  MenuCommandService menuCommandService;
 修改Load事件代码,由于我们已经改了几次了,所以我这里全部帖出:
private   void  Form1_Load( object  sender, EventArgs e)
{
    DesignSurface surface 
= new DesignSurface();

    toolBoxService 
= new DemoToolboxService();
    toolBoxService.ToolBox 
= new ListBox();

    toolBoxService.ToolBox.Items.Add(
"Point");

    toolBoxService.ToolBox.Items.Add(
new ToolboxItem(typeof(Button)));
    toolBoxService.ToolBox.Items.Add(
new ToolboxItem(typeof(TextBox)));
    toolBoxService.ToolBox.Items.Add(
new ToolboxItem(typeof(Label)));
    toolBoxService.ToolBox.Items.Add(
new ToolboxItem(typeof(TabControl)));
    toolBoxService.ToolBox.Items.Add(
new ToolboxItem(typeof(StatusBar)));

   
// Assembly a1=Assembly.LoadFrom(@"D:/Dotnet/MyControl.dll");
   
// toolBoxService.ToolBox.Items.Add(new ToolboxItem(a1.GetType("MyControl.ComboBoxField")));

    toolBoxService.ToolBox.Dock 
= DockStyle.Fill;
    
this.panel1.Controls.Add(toolBoxService.ToolBox);


    IServiceContainer container 
= surface.GetService(typeof(IServiceContainer)) as IServiceContainer;

    menuCommandService 
= new MenuCommandService(surface);

    
if (container != null)
    
{
        container.AddService(
typeof(IToolboxService), toolBoxService);
        container.AddService(
typeof(IMenuCommandService), menuCommandService);
    }


    surface.BeginLoad(
typeof(Form));
    Control view 
= (Control)surface.View;
    view.Dock 
= DockStyle.Fill;
    
this.splitContainer1.Panel1.Controls.Add(view);
    
this.propertyGrid1.SelectedObject = surface.ComponentContainer.Components[0];


    selectionService 
= surface.GetService(typeof(ISelectionService)) as ISelectionService;
    selectionService.SelectionChanged 
+= new EventHandler(selectionService_SelectionChanged);
}

 

我们以删除功能来做测试,在主窗体的设计界面上添加一个MenuStrip,  MenuStrip上添加Edit菜单项,在Edit菜单项下添加子菜单Delete, 设置其快捷键为Del, Delete菜单项写事件代码:

private   void  deleteToolStripMenuItem_Click( object  sender, EventArgs e)
{
    menuCommandService.GlobalInvoke(StandardCommands.Delete);
}


运行方案,在设计器上添加几个控件,然后在选中一个或几个控件,按下Del键或者点击菜单Edit->Delete, 所选的控件就会被删除。

 其它的菜单命令如全选,如对齐等等,皆可如此实现。StandardCommands包含的命令实在太多了。

 我们的设计器除了序列化资源、生成代码、事件处理这三项功能没有实现外,其它的都已经大功告成。

最近我会比较忙,剩下的内容要过段时间才会写出来,谢谢一直支持鼓励我的各位朋友们!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值