注册 登录
无忧答案网 返回首页

张老师的个人空间 https://www.ap5u.com/?112605 [收藏] [复制] [RSS] 专业远程教辅导

日志

Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标 ...

已有 15 次阅读2024-9-27 00:02 |个人分类:编程| WinForm

 Winform中实现无边框窗体只需要设置一个属性FormBorderStyle = FormBorderStyle.None;即可,或者在设计器中直接设置。
但是无边框窗体没有鼠标按住标题栏不放拖动窗体、双击标题栏最大化或还原、鼠标拖动右下角或右/下边框调整大小,以及最大化、最小化、关闭等功能。使得窗体的功能和使用不完整,因此下面介绍几种对这些功能的实现。
一、(拖动边框或四角)调整窗体大小
先上效果图

在窗体的MouseMove事件中,判断鼠标的位置是否位于右侧边缘或底部边缘5像素内(可以修改大小,它表示窗体的边框),并根据位置设置光标的显示样式为双向箭头。
鼠标状态:
Cursors.SizeNWSE; // 双向对角线光标
Cursors.SizeWE; // 双向水平光标
Cursors.SizeNS; // 双向垂直光标
Cursors.Arrow; // 标准箭头鼠标
窗体大小的计算,首先获取鼠标的屏幕位置,水平方向上减去窗体Left即为窗体宽度;垂直方向上减去窗体Top即为窗体高度。
特别注意:代码中必须先进行调整大小,再处理鼠标状态。否则快速拖动(尤其向窗体内)可能导致鼠标位置变化(鼠标变更状态),但此时应该是调整窗体大小的过程中(鼠标状态变更导致大小调整终止)。
完整代码如下

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;
}
}



路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

QQ|手机版|小黑屋|网站地图|无忧答案网 ( 冀ICP备18010495号-1 )

GMT+8, 2024-10-1 17:25

Powered by 无忧答案网 X3.5

Copyright © 2018-2020 Design: Ap5u.Com

返回顶部