From 960d3665de0bf728951db6463eb5c513adb609ca Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 28 Apr 2024 14:29:46 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIForm:=20=E5=A2=9E=E5=8A=A0WindowStateChan?= =?UTF-8?q?ged=E4=BA=8B=E4=BB=B6=20*=20UIForm2:=20=E5=A2=9E=E5=8A=A0Window?= =?UTF-8?q?StateChanged=E4=BA=8B=E4=BB=B6=20*=20UIPage:=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0WindowStateChanged=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Forms/UIForm.cs | 41 ++++++++++++++++ SunnyUI/Forms/UIForm2.cs | 38 +++++++++++++++ SunnyUI/Forms/UIFormHelper.cs | 2 + SunnyUI/Forms/UIMessageForm.Designer.cs | 3 ++ SunnyUI/Forms/UIMessageForm.cs | 5 ++ SunnyUI/Frames/UIPage.cs | 64 ++++++++++++++----------- SunnyUI/Style/UIStyles.cs | 8 ++-- 7 files changed, 129 insertions(+), 32 deletions(-) diff --git a/SunnyUI/Forms/UIForm.cs b/SunnyUI/Forms/UIForm.cs index b84a7688..bb80af42 100644 --- a/SunnyUI/Forms/UIForm.cs +++ b/SunnyUI/Forms/UIForm.cs @@ -55,6 +55,7 @@ * 2023-12-13: V3.6.2 优化UIPage的Init和Final加载逻辑 * 2023-02-19: V3.6.3 修改标题栏文字与控制按钮绘制重叠的问题 * 2024-02-22: V3.6.3 最大化时,鼠标拖拽标题超过一定范围后再恢复Normal显示 + * 2024-04-28: V3.6.5 增加WindowStateChanged事件 ******************************************************************************/ using System; @@ -115,6 +116,24 @@ namespace Sunny.UI set => base.AutoScroll = false; } + public event OnWindowStateChanged WindowStateChanged; + + private void DoWindowStateChanged(FormWindowState thisState) + { + lastWindowState = thisState; + DoWindowStateChanged(thisState, WindowState); + } + + private void DoWindowStateChanged(FormWindowState thisState, FormWindowState lastState) + { + WindowStateChanged?.Invoke(this, thisState, lastState); + + foreach (var page in UIStyles.Pages.Values) + { + page.DoWindowStateChanged(thisState, lastState); + } + } + /// /// 禁止控件跟随窗体缩放 /// @@ -652,6 +671,7 @@ namespace Sunny.UI if (InMinBox) { InMinBox = false; + DoWindowStateChanged(FormWindowState.Minimized); WindowState = FormWindowState.Minimized; } @@ -703,6 +723,7 @@ namespace Sunny.UI // 若窗体从正常模式->最大化模式,该操作是由移动窗体至顶部触发的,记录的是移动前的窗体位置 location = IsOnMoving ? FormLocation : Location; FormEx.SetFormRoundRectRegion(this, 0); + DoWindowStateChanged(FormWindowState.Maximized); WindowState = FormWindowState.Maximized; } else if (WindowState == FormWindowState.Maximized) @@ -725,6 +746,7 @@ namespace Sunny.UI Location = location; FormEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); + DoWindowStateChanged(FormWindowState.Normal); WindowState = FormWindowState.Normal; } @@ -1745,6 +1767,7 @@ namespace Sunny.UI #region 拉拽调整窗体大小 + private FormWindowState lastWindowState = FormWindowState.Normal; protected override void WndProc(ref Message m) { if (m.Msg == Win32.User.WM_ERASEBKGND) @@ -1762,6 +1785,24 @@ namespace Sunny.UI } } + if (m.Msg == Win32.User.WM_ACTIVATE) + { + if (WindowState != FormWindowState.Minimized && lastWindowState == FormWindowState.Minimized) + { + DoWindowStateChanged(WindowState, lastWindowState); + lastWindowState = WindowState; + } + } + + if (m.Msg == Win32.User.WM_ACTIVATEAPP) + { + if (WindowState == FormWindowState.Minimized && lastWindowState != FormWindowState.Minimized) + { + DoWindowStateChanged(WindowState, lastWindowState); + lastWindowState = FormWindowState.Minimized; + } + } + base.WndProc(ref m); if (m.Msg == Win32.User.WM_NCHITTEST && ShowDragStretch && WindowState == FormWindowState.Normal) diff --git a/SunnyUI/Forms/UIForm2.cs b/SunnyUI/Forms/UIForm2.cs index 31172b1d..06e4b53f 100644 --- a/SunnyUI/Forms/UIForm2.cs +++ b/SunnyUI/Forms/UIForm2.cs @@ -19,6 +19,7 @@ * 2024-01-20: V3.6.3 增加文件说明 * 2024-01-25: V3.6.3 增加主题等 * 2024-04-16: V3.6.5 设置默认Padding.Top为TitleHeight + * 2024-04-28: V3.6.5 增加WindowStateChanged事件 ******************************************************************************/ using System; @@ -64,6 +65,24 @@ namespace Sunny.UI public readonly Guid Guid = Guid.NewGuid(); + public event OnWindowStateChanged WindowStateChanged; + + private void DoWindowStateChanged(FormWindowState thisState) + { + lastWindowState = thisState; + DoWindowStateChanged(thisState, WindowState); + } + + private void DoWindowStateChanged(FormWindowState thisState, FormWindowState lastState) + { + WindowStateChanged?.Invoke(this, thisState, lastState); + + foreach (var page in UIStyles.Pages.Values) + { + page.DoWindowStateChanged(thisState, lastState); + } + } + public void Translate() { List controls = this.GetInterfaceControls("ITranslate"); @@ -660,6 +679,7 @@ namespace Sunny.UI if (InMinBox) { InMinBox = false; + DoWindowStateChanged(FormWindowState.Minimized); WindowState = FormWindowState.Minimized; } @@ -690,11 +710,13 @@ namespace Sunny.UI { if (WindowState == FormWindowState.Maximized) { + DoWindowStateChanged(FormWindowState.Normal); WindowState = FormWindowState.Normal; if (Location.Y < 0) Location = new Point(Location.X, 0); } else { + DoWindowStateChanged(FormWindowState.Maximized); WindowState = FormWindowState.Maximized; } } @@ -703,12 +725,14 @@ namespace Sunny.UI if (WindowState == FormWindowState.Maximized) { FormBorderStyle = FormBorderStyle.Sizable; + DoWindowStateChanged(FormWindowState.Normal); WindowState = FormWindowState.Normal; if (Location.Y < 0) Location = new Point(Location.X, 0); } else { FormBorderStyle = FormBorderStyle.None; + DoWindowStateChanged(FormWindowState.Maximized); WindowState = FormWindowState.Maximized; } } @@ -1146,6 +1170,8 @@ namespace Sunny.UI return new Size(width, height); } + private FormWindowState lastWindowState = FormWindowState.Normal; + protected override void WndProc(ref Message m) { var msg = (int)m.Msg; @@ -1154,6 +1180,11 @@ namespace Sunny.UI case Win32.User.WM_ACTIVATE: var margins = new Win32.Dwm.MARGINS(0, 0, 1, 0); Win32.Dwm.DwmExtendFrameIntoClientArea(Handle, ref margins); + if (WindowState != FormWindowState.Minimized && lastWindowState == FormWindowState.Minimized) + { + DoWindowStateChanged(WindowState, lastWindowState); + lastWindowState = WindowState; + } break; case Win32.User.WM_NCCALCSIZE when m.WParam != IntPtr.Zero: if (CalcSize(ref m)) return; @@ -1165,6 +1196,13 @@ namespace Sunny.UI HotKeyEventHandler?.Invoke(this, new HotKeyEventArgs(hotKeys[hotKeyId], DateTime.Now)); } break; + case Win32.User.WM_ACTIVATEAPP: + if (WindowState == FormWindowState.Minimized && lastWindowState != FormWindowState.Minimized) + { + DoWindowStateChanged(WindowState, lastWindowState); + lastWindowState = FormWindowState.Minimized; + } + break; } base.WndProc(ref m); diff --git a/SunnyUI/Forms/UIFormHelper.cs b/SunnyUI/Forms/UIFormHelper.cs index 06d3d09b..f0b41da0 100644 --- a/SunnyUI/Forms/UIFormHelper.cs +++ b/SunnyUI/Forms/UIFormHelper.cs @@ -42,6 +42,8 @@ namespace Sunny.UI public delegate void OnZoomScaleRectChanged(object sender, Rectangle info); + public delegate void OnWindowStateChanged(object sender, FormWindowState thisState, FormWindowState lastState); + public enum UILoginFormFocusControl { UserName, diff --git a/SunnyUI/Forms/UIMessageForm.Designer.cs b/SunnyUI/Forms/UIMessageForm.Designer.cs index bd70dfe7..f0db66a0 100644 --- a/SunnyUI/Forms/UIMessageForm.Designer.cs +++ b/SunnyUI/Forms/UIMessageForm.Designer.cs @@ -48,6 +48,7 @@ btnCancel.Style = UIStyle.Custom; 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; // @@ -64,6 +65,7 @@ btnOK.Style = UIStyle.Custom; 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; // @@ -107,6 +109,7 @@ ShowInTaskbar = false; Text = "UIMsgBox"; ZoomScaleRect = new System.Drawing.Rectangle(15, 15, 450, 270); + FormClosed += UIMessageForm_FormClosed; Shown += UIMessageForm_Shown; ResumeLayout(false); } diff --git a/SunnyUI/Forms/UIMessageForm.cs b/SunnyUI/Forms/UIMessageForm.cs index 633f8fd7..9f912a96 100644 --- a/SunnyUI/Forms/UIMessageForm.cs +++ b/SunnyUI/Forms/UIMessageForm.cs @@ -212,5 +212,10 @@ namespace Sunny.UI if (delay <= 0) Close(); } + + private void UIMessageForm_FormClosed(object sender, FormClosedEventArgs e) + { + timer1.Stop(); + } } } \ No newline at end of file diff --git a/SunnyUI/Frames/UIPage.cs b/SunnyUI/Frames/UIPage.cs index 243b2c5c..5d842c09 100644 --- a/SunnyUI/Frames/UIPage.cs +++ b/SunnyUI/Frames/UIPage.cs @@ -43,6 +43,7 @@ * 2023-11-06: V3.5.2 重构主题 * 2023-12-04: V3.6.1 修复修改Style后,BackColor未保存的问题 * 2023-12-20: V3.6.2 调整AfterShow事件位置及逻辑 + * 2024-04-28: V3.6.5 增加WindowStateChanged事件 ******************************************************************************/ using System; @@ -56,34 +57,6 @@ namespace Sunny.UI [DefaultEvent("Initialize")] public partial class UIPage : Form, IStyleInterface, ISymbol, IZoomScale { - public readonly Guid Guid = Guid.NewGuid(); - private Color _rectColor = UIColor.Blue; - - private ToolStripStatusLabelBorderSides _rectSides = ToolStripStatusLabelBorderSides.None; - - protected UIStyle _style = UIStyle.Inherited; - - [Browsable(false)] - public IFrame Frame - { - get; set; - } - - private bool extendBox; - - [DefaultValue(false)] - [Description("显示扩展按钮"), Category("SunnyUI")] - public bool ExtendBox - { - get => extendBox; - set - { - extendBox = showTitle && value; - CalcSystemBoxPos(); - Invalidate(); - } - } - public UIPage() { InitializeComponent(); @@ -115,6 +88,41 @@ namespace Sunny.UI base.SizeGripStyle = SizeGripStyle.Hide; } + public readonly Guid Guid = Guid.NewGuid(); + private Color _rectColor = UIColor.Blue; + + private ToolStripStatusLabelBorderSides _rectSides = ToolStripStatusLabelBorderSides.None; + + protected UIStyle _style = UIStyle.Inherited; + + [Browsable(false)] + public IFrame Frame + { + get; set; + } + + private bool extendBox; + + [DefaultValue(false)] + [Description("显示扩展按钮"), Category("SunnyUI")] + public bool ExtendBox + { + get => extendBox; + set + { + extendBox = showTitle && value; + CalcSystemBoxPos(); + Invalidate(); + } + } + + public event OnWindowStateChanged WindowStateChanged; + + internal void DoWindowStateChanged(FormWindowState thisState, FormWindowState lastState) + { + WindowStateChanged?.Invoke(this, thisState, lastState); + } + protected override void OnClick(EventArgs e) { base.OnClick(e); diff --git a/SunnyUI/Style/UIStyles.cs b/SunnyUI/Style/UIStyles.cs index 764d2f64..99483eca 100644 --- a/SunnyUI/Style/UIStyles.cs +++ b/SunnyUI/Style/UIStyles.cs @@ -204,10 +204,10 @@ namespace Sunny.UI SetStyle(UIStyle.Colorful); } - private static readonly ConcurrentDictionary Styles = new ConcurrentDictionary(); - private static readonly ConcurrentDictionary Forms = new ConcurrentDictionary(); - private static readonly ConcurrentDictionary Pages = new ConcurrentDictionary(); - private static readonly ConcurrentDictionary Forms2 = new ConcurrentDictionary(); + internal static readonly ConcurrentDictionary Styles = new ConcurrentDictionary(); + internal static readonly ConcurrentDictionary Forms = new ConcurrentDictionary(); + internal static readonly ConcurrentDictionary Pages = new ConcurrentDictionary(); + internal static readonly ConcurrentDictionary Forms2 = new ConcurrentDictionary(); ///