Изменение размеров формы, у которой BorderStyle установлен в None - C#
Формулировка задачи:
У формы border-style:none, для изменения размера ставлю на форму панель, код нарыл на буржуйском сайте, но он позволяет растягивать форму только вправо и вниз, помогите переделать для всех сторон
Point ResizeLocation = Point.Empty;
void panResize_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ResizeLocation = e.Location;
ResizeLocation.Offset(-this.Width, -this.Height);
if (!(ResizeLocation.X > -17 || ResizeLocation.Y > -17))
ResizeLocation = Point.Empty;
}
else
ResizeLocation = Point.Empty;
}
void panResize_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//int cursor2=0;
if (e.Button == MouseButtons.Left && !ResizeLocation.IsEmpty)
{
label1.Text = this.Width.ToString() + "x" + this.Height.ToString();
if (this.Cursor == Cursors.SizeNWSE)
Size = new Size(e.Location.X - ResizeLocation.X, e.Location.Y - ResizeLocation.Y);
else if (this.Cursor == Cursors.SizeWE)
Size = new Size(e.Location.X - ResizeLocation.X, Size.Height);
else if (this.Cursor == Cursors.SizeNS)
Size = new Size(Size.Width, e.Location.Y - ResizeLocation.Y);
}
else if (e.X - this.Width > -17 && e.Y - this.Height > -17)
this.Cursor = Cursors.SizeNWSE;
else if (e.X - this.Width > -17)
this.Cursor = Cursors.SizeWE;
else if (e.Y - this.Height > -17)
this.Cursor = Cursors.SizeNS;
// else if (e.X - this.Width < -(this.Width - 16))
//{ this.Cursor = Cursors.SizeWE; cursor2 = 1; }
else
{
this.Cursor = Cursors.Default;
}
}
void panResize_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
ResizeLocation = Point.Empty;
}Решение задачи: «Изменение размеров формы, у которой BorderStyle установлен в None»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
Point sizePoint = Point.Empty;
SizeMode sizeMode = SizeMode.None;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
Cursor = Cursors.Default;
SizeMode x=GetSizeMode();
if (x== SizeMode.Left || x== SizeMode.Right)
Cursor = Cursors.SizeWE;
if (x == SizeMode.Top || x == SizeMode.Bottom)
Cursor = Cursors.SizeNS;
if (x == SizeMode.LeftTop || x == SizeMode.RightBottom)
Cursor = Cursors.SizeNWSE;
if (x == SizeMode.RightTop || x == SizeMode.LeftBottom)
Cursor = Cursors.SizeNESW;
int dx = MousePosition.X - sizePoint.X;
int dy = MousePosition.Y - sizePoint.Y;
if (sizeMode != SizeMode.None)
SuspendLayout();
switch (sizeMode)
{
case SizeMode.Left:
Left += dx;
Width -= dx;
break;
case SizeMode.Right:
Width += dx;
break;
case SizeMode.Top:
Top += dy;
Height -= dy;
break;
case SizeMode.Bottom:
Height += dy;
break;
case SizeMode.LeftTop:
Left += dx;
Width -= dx;
Top += dy;
Height -= dy;
break;
case SizeMode.RightTop:
Width += dx;
Top += dy;
Height -= dy;
break;
case SizeMode.LeftBottom:
Left += dx;
Width -= dx;
Height += dy;
break;
case SizeMode.RightBottom:
Width += dx;
Height += dy;
break;
}
if (sizeMode != SizeMode.None)
{
sizePoint = MousePosition;
ResumeLayout();
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
Cursor = Cursors.Default;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
sizeMode = GetSizeMode();
if (sizeMode != SizeMode.None)
sizePoint =MousePosition;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
sizeMode = SizeMode.None;
}
SizeMode GetSizeMode()
{
Point p = PointToClient(MousePosition);
if (p.X < Padding.Left)
{
if (p.Y < Padding.Top)
return SizeMode.LeftTop;
if (p.Y > Height - Padding.Bottom)
return SizeMode.LeftBottom;
return SizeMode.Left;
}
if (p.X > Width - Padding.Right)
{
if (p.Y < Padding.Top)
return SizeMode.RightTop;
if (p.Y > Height - Padding.Bottom)
return SizeMode.RightBottom;
return SizeMode.Right;
}
if (p.Y < Padding.Top)
return SizeMode.Top;
if (p.Y > Height - Padding.Bottom)
return SizeMode.Bottom;
return SizeMode.None;
}
enum SizeMode { None, Left, Right, Top, Bottom,LeftTop, LeftBottom, RightTop, RightBottom };
private void panel1_Click(object sender, EventArgs e)
{
}
private void panel1_DoubleClick(object sender, EventArgs e)
{
Application.Exit();
}
}
}