拖动和调整大小 - 无边框窗体
这段代码允许拖动和调整无边框窗体的大小!
引言
所有制作Windows应用程序的人都知道一个美观的Formbox
有多重要。你可能也玩过FormBorderStyle
属性,使用FormBorderStyle
- NONE
来创建你自己的Formbox。
但你很快就会发现,这样的Formbox
将无法拖动或调整大小。在本文中,我们将看到如何硬编码来实现与默认窗体相同的Formbox
行为。
使用代码
拖动Formbox的要求
- 一个
Panel
:用于拖动窗体窗口。为Panel选择左侧和右侧锚点(仅限)。 - 三个
TextBox
:关闭按钮、最大化按钮和最小化按钮。
// ****************************************
// * Created by bEGI *
// * http://www.youtube.com/user/MCneun *
// * VB & C# Programmer *
// ****************************************
using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
namespace Borderless_Form
{
public partial class Borderless : Form
{
public Borderless()
{
InitializeComponent();
}
我们声明两个整数,用于保存鼠标位置坐标的值,以及一个布尔值,当鼠标按下面板区域时,该值变为true
#region 'Drag'
int posX;
int posY;
bool drag;
private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (maximized)
{
this.WindowState = FormWindowState.Normal;
maximized = false;
}
else
{
this.WindowState = FormWindowState.Maximized;
maximized = true;
}
}
}
private void buttonClos_Click(object sender, EventArgs e)
{
this.Close();
}
private void buttonMax_Click(object sender, EventArgs e)
{
if (maximized)
{
maximized = false;
this.WindowState = FormWindowState.Normal;
}
else
{
maximized = true;
this.WindowState = FormWindowState.Maximized;
}
}
private void buttonMin_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
drag = true;
posX = Cursor.Position.X - this.Left;
posY = Cursor.Position.Y - this.Top;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
this.Top = System.Windows.Forms.Cursor.Position.Y - posY;
this.Left = System.Windows.Forms.Cursor.Position.X - posX;
}
this.Cursor = Cursors.Default;
}
#endregion
我们还需要一个Timer
,它会在我们调整窗口大小时负责自动重新排列Panel
和Button
,使其始终位于比例所需的正确位置。
private void timer1_Tick(object sender, EventArgs e)
{
panel1.Width = this.Width - 6;
panel1.Location = new Point(3, 3);
buttonClos.Location = new Point(panel1.Width - 23, 3);
buttonMax.Location = new Point(panel1.Width - 43, 3);
buttonMin.Location = new Point(panel1.Width - 63, 3);
}
调整Formbox大小的要求
我们需要事件的帮助来允许我们创建复杂的Windows应用程序。在这种情况下,我们需要捕获:鼠标抬起、鼠标按下和鼠标移动事件。
#region 'Resize'
bool maximized;
bool on_MinimumSize;
short minimumWidth = 350;
short minimumHeight = 26;
short borderSpace = 20;
short borderDiameter = 3;
bool onBorderRight;
bool onBorderLeft;
bool onBorderTop;
bool onBorderBottom;
bool onCornerTopRight;
bool onCornerTopLeft;
bool onCornerBottomRight;
bool onCornerBottomLeft;
bool movingRight;
bool movingLeft;
bool movingTop;
bool movingBottom;
bool movingCornerTopRight;
bool movingCornerTopLeft;
bool movingCornerBottomRight;
bool movingCornerBottomLeft;
private void Borderless_MouseUp(object sender, MouseEventArgs e)
{
stopResizer();
}
private void Borderless_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (onBorderRight ) { movingRight = true; } else { movingRight = false; }
if (onBorderLeft) { movingLeft = true;} else { movingLeft = false; }
if (onBorderTop) { movingTop = true;} else { movingTop = false; }
if (onBorderBottom) { movingBottom = true; } else { movingBottom = false; }
if (onCornerTopRight) { movingCornerTopRight = true;} else { movingCornerTopRight = false; }
if (onCornerTopLeft) { movingCornerTopLeft = true; } else { movingCornerTopLeft = false; }
if (onCornerBottomRight) { movingCornerBottomRight = true;} else { movingCornerBottomRight = false; }
if (onCornerBottomLeft) { movingCornerBottomLeft = true; } else { movingCornerBottomLeft = false; }
}
}
private void Borderless_MouseMove(object sender, MouseEventArgs e)
{
if (maximized) { return; }
if (this.Width <= minimumWidth) { this.Width = (minimumWidth + 5); on_MinimumSize = true; }
if (this.Height <= minimumHeight) { this.Height = (minimumHeight + 5); on_MinimumSize = true; }
if (on_MinimumSize) { stopResizer(); } else { startResizer(); }
if ((Cursor.Position.X > ((this.Location.X + this.Width) - borderDiameter))
& (Cursor.Position.Y > (this.Location.Y + borderSpace))
& (Cursor.Position.Y < ((this.Location.Y + this.Height) - borderSpace)))
{ this.Cursor = Cursors.SizeWE; onBorderRight = true; }
else if ((Cursor.Position.X < (this.Location.X + borderDiameter))
& (Cursor.Position.Y > (this.Location.Y + borderSpace))
& (Cursor.Position.Y < ((this.Location.Y + this.Height) - borderSpace)))
{ this.Cursor = Cursors.SizeWE; onBorderLeft = true; }
else if ((Cursor.Position.Y < (this.Location.Y + borderDiameter))
& (Cursor.Position.X > (this.Location.X + borderSpace))
& (Cursor.Position.X < ((this.Location.X + this.Width) - borderSpace)))
{ this.Cursor = Cursors.SizeNS; onBorderTop = true; }
else if ((Cursor.Position.Y > ((this.Location.Y + this.Height) - borderDiameter))
& (Cursor.Position.X > (this.Location.X + borderSpace))
& (Cursor.Position.X < ((this.Location.X + this.Width) - borderSpace)))
{ this.Cursor = Cursors.SizeNS; onBorderBottom = true; }
else if ((Cursor.Position.X == ((this.Location.X + this.Width) - 1))
& (Cursor.Position.Y == this.Location.Y))
{ this.Cursor = Cursors.SizeNESW; onCornerTopRight = true; }
else if ((Cursor.Position.X == this.Location.X)
& (Cursor.Position.Y == this.Location.Y))
{ this.Cursor = Cursors.SizeNWSE; onCornerTopLeft = true; }
else if ((Cursor.Position.X == ((this.Location.X + this.Width) - 1))
& (Cursor.Position.Y == ((this.Location.Y + this.Height) - 1)))
{ this.Cursor = Cursors.SizeNWSE; onCornerBottomRight = true; }
else if ((Cursor.Position.X == this.Location.X)
& (Cursor.Position.Y == ((this.Location.Y + this.Height) - 1)))
{ this.Cursor = Cursors.SizeNESW; onCornerBottomLeft = true; }
else
{
onBorderRight = false;
onBorderLeft = false;
onBorderTop = false;
onBorderBottom = false;
onCornerTopRight = false;
onCornerTopLeft = false;
onCornerBottomRight = false;
onCornerBottomLeft = false;
this.Cursor = Cursors.Default;
}
}
private void startResizer()
{
if (movingRight)
{
this.Width = Cursor.Position.X - this.Location.X;
}
else if (movingLeft)
{
this.Width = ((this.Width + this.Location.X) - Cursor.Position.X);
this.Location = new Point(Cursor.Position.X, this.Location.Y);
}
else if (movingTop)
{
this.Height = ((this.Height + this.Location.Y) - Cursor.Position.Y);
this.Location = new Point(this.Location.X, Cursor.Position.Y);
}
else if (movingBottom)
{
this.Height = (Cursor.Position.Y - this.Location.Y);
}
else if (movingCornerTopRight)
{
this.Width = (Cursor.Position.X - this.Location.X);
this.Height = ((this.Location.Y - Cursor.Position.Y) + this.Height);
this.Location = new Point(this.Location.X, Cursor.Position.Y);
}
else if (movingCornerTopLeft)
{
this.Width = ((this.Width + this.Location.X) - Cursor.Position.X);
this.Location = new Point(Cursor.Position.X, this.Location.Y);
this.Height = ((this.Height + this.Location.Y) - Cursor.Position.Y);
this.Location = new Point(this.Location.X, Cursor.Position.Y);
}
else if (movingCornerBottomRight)
{
this.Size = new Size(Cursor.Position.X - this.Location.X,
Cursor.Position.Y - this.Location.Y);
}
else if (movingCornerBottomLeft)
{
this.Width = ((this.Width + this.Location.X) - Cursor.Position.X);
this.Height = (Cursor.Position.Y - this.Location.Y);
this.Location = new Point(Cursor.Position.X, this.Location.Y);
}
}
private void stopResizer()
{
movingRight = false;
movingLeft = false;
movingTop = false;
movingBottom = false;
movingCornerTopRight = false;
movingCornerTopLeft = false;
movingCornerBottomRight = false;
movingCornerBottomLeft = false;
this.Cursor = Cursors.Default;
System.Threading.Thread.Sleep(300);
on_MinimumSize = false;
}
#endregion
}
}
历史
- 2012年12月18日:编辑并添加了C#源代码。