65.9K
CodeProject 正在变化。 阅读更多。
Home

DialogForm - 一个扩展的 WinForms 类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.65/5 (18投票s)

2011年1月16日

CPOL

3分钟阅读

viewsIcon

44477

downloadIcon

2735

易于使用的 WinForms 类, 用于创建扩展对话框

DialogForm

引言

我将在 CodeProject 上发表我的第一篇文章,主题有点枯燥:对话框。我们都知道设计对话框的烦人之处(定位、锚定、对齐适当的按钮,同时保持一致的 GUI 设计)。为此,我实现了 DialogForm 类,它应该能帮助我们更轻松地完成这项任务。

其背后的概念是程序化地创建和处理标准对话框按钮,并节省处理按钮定位或忘记 ShowInTaskBar 标志的时间。

优点

  • 自动设置对话框要求的 FORM 属性(ShowInTaskBar, MinimizeBox, MaximizeBox, AcceptButton, CancelButton, KeyPreview
  • 自动定位、锚定、对齐和选项卡排序标准按钮
  • 标准对话框按钮的自动 'DialogBoxResult' 映射
  • 一个可配置的对话框标题,带有标题文本、描述文本和背景图像
  • 一个可配置的对话框页脚,提供各种预定义的对话框按钮类型
  • DialogForm 类派生您自己的对话框,确保一致的 GUI 设计

简化一项任务通常会导致失去灵活性。对于此实现也是如此,但对于一个开发团队来说,更容易实现一致的 GUI 设计。所以这里有一些 限制

  • 常用的对话框按钮(确定、取消、关闭、是、否、下一步、后退、完成、重置)是预定义的,另外还有两个“可定制”的按钮
  • 此实现可能无法满足依赖于语言的对话框要求(例如东方或阿拉伯语)
  • 目前,没有算法来计算可调整大小的对话框的适当 MinimumSize(这意味着我们必须设置 MinimumSize 属性,以确保在非常小的对话框上页脚与标题重叠)

Using the Code

在您的项目中引用 DialogForm 程序集。

不要从 System.Windows.Forms 派生您的对话框,而是从 Dialog.DialogForm 派生,例如

public partial class DerivedDialogForm : Dialog.DialogForm
{
    public DerivedDialogForm()
    {
        InitializeComponent();
    }
}

您现在可以根据您的要求设置 DialogForm 属性。

DesignerSupport

公共 DialogForm 属性的描述

  • public ButtonsType Buttons:指定要显示的按钮。
    提示:这可能是一个组合的枚举值。对于自定义按钮,以编程方式使用此属性。例如
     public partial class DerivedDialogForm : Dialog.DialogForm
      {
        public DerivedDialogForm()
        {
          InitializeComponent();
          // This creates a Reset Button and 2 customized buttons
          Buttons = ButtonsType.Reset | ButtonsType.Customized1 | ButtonsType.Customized2;
          // aligning the reset Button to the left
          SetButtonLeftAligned(ButtonsType.Reset);
          // rename the button text to "default"
          SetButtonText(ButtonsType.Reset, "Default");
          // rename the 1. customized button text to "import"
          SetButtonText(ButtonsType.Customized1, "&Import");
          // rename the 2. customized button text to "export"
          SetButtonText(ButtonsType.Customized2, "&Export");
        }
      }
  • public string HeaderText 设置标题文本 string
  • public Image HeaderImage 设置标题背景图像
  • public string DescriptionText 设置标题描述 string
  • public bool DisabledButtons 将此设置为 true 以实现初始禁用的按钮
  • public bool ShowFooter 启用/禁用对话框页脚
  • public bool ShowHeader 启用/禁用对话框标题

public DialogForm 方法的描述

  • bool IsButtonEnabled(ButtonsType buttonType) 获取一个值,指示是否启用了按钮。
  • void SetButtonState(ButtonsType buttonType, bool enabled) 将指定的按钮设置为指定的启用状态。
  • void SetButtonText(ButtonsType buttonType, string text) 将指定的按钮设置为指定的文本。
  • void SetButtonLeftAligned 设置指定的按钮左对齐。

public DialogForm 事件的描述

  • public event EventHandler ButtonClicked 按钮点击事件。
    提示:发送者包含点击的按钮,其 Tag 属性包含 Dialog.DialogForm.ButtonsType 值,用于标识点击了哪个按钮,接收器示例
    void dlg_ButtonClicked(object sender, EventArgs e)
    {
        Console.WriteLine(string.Format("Button {0} clicked"
          , (DialogForm.ButtonsType)(((Button)sender).Tag))
          );
    }

可以在 DialogForm 上进行的可能扩展

  • 从派生的可调整大小的对话框中检测 MinimumSize 属性的算法将很有用!
  • 公开 DialogForm 的更多属性(颜色、字体等)
  • 我知道 CodeProject 上有一篇关于 Windows Forms 向导实现的优秀文章,但根据要求,我可以再写一篇关于在此 DialogForm 基类上实现 Windows Forms 向导的文章。

关注点

  • 当然,您想要修改对话框标题绘图代码,所以在 DialogForm.PanelHeader.OnPaint(...) 中执行此操作
  • 有一个固定的按钮顺序,要更改它,您可以根据您的喜好编辑 DialogForm.InitializeButtons() 方法。
  • 预定义的按钮集是以下枚举(修改此枚举后,不要忘记调整 DialogForm.InitializeButtons() 方法)。
    /// <summary>
    /// Predefined button types.
    /// 
    /// Any combination is allowed.
    /// </summary>
    [Flags]
    public enum ButtonsType
    {
      Ok=0x01,
      Cancel=0x02,
      Close = 0x04,
      Yes = 0x08,
      No = 0x10,
      Back = 0x20,
      Next = 0x40,
      Finish = 0x80,
      Reset = 0x100,
      Default = 0x200,
      Customized1 = 0x400,
      Customized2 = 0x800,
      /// <summary>Standard ok/cancel button set </summary>
      OkCancel = Ok | Cancel,
      /// <summary>Standard yes/no button set</summary>
      YesNo = Yes | No,
      /// <summary>Standard wizard button set </summary>
      Wizard = Back | Next | Finish | Cancel,
    }

历史

  • 2011/01/16 初始版本
© . All rights reserved.