* UIFormHelper: 提问弹窗增加可默认是确认或者取消按钮的选择

This commit is contained in:
Sunny 2023-07-27 17:18:00 +08:00
parent c5251848cc
commit ba4014f780
6 changed files with 188 additions and 157 deletions

View File

@ -1971,9 +1971,9 @@ namespace Sunny.UI
/// <param name="msg">信息</param>
/// <param name="showMask">显示遮罩层</param>
/// <returns>结果</returns>
public bool ShowAskDialog(string msg, bool showMask = false)
public bool ShowAskDialog(string msg, UIMessageDialogButtons defaultButton = UIMessageDialogButtons.Ok, bool showMask = false)
{
return UIMessageDialog.ShowMessageDialog(msg, UILocalize.AskTitle, true, UIStyle.Blue, showMask, true);
return UIMessageDialog.ShowMessageDialog(msg, UILocalize.AskTitle, true, UIStyle.Blue, showMask, true, defaultButton);
}
/// <summary>
@ -2032,9 +2032,9 @@ namespace Sunny.UI
/// <param name="style">主题</param>
/// <param name="showMask">显示遮罩层</param>
/// <returns>结果</returns>
public bool ShowAskDialog(string title, string msg, UIStyle style = UIStyle.Blue, bool showMask = false)
public bool ShowAskDialog(string title, string msg, UIMessageDialogButtons defaultButton = UIMessageDialogButtons.Ok, UIStyle style = UIStyle.Blue, bool showMask = false)
{
return UIMessageDialog.ShowMessageDialog(msg, title, true, style, showMask, true);
return UIMessageDialog.ShowMessageDialog(msg, title, true, style, showMask, true, defaultButton);
}
/// <summary>

View File

@ -23,6 +23,7 @@
* 2021-12-13: V3.0.9 Form的ShowDialogWithMask()
* 2022-07-17: V3.2.1 ShowNotifier打开多个
* 2023-07-27: V3.4.1 TopMost为true
* 2023-07-27: V3.4.1
******************************************************************************/
using System;
@ -86,52 +87,7 @@ namespace Sunny.UI
/// <param name="style">主题</param>
public static void ShowSuccessDialog(this Form form, string msg, UIStyle style = UIStyle.Green)
{
form.ShowMessageDialog(msg, UILocalize.SuccessTitle, false, style);
}
/// <summary>
/// 信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style">主题</param>
public static void ShowInfoDialog(this Form form, string msg, UIStyle style = UIStyle.Gray)
{
form.ShowMessageDialog(msg, UILocalize.InfoTitle, false, style);
}
/// <summary>
/// 警告信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style">主题</param>
public static void ShowWarningDialog(this Form form, string msg, UIStyle style = UIStyle.Orange)
{
form.ShowMessageDialog(msg, UILocalize.WarningTitle, false, style);
}
/// <summary>
/// 错误信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style">主题</param>
public static void ShowErrorDialog(this Form form, string msg, UIStyle style = UIStyle.Red)
{
form.ShowMessageDialog(msg, UILocalize.ErrorTitle, false, style);
}
/// <summary>
/// 确认信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style"></param>
/// <returns>结果</returns>
public static bool ShowAskDialog(this Form form, string msg, UIStyle style = UIStyle.Blue)
{
return form.ShowMessageDialog(msg, UILocalize.AskTitle, true, style);
form.ShowSuccessDialog(UILocalize.SuccessTitle, msg, style);
}
/// <summary>
@ -146,6 +102,17 @@ namespace Sunny.UI
form.ShowMessageDialog(msg, title, false, style);
}
/// <summary>
/// 信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style">主题</param>
public static void ShowInfoDialog(this Form form, string msg, UIStyle style = UIStyle.Gray)
{
form.ShowInfoDialog(UILocalize.InfoTitle, msg, style);
}
/// <summary>
/// 信息提示框
/// </summary>
@ -158,6 +125,17 @@ namespace Sunny.UI
form.ShowMessageDialog(msg, title, false, style);
}
/// <summary>
/// 警告信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style">主题</param>
public static void ShowWarningDialog(this Form form, string msg, UIStyle style = UIStyle.Orange)
{
form.ShowWarningDialog(UILocalize.WarningTitle, msg, style);
}
/// <summary>
/// 警告信息提示框
/// </summary>
@ -170,6 +148,17 @@ namespace Sunny.UI
form.ShowMessageDialog(msg, title, false, style);
}
/// <summary>
/// 错误信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style">主题</param>
public static void ShowErrorDialog(this Form form, string msg, UIStyle style = UIStyle.Red)
{
form.ShowErrorDialog(UILocalize.ErrorTitle, msg, style);
}
/// <summary>
/// 错误信息提示框
/// </summary>
@ -182,6 +171,18 @@ namespace Sunny.UI
form.ShowMessageDialog(msg, title, false, style);
}
/// <summary>
/// 确认信息提示框
/// </summary>
/// <param name="form">窗体</param>
/// <param name="msg">信息</param>
/// <param name="style"></param>
/// <returns>结果</returns>
public static bool ShowAskDialog(this Form form, string msg, UIMessageDialogButtons defaultButton = UIMessageDialogButtons.Ok, UIStyle style = UIStyle.Blue)
{
return form.ShowAskDialog(UILocalize.AskTitle, msg, defaultButton, style);
}
/// <summary>
/// 确认信息提示框
/// </summary>
@ -190,7 +191,7 @@ namespace Sunny.UI
/// <param name="msg">信息</param>
/// <param name="style"></param>
/// <returns>结果</returns>
public static bool ShowAskDialog(this Form form, string title, string msg, UIStyle style = UIStyle.Blue)
public static bool ShowAskDialog(this Form form, string title, string msg, UIMessageDialogButtons defaultButton = UIMessageDialogButtons.Ok, UIStyle style = UIStyle.Blue)
{
return form.ShowMessageDialog(msg, title, true, style);
}
@ -216,11 +217,21 @@ namespace Sunny.UI
/// <param name="showMask">显示遮罩层</param>
/// <param name="topMost">置顶</param>
/// <returns>结果</returns>
public static bool ShowMessageDialog(string message, string title, bool showCancelButton, UIStyle style, bool showMask = true, bool topMost = true)
public static bool ShowMessageDialog(string message, string title, bool showCancelButton, UIStyle style, bool showMask = true, bool topMost = true, UIMessageDialogButtons defaultButton = UIMessageDialogButtons.Ok)
{
Point pt = SystemEx.GetCursorPos();
Rectangle screen = Screen.GetBounds(pt);
UIMessageForm frm = new UIMessageForm();
if (showCancelButton)
{
frm.DefaultButton = defaultButton;
}
else
{
frm.DefaultButton = UIMessageDialogButtons.Ok;
}
frm.StartPosition = FormStartPosition.CenterScreen;
frm.ShowMessage(message, title, showCancelButton, style);
frm.ShowInTaskbar = false;
@ -237,6 +248,12 @@ namespace Sunny.UI
}
}
public enum UIMessageDialogButtons
{
Ok,
Cancel
}
public static class UIMessageBox
{
public static void Show(string text, bool showMask = true, bool topMost = true)

View File

@ -28,85 +28,82 @@
/// </summary>
private void InitializeComponent()
{
this.btnCancel = new Sunny.UI.UIButton();
this.btnOK = new Sunny.UI.UIButton();
this.lbMsg = new Sunny.UI.UIRichTextBox();
this.SuspendLayout();
btnCancel = new UIButton();
btnOK = new UIButton();
lbMsg = new UIRichTextBox();
SuspendLayout();
//
// btnCancel
//
this.btnCancel.BackColor = System.Drawing.Color.Transparent;
this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 12F);
this.btnCancel.Location = new System.Drawing.Point(224, 220);
this.btnCancel.Margin = new System.Windows.Forms.Padding(0);
this.btnCancel.MinimumSize = new System.Drawing.Size(1, 1);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(224, 48);
this.btnCancel.TabIndex = 6;
this.btnCancel.Text = "取消";
this.btnCancel.TipsFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnCancel.TipsText = null;
this.btnCancel.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
this.btnCancel.MouseEnter += new System.EventHandler(this.btnOK_MouseEnter);
this.btnCancel.MouseLeave += new System.EventHandler(this.btnOK_MouseLeave);
btnCancel.BackColor = System.Drawing.Color.Transparent;
btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
btnCancel.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
btnCancel.Location = new System.Drawing.Point(224, 220);
btnCancel.Margin = new System.Windows.Forms.Padding(0);
btnCancel.MinimumSize = new System.Drawing.Size(1, 1);
btnCancel.Name = "btnCancel";
btnCancel.Size = new System.Drawing.Size(224, 48);
btnCancel.TabIndex = 6;
btnCancel.Text = "取消";
btnCancel.TipsFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
btnCancel.TipsText = null;
btnCancel.Click += btnCancel_Click;
btnCancel.MouseEnter += btnOK_MouseEnter;
btnCancel.MouseLeave += btnOK_MouseLeave;
//
// btnOK
//
this.btnOK.BackColor = System.Drawing.Color.Transparent;
this.btnOK.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnOK.Font = new System.Drawing.Font("微软雅黑", 12F);
this.btnOK.Location = new System.Drawing.Point(2, 220);
this.btnOK.Margin = new System.Windows.Forms.Padding(0);
this.btnOK.MinimumSize = new System.Drawing.Size(1, 1);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(223, 48);
this.btnOK.TabIndex = 5;
this.btnOK.Text = "确定";
this.btnOK.TipsFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnOK.TipsText = null;
this.btnOK.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
this.btnOK.MouseEnter += new System.EventHandler(this.btnOK_MouseEnter);
this.btnOK.MouseLeave += new System.EventHandler(this.btnOK_MouseLeave);
btnOK.BackColor = System.Drawing.Color.Transparent;
btnOK.Cursor = System.Windows.Forms.Cursors.Hand;
btnOK.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
btnOK.Location = new System.Drawing.Point(2, 220);
btnOK.Margin = new System.Windows.Forms.Padding(0);
btnOK.MinimumSize = new System.Drawing.Size(1, 1);
btnOK.Name = "btnOK";
btnOK.Size = new System.Drawing.Size(223, 48);
btnOK.TabIndex = 5;
btnOK.Text = "确定";
btnOK.TipsFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
btnOK.TipsText = null;
btnOK.Click += btnOK_Click;
btnOK.MouseEnter += btnOK_MouseEnter;
btnOK.MouseLeave += btnOK_MouseLeave;
//
// lbMsg
//
this.lbMsg.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
this.lbMsg.FillColor = System.Drawing.Color.White;
this.lbMsg.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbMsg.Location = new System.Drawing.Point(14, 50);
this.lbMsg.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.lbMsg.MinimumSize = new System.Drawing.Size(1, 1);
this.lbMsg.Name = "lbMsg";
this.lbMsg.Padding = new System.Windows.Forms.Padding(2);
this.lbMsg.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
this.lbMsg.ReadOnly = true;
this.lbMsg.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None;
this.lbMsg.ShowText = false;
this.lbMsg.Size = new System.Drawing.Size(422, 158);
this.lbMsg.TabIndex = 7;
this.lbMsg.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
this.lbMsg.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
lbMsg.BackColor = System.Drawing.Color.FromArgb(235, 243, 255);
lbMsg.FillColor = System.Drawing.Color.White;
lbMsg.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
lbMsg.Location = new System.Drawing.Point(14, 50);
lbMsg.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
lbMsg.MinimumSize = new System.Drawing.Size(1, 1);
lbMsg.Name = "lbMsg";
lbMsg.Padding = new System.Windows.Forms.Padding(2);
lbMsg.RadiusSides = UICornerRadiusSides.None;
lbMsg.ReadOnly = true;
lbMsg.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None;
lbMsg.ShowText = false;
lbMsg.Size = new System.Drawing.Size(422, 158);
lbMsg.TabIndex = 7;
lbMsg.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
//
// UIMessageForm
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(450, 270);
this.Controls.Add(this.lbMsg);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.EscClose = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "UIMessageForm";
this.Padding = new System.Windows.Forms.Padding(1, 35, 1, 3);
this.ShowInTaskbar = false;
this.Text = "UIMsgBox";
this.ZoomScaleRect = new System.Drawing.Rectangle(15, 15, 450, 270);
this.ResumeLayout(false);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
ClientSize = new System.Drawing.Size(450, 270);
Controls.Add(lbMsg);
Controls.Add(btnCancel);
Controls.Add(btnOK);
EscClose = true;
MaximizeBox = false;
MinimizeBox = false;
Name = "UIMessageForm";
Padding = new System.Windows.Forms.Padding(1, 35, 1, 3);
ShowInTaskbar = false;
Text = "UIMsgBox";
ZoomScaleRect = new System.Drawing.Rectangle(15, 15, 450, 270);
Shown += UIMessageForm_Shown;
ResumeLayout(false);
}
#endregion

View File

@ -100,7 +100,10 @@ namespace Sunny.UI
protected override void DoEnter()
{
base.DoEnter();
btnOK_Click(null, null);
if (!ShowCancel)
{
btnOK_Click(null, null);
}
}
private void btnOK_Click(object sender, EventArgs e)
@ -125,13 +128,6 @@ namespace Sunny.UI
{
base.SetStyleColor(uiColor);
if (btnOK != null)
{
btnOK.FillColor = BackColor;
btnOK.RectColor = Color.FromArgb(36, uiColor.ButtonRectColor);
btnOK.ForeColor = uiColor.LabelForeColor;
}
if (btnCancel != null)
{
btnCancel.FillColor = BackColor;
@ -139,6 +135,13 @@ namespace Sunny.UI
btnCancel.ForeColor = uiColor.LabelForeColor;
}
if (btnOK != null)
{
btnOK.FillColor = BackColor;
btnOK.RectColor = Color.FromArgb(36, uiColor.ButtonRectColor);
btnOK.ForeColor = uiColor.LabelForeColor;
}
if (lbMsg != null)
{
lbMsg.ForeColor = uiColor.LabelForeColor;
@ -149,6 +152,8 @@ namespace Sunny.UI
}
}
public UIMessageDialogButtons DefaultButton { get; set; } = UIMessageDialogButtons.Ok;
private void btnOK_MouseEnter(object sender, EventArgs e)
{
//((UIButton)sender).RadiusSides = UICornerRadiusSides.All;
@ -175,5 +180,17 @@ namespace Sunny.UI
//btnOK.ShowFocusLine = btnCancel.ShowFocusLine = showCancel;
btnOK.ShowFocusColor = btnCancel.ShowFocusColor = showCancel;
}
private void UIMessageForm_Shown(object sender, EventArgs e)
{
if (!ShowCancel || DefaultButton == UIMessageDialogButtons.Ok)
{
btnOK.Focus();
}
else
{
btnCancel.Focus();
}
}
}
}

View File

@ -18,7 +18,7 @@
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>

View File

@ -1233,9 +1233,9 @@ namespace Sunny.UI
/// <param name="msg">信息</param>
/// <param name="showMask">显示遮罩层</param>
/// <returns>结果</returns>
public bool ShowAskDialog(string msg, bool showMask = false)
public bool ShowAskDialog(string msg, UIMessageDialogButtons defaultButton = UIMessageDialogButtons.Ok, bool showMask = false)
{
return UIMessageDialog.ShowMessageDialog(msg, UILocalize.AskTitle, true, UIStyle.Blue, showMask, true);
return UIMessageDialog.ShowMessageDialog(msg, UILocalize.AskTitle, true, UIStyle.Blue, showMask, true, defaultButton);
}
/// <summary>
@ -1294,9 +1294,9 @@ namespace Sunny.UI
/// <param name="style">主题</param>
/// <param name="showMask">显示遮罩层</param>
/// <returns>结果</returns>
public bool ShowAskDialog(string title, string msg, UIStyle style = UIStyle.Blue, bool showMask = false)
public bool ShowAskDialog(string title, string msg, UIMessageDialogButtons defaultButton = UIMessageDialogButtons.Ok, UIStyle style = UIStyle.Blue, bool showMask = false)
{
return UIMessageDialog.ShowMessageDialog(msg, title, true, style, showMask, true);
return UIMessageDialog.ShowMessageDialog(msg, title, true, style, showMask, true, defaultButton);
}
/// <summary>