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

@ -384,31 +384,37 @@ namespace Sunny.UI
}
private Size size;
private Point mLocation; // 缩放前的窗体位置
private Point location;
private void ShowMaximize()
private int GetMouseInScreen(Point mousePos)
{
int screenIndex = 0;
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;
break;
}
}
return screenIndex;
}
private void ShowMaximize()
{
int screenIndex = GetMouseInScreen(MousePosition);
Screen screen = Screen.AllScreens[screenIndex];
if (windowState == FormWindowState.Normal)
{
size = Size;
mLocation = Location;
location = Location;
Width = ShowFullScreen ? screen.Bounds.Width : screen.WorkingArea.Width;
Height = ShowFullScreen ? screen.Bounds.Height : screen.WorkingArea.Height;
Left = screen.Bounds.Left;
Top = screen.Bounds.Top;
StartPosition = FormStartPosition.Manual;
//StartPosition = FormStartPosition.Manual;
SetFormRoundRectRegion(this, 0);
windowState = FormWindowState.Maximized;
@ -419,12 +425,13 @@ namespace Sunny.UI
{
size = new Size(800, 600);
}
if (mLocation.IsEmpty)
{
mLocation = screen.WorkingArea.Location;
}
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;
SetFormRoundRectRegion(this, ShowRadius ? 5 : 0);
windowState = FormWindowState.Normal;
@ -433,7 +440,6 @@ namespace Sunny.UI
Invalidate();
}
private Point MousePos;
private bool FormMoveMouseDown;
private Point FormLocation; //form的location
private Point mouseOffset; //鼠标的按下位置
@ -445,6 +451,7 @@ 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)
{
@ -472,27 +479,25 @@ namespace Sunny.UI
if (FormMoveMouseDown)
{
int screenIndex = 0;
for (int i = 0; i < Screen.AllScreens.Length; i++)
if (MousePosition.Y <= 0 && MaximizeBox)
{
if (MousePos.InRect(Screen.AllScreens[i].Bounds))
{
screenIndex = i;
break;
}
}
Screen screen = Screen.AllScreens[screenIndex];
if (MousePosition.Y == 0 && MaximizeBox)
if (windowState == FormWindowState.Normal)
{
ShowMaximize();
}
}
else
{
int screenIndex = GetMouseInScreen(MousePosition);
Screen screen = Screen.AllScreens[screenIndex];
if (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)
{
MousePos = PointToScreen(e.Location);
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;
int offsetX = mouseOffset.X - pt.X;
int offsetY = mouseOffset.Y - pt.Y;
Location = new Point(FormLocation.X - offsetX, FormLocation.Y - offsetY);
}
}
else
{
if (FormBorderStyle == FormBorderStyle.None)
@ -596,7 +584,8 @@ namespace Sunny.UI
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)
{