科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件用.NET Framework 2.0创建Form设计器

用.NET Framework 2.0创建Form设计器

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

Microsoft .NET Framework 1.0提供了一个非常通用的设计时框架,但是没有提供任何实现代码来完成一个设计器

作者:佚名 来源:blog 2007年11月3日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
DesignSurface 和DesignSurfaceManager

  .NET Framework 2.0引入了两个类DesignSurface 和DesignSurfaceManager.给设计器提供宿主以及给设计器提供服务。DesignSurface是使用者所感知的设计器,它是UI使用者操纵改变设计时特征,DesignSurface 可能被当作一个单独的设计者使用或者和DesignSurfaceManager结合使用为设计器应用程序提供多个DesignSurface。

  DesignSurface提供好几个设计时服务,大多数服务都可以在服务容器中被覆盖,替换不可替换的服务是非法的,因为他们之间彼此仰赖.注意添加到Service Containe实现了接口IDisposabler的服务当DesignSurface 销毁的时候都会被销毁。

  除了提供缺省的服务,DesignSurface也提供了IDictionaryService,此服务提供一个使用关联键设置、检索和查找对象的简单接口。不可能替换这些服务因为在每个站点上无法替换这些服务。

  DesignSurfaceManager是设计器的容器,它提供通用的服务以处理在设计者,属性窗口和其他的全局对象之间的事件路由. 使用 DesignSurfaceManager 是可选择的, 但是如果你想需要有一组设计者窗口,推荐使用DesignSurfaceManager。

  DesignSurfaceManager也提供了几个设计时服务(see Figure 5).。每一个都可以在Protected属性ServiceContainer(服务容器)中被覆盖。和DesignSurface一样,DesignSurfaceManager所有的 实现了接口IDisposabler的服务 当设计器应用程序销毁的时候都会被销毁。

  IDesignerEventService 是一个特别地有用的服务. 当一个设计器变成活跃的时候 , 它允许一个设计器应用程序被通知到. IDesignerEventService 提供了一组设计器和全局对象的访问点, 例如属性窗口能够侦听到选择变化事件.

  宿主form

  为了示范一下宿主一个设计器是多么简单,我写了下面的简单代码来创建一个基本的Windows? Forms designer并显示它:

// Create the DesignSurface and load it with a form

DesignSurface ds = new DesignSurface();
ds.BeginLoad(typeof(Form));

// Get the View of the DesignSurface, host it in a form, and show it

Control c = ds.View as Control;
Form f = new Form();
c.Parent = f;
c.Dock = DockStyle.Fill;
f.Show();

  在这一个代码片断中,我已经用Form方式装载 DesignSurface. 同样地,你能用拥有根设计器的任何组件装载 DesignSurface. 举例来说,你可以改为装载 UserControl 或一个组件.


Figure 6 Hosting Windows Forms Designer

  提供下载的例子代码中有四种根组件:Form, UserControl, Component, and MyTopLevelComponent (一个图形设计器). 当你运行例子的时候,一个Shell UI 将会打开. 它包括一个工具箱,一个属性窗口, 一个tab Control来宿主设计器,一个Output window和一个Solution Explorer,如图6所示..使用菜单的File | New | Form 用窗口打开一个新的Windows Forms Designer。这本质上就是使用上面所展示的代码加载一个设计器。与装载一个Form相比较,例子中还展示了如何装载UserControl或者组件。

  创建一个根组件,也就是创建一个设计器实现IRootDesigner接口,然后指定这个组件的designer相关联,根组件的视图属性将呈现给使用者。

  DesignSurface 提供的主要服务之一是 IDesignerHost,IDesignerHost是用于提供设计器和对类型、服务和事务控制的主要接口。它也用于创建和销毁组件。添加一个按钮到Windows Forms designer所要做的工作就是从DesignSurface获得IDesignerHost接口并创建button,代码如图7

// Add a Button to the Form
IDesignerHost idh = (IDesignerHost)ds.GetService(typeof(IDesignerHost));
Button b = (Button)idh.CreateComponent(typeof(Button));
// Set the Parent of this Button to the RootComponent (the Form)
b.Parent = (Form)idh.RootComponent;
// Use ComponentChangeService to announce changing of the
// Form's Controls collection */
IComponentChangeService icc = (IComponentChangeService) idh.GetService(typeof(IComponentChangeService));
icc.OnComponentChanging(idh.RootComponent, TypeDescriptor.GetProperties(idh.RootComponent)["Controls");

  ItoolboxUser指定设计器支持从Toolbox中增加控件到设计器,这意味着你确实需要一个实现ToolboxService的Toolbox,你能够用IToolboxUser接口把控件添加到根组件。例如:

/* Add a Button to the Form using IToolboxUser */

IDesignerHost idh = (IDesignerHost)ds.GetService(typeof(IDesignerHost));
IToolboxUser itu = (IToolboxUser)idh.GetDesigner(idh.RootComponent);
itu.ToolPicked(new ToolboxItem(typeof(Button)));


Figure 8 Custom RootDesigner Updates

  例子程序中双击Toolbox中的控件,控件被添加到自定义的根设计器,根设计器的视图中显示一个pie chart如图8所示,点击GraphStyle链接改变视图到bar graph.

  工具箱

  MyRootDesigner实现IToolboxUser接口,这个接口有两个方法:GetToolSupported and ToolPicked. 你能使用 GetToolSupported 过滤项目能被填加到设计器上的组件. 进入ToolboxItem 的 CreateComponents 方法 (如名字应用,负责创造组件) 调用的时候调用ToolPicked。

  既然我们已经成功添加控件和组件到设计器,让我们来看一下如何实现一个Toolbox。首先,你的工具箱需要实现 IToolboxService —这一个服务被增加到服务容器,任何需要使用的任何人都可以被存取。

  允许项目从工具箱通过老鼠或键盘的添加到设计器上,示例程序的工具箱处理KeyDown 和 MouseDown 事件。Enter键或鼠标双击事件, IToolboxUser.ToolPicked 被调用. 示例展示了鼠标单击拖动控件如何序列化ToolboxItem 到 DataObject和DoDragDrop方法调用,鼠标mouse up事件IToolboxService.SerializeToolboxItem被调用,而且项目将会被增加到设计器.

  当一个控件或者组件被添加到设计器,你能藉由实现 INameCreationService 提供一个定制的名字给组件,示例程序展示了CreateName, ValidateName, and IsValidName的代码实现。
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章