// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED. // THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License. // LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License") // YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License. // YOU MAY OBTAIN A COPY OF THE LICENSE AT // // http://www.apache.org/licenses/LICENSE-2.0 // // UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE // DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. // SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND // LIMITATIONS UNDER THE License. // GITEE: https://gitee.com/antdui/AntdUI // GITHUB: https://github.com/AntdUI/AntdUI // CSDN: https://blog.csdn.net/v_132 // QQ: 17379620 using System; using System.Drawing; using System.Windows.Forms; namespace AntdUI { /// /// Modal 对话框 /// /// 模态对话框。 public static class Modal { /// /// Model 对话框 /// /// 所属窗口 /// 标题 /// 内容 public static DialogResult open(Form form, string title, string content) { return open(new Config(form, title, content)); } /// /// Model 对话框 /// /// 所属窗口 /// 标题 /// 内容 public static DialogResult open(Form form, string title, object content) { return open(new Config(form, title, content)); } /// /// Model 对话框 /// /// 所属窗口 /// 标题 /// 内容 /// 图标 public static DialogResult open(Form form, string title, string content, TType icon) { return open(new Config(form, title, content, icon)); } /// /// Model 对话框 /// /// 配置 public static DialogResult open(this Config config) { if (config.Form == null) { config.Mask = config.MaskClosable = false; return new LayeredFormModal(config).ShowDialog(config.Form); } if (!config.Form.IsHandleCreated) config.Mask = config.MaskClosable = false; if (config.Form.InvokeRequired) { var dialog = DialogResult.None; config.Form.Invoke(new Action(() => { dialog = open(config); })); return dialog; } var frm = new LayeredFormModal(config); if (config.Mask) { bool isclose = false; var formMask = config.Form.FormMask(); frm.FormClosed += (s1, e1) => { if (isclose) return; isclose = true; formMask.IClose(); }; return frm.ShowDialog(formMask); } else return frm.ShowDialog(config.Form); } #region 配置 /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 内容 public static Config config(Form form, string title, string content) { return new Config(form, title, content); } /// /// Model 配置 /// /// 标题 /// 内容 public static Config config(string title, string content) { return new Config(title, content); } /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 控件/Obj public static Config config(Form form, string title, object content) { return new Config(form, title, content); } /// /// Model 配置 /// /// 标题 /// 控件/Obj public static Config config(string title, object content) { return new Config(title, content); } /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 内容 /// 图标 public static Config config(Form form, string title, string content, TType icon) { return new Config(form, title, content, icon); } /// /// Model 配置 /// /// 标题 /// 内容 /// 图标 public static Config config(string title, string content, TType icon) { return new Config(title, content, icon); } /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 控件/Obj /// 图标 public static Config config(Form form, string title, object content, TType icon) { return new Config(form, title, content, icon); } /// /// Model 配置 /// /// 标题 /// 控件/Obj /// 图标 public static Config config(string title, object content, TType icon) { return new Config(title, content, icon); } #endregion /// /// 配置 /// public class Config { /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 内容 public Config(Form form, string title, string content) { Form = form; Title = title; Content = content; } /// /// Model 配置 /// /// 标题 /// 内容 public Config(string title, string content) { Mask = MaskClosable = false; Title = title; Content = content; } /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 控件/内容 public Config(Form form, string title, object content) { Form = form; Title = title; Content = content; } /// /// Model 配置 /// /// 标题 /// 控件/内容 public Config(string title, object content) { Mask = MaskClosable = false; Title = title; Content = content; } /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 内容 /// 图标 public Config(Form form, string title, string content, TType icon) { Form = form; Title = title; Content = content; Icon = icon; } /// /// Model 配置 /// /// 标题 /// 内容 /// 图标 public Config(string title, string content, TType icon) { Mask = MaskClosable = false; Title = title; Content = content; Icon = icon; } /// /// Model 配置 /// /// 所属窗口 /// 标题 /// 控件/内容 /// 图标 public Config(Form form, string title, object content, TType icon) { Form = form; Title = title; Content = content; Icon = icon; } /// /// Model 配置 /// /// 标题 /// 控件/内容 /// 图标 public Config(string title, object content, TType icon) { Mask = MaskClosable = false; Title = title; Content = content; Icon = icon; } /// /// 所属窗口 /// public Form? Form { get; set; } /// /// 标题 /// public string Title { get; set; } /// /// 控件/内容 /// public object Content { get; set; } /// /// 消息框宽度 /// public int Width { get; set; } = 416; /// /// 字体 /// public Font? Font { get; set; } /// /// 是否支持键盘 esc 关闭 /// public bool Keyboard { get; set; } = true; /// /// 是否展示遮罩 /// public bool Mask { get; set; } = true; /// /// 点击蒙层是否允许关闭 /// public bool MaskClosable { get; set; } = true; /// /// 是否显示关闭图标 /// public bool CloseIcon { get; set; } = false; /// /// 取消按钮字体 /// public Font? CancelFont { get; set; } /// /// 确认按钮字体 /// public Font? OkFont { get; set; } /// /// 按钮栏高度 /// public int BtnHeight { get; set; } = 38; /// /// 边距 /// public Size Padding { get; set; } = new Size(24, 20); string? canceltext = Localization.Provider?.GetLocalizedString("Cancel") ?? "取消"; /// /// 取消按钮文字 /// public string? CancelText { get => canceltext; set { if (canceltext == value) return; canceltext = value; Layered?.SetCancelText(value); } } string oktext = Localization.Provider?.GetLocalizedString("OK") ?? "确定"; /// /// 确认按钮文字 /// public string OkText { get => oktext; set { if (oktext == value) return; oktext = value; Layered?.SetOkText(value); } } internal LayeredFormModal? Layered = null; /// /// 确认按钮类型 /// public TTypeMini OkType { get; set; } = TTypeMini.Primary; /// /// 图标 /// public TType Icon { get; set; } = TType.None; /// /// 确定回调 /// public Func? OnOk { get; set; } /// /// 用户定义数据 /// public object? Tag { get; set; } /// /// 加载时禁用取消按钮 /// public bool LoadingDisableCancel { get; set; } #region 自定义按钮 /// /// 自定义按钮 /// public Btn[]? Btns { get; set; } /// /// 自定义按钮回调 /// public Func? OnBtns { get; set; } #endregion /// /// 自定义按钮样式回调 /// public Action? OnButtonStyle { get; set; } } /// /// 按钮 /// public class Btn { /// /// 自定义按钮 /// /// 按钮名称 /// 按钮文字 /// 按钮类型 public Btn(string name, string text, TTypeMini type = TTypeMini.Default) { Name = name; Text = text; Type = type; } /// /// 自定义按钮 /// /// 按钮名称 /// 按钮文字 /// 字体颜色 /// 背景颜色 /// 按钮类型 public Btn(string name, string text, Color fore, Color back, TTypeMini type = TTypeMini.Default) { Name = name; Text = text; Fore = fore; Back = back; Type = type; } /// /// 按钮名称 /// public string Name { get; set; } /// /// 按钮文字 /// public string Text { get; set; } /// /// 按钮类型 /// public TTypeMini Type { get; set; } /// /// 文字颜色 /// public Color? Fore { get; set; } /// /// 背景颜色 /// public Color? Back { get; set; } /// /// 用户定义数据 /// public object? Tag { get; set; } } /// /// 多行文本 /// public class TextLine { public TextLine(string text) { Text = text; } public TextLine(string text, int gap) { Text = text; Gap = gap; } public TextLine(string text, int gap, Color fore) { Text = text; Gap = gap; Fore = fore; } public TextLine(string text, Color fore) { Text = text; Fore = fore; } /// /// 文字 /// public string Text { get; set; } /// /// 间距 /// public int Gap { get; set; } /// /// 文字颜色 /// public Color? Fore { get; set; } /// /// 字体 /// public Font? Font { get; set; } } } }