UIForm移动到顶最大化,移动范围判断

This commit is contained in:
Sunny 2020-05-28 21:07:23 +08:00
parent 19ec4fcfa4
commit 361af57295
5 changed files with 57 additions and 68 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,22 +1,22 @@
/****************************************************************************** /******************************************************************************
* SunnyUI * SunnyUI
* CopyRight (C) 2012-2020 ShenYongHua(). * CopyRight (C) 2012-2020 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@qq.com * QQ群56829229 QQ17612584 EMailSunnyUI@qq.com
* *
* Blog: https://www.cnblogs.com/yhuse * Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI * Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI * GitHub: https://github.com/yhuse/SunnyUI
* *
* SunnyUI.dll can be used for free under the GPL-3.0 license. * SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note. * If you use this code, please keep this note.
* 使 * 使
****************************************************************************** ******************************************************************************
* : UIForm.cs * : UIForm.cs
* : * :
* : V2.2 * : V2.2
* : 2020-01-01 * : 2020-01-01
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -384,31 +384,37 @@ namespace Sunny.UI
} }
private Size size; private Size size;
private Point mLocation; // 缩放前的窗体位置 private Point location;
private void ShowMaximize() private int GetMouseInScreen(Point mousePos)
{ {
int screenIndex = 0; int screenIndex = 0;
for (int i = 0; i < Screen.AllScreens.Length; i++) for (int i = 0; i < Screen.AllScreens.Length; i++)
{ {
if (MousePos.InRect(Screen.AllScreens[i].Bounds)) if (mousePos.InRect(Screen.AllScreens[i].Bounds))
{ {
screenIndex = i; screenIndex = i;
break; break;
} }
} }
return screenIndex;
}
private void ShowMaximize()
{
int screenIndex = GetMouseInScreen(MousePosition);
Screen screen = Screen.AllScreens[screenIndex]; Screen screen = Screen.AllScreens[screenIndex];
if (windowState == FormWindowState.Normal) if (windowState == FormWindowState.Normal)
{ {
size = Size; size = Size;
mLocation = Location; location = Location;
Width = ShowFullScreen ? screen.Bounds.Width : screen.WorkingArea.Width; Width = ShowFullScreen ? screen.Bounds.Width : screen.WorkingArea.Width;
Height = ShowFullScreen ? screen.Bounds.Height : screen.WorkingArea.Height; Height = ShowFullScreen ? screen.Bounds.Height : screen.WorkingArea.Height;
Left = screen.Bounds.Left; Left = screen.Bounds.Left;
Top = screen.Bounds.Top; Top = screen.Bounds.Top;
StartPosition = FormStartPosition.Manual; //StartPosition = FormStartPosition.Manual;
SetFormRoundRectRegion(this, 0); SetFormRoundRectRegion(this, 0);
windowState = FormWindowState.Maximized; windowState = FormWindowState.Maximized;
@ -419,12 +425,13 @@ namespace Sunny.UI
{ {
size = new Size(800, 600); size = new Size(800, 600);
} }
if (mLocation.IsEmpty)
{
mLocation = screen.WorkingArea.Location;
}
Size = size; Size = size;
Location = mLocation; Point center = new Point(screen.Bounds.Left + screen.WorkingArea.Width / 2 - Size.Width / 2,
screen.Bounds.Top + screen.WorkingArea.Height / 2 - Size.Height / 2);
if (location.X == 0 && location.Y == 0) location = center;
Location = StartPosition == FormStartPosition.CenterScreen ? center : location;
StartPosition = FormStartPosition.CenterScreen; StartPosition = FormStartPosition.CenterScreen;
SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); SetFormRoundRectRegion(this, ShowRadius ? 5 : 0);
windowState = FormWindowState.Normal; windowState = FormWindowState.Normal;
@ -433,7 +440,6 @@ namespace Sunny.UI
Invalidate(); Invalidate();
} }
private Point MousePos;
private bool FormMoveMouseDown; private bool FormMoveMouseDown;
private Point FormLocation; //form的location private Point FormLocation; //form的location
private Point mouseOffset; //鼠标的按下位置 private Point mouseOffset; //鼠标的按下位置
@ -445,6 +451,7 @@ namespace Sunny.UI
if (InControlBox || InMaxBox || InMinBox) return; if (InControlBox || InMaxBox || InMinBox) return;
if (!ShowTitle) return; if (!ShowTitle) return;
if (e.Y > Padding.Top) return; if (e.Y > Padding.Top) return;
if (windowState == FormWindowState.Maximized) return;
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
{ {
@ -472,27 +479,25 @@ namespace Sunny.UI
if (FormMoveMouseDown) if (FormMoveMouseDown)
{ {
int screenIndex = 0; if (MousePosition.Y <= 0 && MaximizeBox)
for (int i = 0; i < Screen.AllScreens.Length; i++)
{ {
if (MousePos.InRect(Screen.AllScreens[i].Bounds)) if (windowState == FormWindowState.Normal)
{
screenIndex = i;
break;
}
}
Screen screen = Screen.AllScreens[screenIndex];
if (MousePosition.Y == 0 && MaximizeBox)
{ {
ShowMaximize(); ShowMaximize();
} }
if(Top < screen.WorkingArea.Top) // 防止窗体上移时标题栏超出容器,导致后续无法移动 }
else
{
int screenIndex = GetMouseInScreen(MousePosition);
Screen screen = Screen.AllScreens[screenIndex];
if (Top < screen.WorkingArea.Top) // 防止窗体上移时标题栏超出容器,导致后续无法移动
{ {
Top = screen.WorkingArea.Top; Top = screen.WorkingArea.Top;
} }
else if(Top > screen.WorkingArea.Bottom) // 防止窗体下移时标题栏超出容器,导致后续无法移动 if (Top > screen.WorkingArea.Bottom - TitleHeight) // 防止窗体下移时标题栏超出容器,导致后续无法移动
{ {
Top = screen.WorkingArea.Bottom - 10; Top = screen.WorkingArea.Bottom - TitleHeight;
}
} }
} }
@ -501,30 +506,13 @@ namespace Sunny.UI
protected override void OnMouseMove(MouseEventArgs e) protected override void OnMouseMove(MouseEventArgs e)
{ {
MousePos = PointToScreen(e.Location);
if (FormMoveMouseDown) if (FormMoveMouseDown)
{
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; Point pt = MousePosition;
int offsetX = mouseOffset.X - pt.X; int offsetX = mouseOffset.X - pt.X;
int offsetY = mouseOffset.Y - pt.Y; int offsetY = mouseOffset.Y - pt.Y;
Location = new Point(FormLocation.X - offsetX, FormLocation.Y - offsetY); Location = new Point(FormLocation.X - offsetX, FormLocation.Y - offsetY);
} }
}
else else
{ {
if (FormBorderStyle == FormBorderStyle.None) if (FormBorderStyle == FormBorderStyle.None)
@ -596,7 +584,8 @@ namespace Sunny.UI
return; return;
} }
//Color titleColor = rectColor;// IsDesignMode ? rectColor : IsActive ? rectColor : Color.FromArgb(173, 178, 181); //Color titleColor = rectColor;// IsDesignMode ? rectColor : IsActive ? rectColor : Color.From
//Argb(173, 178, 181);
if (ShowTitle) if (ShowTitle)
{ {