From e5f27b35c34515141bb1599a430ce6032f181ef0 Mon Sep 17 00:00:00 2001 From: Owen Chang Date: Thu, 28 May 2020 15:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=A0=87=E9=A2=98=E6=A0=8F?= =?UTF-8?q?=E5=81=9C=E9=9D=A0=E5=AE=B9=E5=99=A8=E9=A1=B6=E9=83=A8=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=9C=80=E5=A4=A7=E5=8C=96=EF=BC=8C=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=8C=96=E6=97=B6=E6=8B=96=E5=8A=A8=E6=A0=87=E9=A2=98=E6=A0=8F?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=81=A2=E5=A4=8D=E5=8E=9F=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E8=AE=B0=E5=BD=95=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=8C=96=E5=89=8D=E7=AA=97=E4=BD=93=E4=BD=8D=E7=BD=AE=EF=BC=88?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=90=8E=E8=87=AA=E5=8A=A8=E5=9B=9E=E5=88=B0?= =?UTF-8?q?=E5=8E=9F=E4=BD=8D=E7=BD=AE=EF=BC=8C=E8=80=8C=E9=9D=9E=E5=9B=BA?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E7=BD=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Forms/UIForm.cs | 61 +++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/SunnyUI/Forms/UIForm.cs b/SunnyUI/Forms/UIForm.cs index 5de45961..68909e27 100644 --- a/SunnyUI/Forms/UIForm.cs +++ b/SunnyUI/Forms/UIForm.cs @@ -384,6 +384,7 @@ namespace Sunny.UI } private Size size; + private Point mLocation; // 缩放前的窗体位置 private void ShowMaximize() { @@ -401,6 +402,7 @@ namespace Sunny.UI if (windowState == FormWindowState.Normal) { size = Size; + mLocation = Location; Width = ShowFullScreen ? screen.Bounds.Width : screen.WorkingArea.Width; Height = ShowFullScreen ? screen.Bounds.Height : screen.WorkingArea.Height; @@ -417,10 +419,12 @@ namespace Sunny.UI { size = new Size(800, 600); } - + if (mLocation.IsEmpty) + { + mLocation = screen.WorkingArea.Location; + } Size = size; - Left = screen.Bounds.Left + screen.WorkingArea.Width / 2 - Size.Width / 2; - Top = screen.Bounds.Top + screen.WorkingArea.Height / 2 - Size.Height / 2; + Location = mLocation; StartPosition = FormStartPosition.CenterScreen; SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); windowState = FormWindowState.Normal; @@ -441,7 +445,6 @@ namespace Sunny.UI if (InControlBox || InMaxBox || InMinBox) return; if (!ShowTitle) return; if (e.Y > Padding.Top) return; - if (windowState == FormWindowState.Maximized) return; if (e.Button == MouseButtons.Left) { @@ -466,6 +469,33 @@ namespace Sunny.UI protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); + + if (FormMoveMouseDown) + { + int screenIndex = 0; + for (int i = 0; i < Screen.AllScreens.Length; i++) + { + if (MousePos.InRect(Screen.AllScreens[i].Bounds)) + { + screenIndex = i; + break; + } + } + Screen screen = Screen.AllScreens[screenIndex]; + if (MousePosition.Y == 0 && MaximizeBox) + { + ShowMaximize(); + } + if(Top < screen.WorkingArea.Top) // 防止窗体上移时标题栏超出容器,导致后续无法移动 + { + Top = screen.WorkingArea.Top; + } + else if(Top > screen.WorkingArea.Bottom) // 防止窗体下移时标题栏超出容器,导致后续无法移动 + { + Top = screen.WorkingArea.Bottom - 10; + } + } + FormMoveMouseDown = false; } @@ -475,10 +505,25 @@ namespace Sunny.UI if (FormMoveMouseDown) { - Point pt = MousePosition; - int offsetX = mouseOffset.X - pt.X; - int offsetY = mouseOffset.Y - pt.Y; - Location = new Point(FormLocation.X - offsetX, FormLocation.Y - offsetY); + if (this.windowState == FormWindowState.Maximized) + { + Point pt = MousePosition; + int MaximizedWidth = Width; + int LocationX = Left; + ShowMaximize(); + // 计算等比例缩放后,鼠标与原位置的相对位移 + mouseOffset.X -= Math.Abs(mouseOffset.X) - (mouseOffset.X - LocationX) * Width / MaximizedWidth; + int offsetX = mouseOffset.X - pt.X; + int offsetY = mouseOffset.Y - pt.Y; + Location = new Point(offsetX, offsetY); + } + else + { + Point pt = MousePosition; + int offsetX = mouseOffset.X - pt.X; + int offsetY = mouseOffset.Y - pt.Y; + Location = new Point(FormLocation.X - offsetX, FormLocation.Y - offsetY); + } } else {