C# Winform移动窗体、改变窗体大小
Winform,拖拽改变窗体大小,自定义改变窗体
一、FormBoderStyle属性改变窗体样式
属性 | 值 | 意义 |
FormBorderStyle.None | 0 | 无边框 |
FormBorderStyle.FixedSingle | 1 | 固定的单行边框 |
FormBorderStyle.Fixed3D | 2 | 固定的三维样式边框 |
FormBorderStyle.FixedDialog | 3 | 固定的对话框样式的粗边框 |
FormBorderStyle.Sizable | 4 | 可调整大小的边框 |
FormBorderStyle.FixedToolWindow | 5 | 不可调整大小的工具窗口边框 |
FormBorderStyle.SizableToolWindow | 6 | 可调整大小的工具窗口边框 |
二、将FormBorderStyle设置成None,如何再后台代码操作窗体改变
1.拖动窗体
拖动窗体需要用到MouseDown(鼠标按下)、MouseUp(鼠标抬起)、MouseMove(鼠标移动)三个事件
private Point position;//定位private void pnlmouse_MouseDown(object sender, MouseEventArgs e)
{
position = e.Location;
}private void pnlmouse_MouseUp(object sender, MouseEventArgs e)
{
position = e.Location;
}private void pnlmouse_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
this.Location = new Point(this.Left + (e.X - position.X), this.Top + (e.Y - position.Y));
}
2.代码拖拽改变窗体大小
给控件添加点击事件
this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panelXXX_MouseUp);
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panelXXX_MouseDown);
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panelXXX_MouseMove)//定义一个枚举,表示拖动方向
public enum MouseDirection
{Horizontal,//水平方向拖动,只改变窗体宽度
Vertical,//垂直方向拖动,只改变窗体的高度Declining,//倾斜方向,同时改变宽度和高度
None//不做标志,即不拖动窗体改变大小
}bool isMouseDown = false; //表示鼠标当前是否处于按下状态,初始值为否
MouseDirection direction = MouseDirection.None;//表示拖动的方向,起始为None,表示不拖动
private void panelXXX_MouseDown(object sender, MouseEventArgs e)
{
//鼠标按下
isMouseDown = true;
}private void panelXXX_MouseUp(object sender, MouseEventArgs e)
{
// 鼠标弹起,isMouseDown = false;
//既然鼠标弹起了,那么就不能再改变窗体尺寸,拖拽方向置 none
direction = MouseDirection.None;
}private void panelXXX_MouseMove(object sender, MouseEventArgs e)
{
//鼠标移动过程中,坐标时刻在改变//同理当鼠标移动时坐标距离窗体下边缘5像素以内时,要将光标改变形状,同时拖拽方向direction
if (e.Location.Y >= this.panelXXX.Height - 10)//距离容器高度10像素内改变竖向光标
{
this.Cursor = Cursors.SizeNS;
direction = MouseDirection.Vertical;
}else if (e.Location.X>=this.panelXXX.Width-10&&e.Location.Y >= this.panelXXX.Height - 10)//距离容器高度10像素内改变拖拽光标
{
this.Cursor = Cursors.SizeNWSE;
direction = MouseDirection.Declining;
}else if (e.Location.X>=this.panelXXX.Width-10)//距离容器宽度10像素内改变横向光标
{
this.Cursor = Cursors.SizeWE;
direction = MouseDirection.Herizontal;
} else //否则,以外的窗体区域,鼠标星座均为单向箭头(默认)
{
this.Cursor = Cursors.Arrow;
}
//设定好方向后,调用下面方法,改变窗体大小
ResizeWindow();
}
private void ResizeWindow()
{
//这个判断很重要,只有在鼠标按下时才能拖拽改变窗体大小,如果不作判断,那么鼠标弹起和按下时,窗体都可以改变
if (!isMouseDown)
return;
//MousePosition的参考点是屏幕的左上角,表示鼠标当前相对于屏幕左上角的坐标this.left和this.top的参考点也是屏幕,属性MousePosition是该程序的重点
if (direction == MouseDirection.Vertical)
{
this.Cursor = Cursors.SizeNS;
this.panelXXX.Height = MousePosition.Y - this.Top;}else if (direction == MouseDirection.Horizontal)
{
this.Cursor = Cursors.SizeWE;
this.panelXXX.Width= MousePosition.X - this.Left;}else if (direction == MouseDirection.Declining)
{
this.Cursor = Cursors.SizeNWSE;
this.panelXXX.Width= MousePosition.X - this.Left;
this.panelXXX.Height = MousePosition.Y - this.Top;} else{
//即使鼠标按下,但是不在窗口右和下边缘,那么也不能改变窗口大小
this.Cursor = Cursors.Arrow;
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)