||
FormBorderStyle = FormBorderStyle.None;
即可,或者在设计器中直接设置。namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MouseMove += Main_MouseMove;
MouseLeave += Main_Leave; // 有控件在边缘时,处理一下更好一些
}
#region 拖拽调整窗体大小
private void Main_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//左键按下移动,拖拽调整大小
{
// MousePosition的参考点是屏幕的左上角,表示鼠标当前相对于屏幕左上角的坐标。this.Left和this.Top的参考点也是屏幕
if (Cursor == Cursors.SizeNWSE) // 倾斜拖拽
{
// 改变窗体宽和高的代码,其宽高为鼠标屏幕位置减去窗体的Left,Top距离
this.Width = MousePosition.X - this.Left;
this.Height = MousePosition.Y - this.Top;
}
else if (Cursor == Cursors.SizeWE) // 水平拖拽
{
Width = MousePosition.X - this.Left;
}
else if (Cursor == Cursors.SizeNS) // 垂直拖拽
{
Height = MousePosition.Y - this.Top;
}
}
//鼠标移动过程中,坐标时刻在改变
//当鼠标移动时横坐标距离窗体右边缘5像素以内且纵坐标距离下边缘也在5像素以内时,要将光标变为倾斜的箭头形状
if (e.Location.X >= this.Width - 5 && e.Location.Y > this.Height - 5)
{
this.Cursor = Cursors.SizeNWSE; // 右下角 双向对角线光标
}
//当鼠标移动时横坐标距离窗体右边缘5像素以内时,要将光标变为双向水平箭头形状
else if (e.Location.X >= this.Width - 5)
{
this.Cursor = Cursors.SizeWE; // 双向水平光标
}
//当鼠标移动时纵坐标距离窗体下边缘5像素以内时,要将光标变为垂直水平箭头形状
else if (e.Location.Y >= this.Height - 5)
{
this.Cursor = Cursors.SizeNS; // 双向垂直光标
}
//否则,以外的窗体区域,鼠标星座均为单向箭头(默认)
else this.Cursor = Cursors.Arrow;
}
private void Main_Leave(object sender, EventArgs e)
{
Cursor = Cursors.Arrow;// 移出窗体变为正常
}
#endregion
}
}
添加八个方向的控件,然后通过控件捕捉窗体拖动事件。废话不多啰唆了。
show me the code 如下:
public class BorderlessForm : Form
{
protected bool isDragging = false;
protected Rectangle lastRectangle = new Rectangle();
public BorderlessForm() : base()
{
this.FormBorderStyle = FormBorderStyle.None;
initialiseFormEdge();
}
protected void initialiseFormEdge()
{
int resizeWidth = 5;
this.MouseDown += new MouseEventHandler(form_MouseDown);
this.MouseMove += new MouseEventHandler(form_MouseMove);
this.MouseUp += delegate (object sender, MouseEventArgs e)
{
isDragging = false;
};
// bottom
UserControl uc1 = new UserControl()
{
Anchor = (AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right),
Height = resizeWidth,
Width = this.DisplayRectangle.Width - (resizeWidth * 2),
Left = resizeWidth,
Top = this.DisplayRectangle.Height - resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNS
};
uc1.MouseDown += form_MouseDown;
uc1.MouseUp += form_MouseUp;
uc1.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size(lastRectangle.Width, e.Y - lastRectangle.Y + this.Height);
}
};
uc1.BringToFront();
this.Controls.Add(uc1);
// right
UserControl uc2 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom),
Height = this.DisplayRectangle.Height - (resizeWidth * 2),
Width = resizeWidth,
Left = this.DisplayRectangle.Width - resizeWidth,
Top = resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeWE
};
uc2.MouseDown += form_MouseDown;
uc2.MouseUp += form_MouseUp;
uc2.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size(e.X - lastRectangle.X + this.Width, lastRectangle.Height);
}
};
uc2.BringToFront();
this.Controls.Add(uc2);
// bottom-right
UserControl uc3 = new UserControl()
{
Anchor = (AnchorStyles.Bottom | AnchorStyles.Right),
Height = resizeWidth,
Width = resizeWidth,
Left = this.DisplayRectangle.Width - resizeWidth,
Top = this.DisplayRectangle.Height - resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNWSE
};
uc3.MouseDown += form_MouseDown;
uc3.MouseUp += form_MouseUp;
uc3.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size((e.X - lastRectangle.X + this.Width), (e.Y - lastRectangle.Y + this.Height));
}
};
uc3.BringToFront();
this.Controls.Add(uc3);
// top-right
UserControl uc4 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Right),
Height = resizeWidth,
Width = resizeWidth,
Left = this.DisplayRectangle.Width - resizeWidth,
Top = 0,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNESW
};
uc4.MouseDown += form_MouseDown;
uc4.MouseUp += form_MouseUp;
uc4.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.Y - lastRectangle.Y);
int y = (this.Location.Y + diff);
this.Location = new Point(this.Location.X, y);
this.Size = new Size(e.X - lastRectangle.X + this.Width, (this.Height + (diff * -1)));
}
};
uc4.BringToFront();
//uc4.BackColor = Color.Firebrick;
this.Controls.Add(uc4);
// top
UserControl uc5 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right),
Height = resizeWidth,
Width = this.DisplayRectangle.Width - (resizeWidth * 2),
Left = resizeWidth,
Top = 0,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNS
};
uc5.MouseDown += form_MouseDown;
uc5.MouseUp += form_MouseUp;
uc5.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.Y - lastRectangle.Y);
int y = (this.Location.Y + diff);
this.Location = new Point(this.Location.X, y);
this.Size = new Size(lastRectangle.Width, (this.Height + (diff * -1)));
}
};
uc5.BringToFront();
this.Controls.Add(uc5);
// left
UserControl uc6 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom),
Height = this.DisplayRectangle.Height - (resizeWidth * 2),
Width = resizeWidth,
Left = 0,
Top = resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeWE
};
uc6.MouseDown += form_MouseDown;
uc6.MouseUp += form_MouseUp;
uc6.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.X - lastRectangle.X);
int x = (this.Location.X + diff);
this.Location = new Point(x, this.Location.Y);
this.Size = new Size((this.Width + (diff * -1)), this.Height);
}
};
uc6.BringToFront();
this.Controls.Add(uc6);
// bottom-left
UserControl uc7 = new UserControl()
{
Anchor = (AnchorStyles.Bottom | AnchorStyles.Left),
Height = resizeWidth,
Width = resizeWidth,
Left = 0,
Top = this.DisplayRectangle.Height - resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNESW
};
uc7.MouseDown += form_MouseDown;
uc7.MouseUp += form_MouseUp;
uc7.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.X - lastRectangle.X);
int x = (this.Location.X + diff);
this.Location = new Point(x, this.Location.Y);
this.Size = new Size((this.Width + (diff * -1)), (e.Y - lastRectangle.Y + this.Height));
}
};
uc7.BringToFront();
this.Controls.Add(uc7);
// bottom-left
UserControl uc8 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Left),
Height = resizeWidth,
Width = resizeWidth,
Left = 0,
Top = 0,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNWSE
};
uc8.MouseDown += form_MouseDown;
uc8.MouseUp += form_MouseUp;
uc8.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int dX = (e.Location.X - lastRectangle.X);
int dY = (e.Location.Y - lastRectangle.Y);
int x = (this.Location.X + dX);
int y = (this.Location.Y + dY);
this.Location = new Point(x, y);
this.Size = new Size((this.Width + (dX * -1)), (this.Height + (dY * -1)));
}
};
uc8.BringToFront();
this.Controls.Add(uc8);
}
private void form_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
lastRectangle = new Rectangle(e.Location.X, e.Location.Y, this.Width, this.Height);
}
}
private void form_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
int x = (this.Location.X + (e.Location.X - lastRectangle.X));
int y = (this.Location.Y + (e.Location.Y - lastRectangle.Y));
this.Location = new Point(x, y);
}
}
private void form_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
}
|手机版|小黑屋|网站地图|无忧答案网 ( 冀ICP备18010495号-1 )
GMT+8, 2025-1-3 02:21