首页 > 编程语言 >C#winfrom调整任意控件宽和高

C#winfrom调整任意控件宽和高

时间:2022-11-21 17:55:44浏览次数:66  
标签:Location 控件 C# winfrom Width ContorlMove ms new CON

在winform项目开发中,偶尔需要用到边框拖拽。度娘也没找到相关的轮子(可能是我不配,没推给我)。只能自己造一个

上效果图(鼠标没录制上,问题不大)

 

 

 上代码

  private void Form1_Load(object sender, EventArgs e)
        {//使用方式
            panelLeft.SetContorlMove(this, ContorlMove.Left);
            panelRight.SetContorlMove(this, ContorlMove.Right);
            panelTop.SetContorlMove(this, ContorlMove.Top);
            panelDown.SetContorlMove(this, ContorlMove.Down);
            dataGridView1.SetContorlMove(this, ContorlMove.All);
        }

  

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoveControlBorder
{
    public static class ContorlExt
    {
        #region 设置控件拖大拖小
        public static void SetContorlMove(this DataGridView con, Form form, ContorlMove moveEnum, Size? maxSize = null, Size? minSize = null)
        {
            new SetContorlMove(con, form, moveEnum, maxSize, minSize);
        }
        public static void SetContorlMove(this Panel con, Form form, ContorlMove moveEnum, Size? maxSize = null, Size? minSize = null)
        {
            new SetContorlMove(con, form, moveEnum, maxSize, minSize);
        }
        #endregion
    }
    public enum ContorlMove
    {
        /// <summary>
        /// 左边可以拉动宽度调整
        /// </summary>
        Left,
        /// <summary>
        /// 右边可以拉动宽度调整
        /// </summary>
        Right,
        /// <summary>
        /// 上边框可以拉动高度调整
        /// </summary>
        Top,
        /// <summary>
        /// 下边框可以拉动高度调整
        /// </summary>
        Down,
        /// <summary>
        /// 四个边都可以调整
        /// </summary>
        All
        /// <summary>
        /// 左上斜角
        /// </summary>
        //LeftTop,
        //LeftDown,
        //RightTop,
        //RightDown
    }
    public class SetContorlMove
    {
        private Control CON;
        private Form FORM;
        private ContorlMove MOVEENUM;
        private Size MaxSize;
        private Size MinSize;
        private bool IsAll = false;
        public SetContorlMove(Control _con, Form _form, ContorlMove _moveEnum, Size? _maxSize, Size? _minSize)
        {
            CON = _con;
            FORM = _form;
            MOVEENUM = _moveEnum;
            if (_moveEnum==ContorlMove.All)
            {
                IsAll = true;
            }
            if (_maxSize != null)
            {
                MaxSize = (Size)_maxSize;
            }
            else
            {
                MaxSize = new Size()
                {
                    Height = 1000,
                    Width = 1000
                };
            }
            if (_minSize != null)
            {
                MinSize = (Size)_minSize;
            }
            else
            {
                MinSize = new Size() { Height = 100, Width = 100 };
            }
            _con.MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown);
            _con.MouseLeave += new System.EventHandler(MouseLeave);
            _con.MouseMove += new System.Windows.Forms.MouseEventHandler(MouseMove);
            _con.MouseUp += new System.Windows.Forms.MouseEventHandler(MouseUp);
        }
        private void MouseUp(object sender, MouseEventArgs e)
        {
            if (IsAll)
            {
                MOVEENUM = ContorlMove.All; Now_MOVEENUM = ContorlMove.All;
            }
            moveflag = false;
            CON.Cursor = Cursors.Default;
        }
        bool moveflag = false;
        ContorlMove Now_MOVEENUM = ContorlMove.All;
        private void MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && (CON.Cursor == Cursors.SizeWE || CON.Cursor == Cursors.SizeNS))
            {
                if(MOVEENUM== ContorlMove.All)//判定当前按下了哪个的边
                {
                    Point ms = Control.MousePosition;
                    Point p;
                    p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y));
                        if (ms.X > p.X - 5 && ms.X < p.X + 5)//left
                        {
                            if (ms.Y >= p.Y)
                            {
                            Now_MOVEENUM = ContorlMove.Left;
                            }
                        }
                        else if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//top
                        {
                            if (ms.X >= p.X)
                            {
                            Now_MOVEENUM = ContorlMove.Top;
                            }
                        }
                        p = FORM.PointToScreen(new Point(CON.Location.X + CON.Width, CON.Location.Y));
                        if (ms.X > p.X - 5 && ms.X < p.X + 5)//right
                        {
                            if (ms.Y >= p.Y)
                            {
                            Now_MOVEENUM = ContorlMove.Right;
                            }
                        }
                        p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y + CON.Height));

                        if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//down
                        {
                            if (ms.X >= p.X)
                            {
                            Now_MOVEENUM = ContorlMove.Down;
                            }
                        }
                        
                }

                this.moveflag = true;
            }
        }
        private void MouseMove(object sender, MouseEventArgs e)
        {
            Point ms = Control.MousePosition;
            bool b = false;
            Point p;
            switch (MOVEENUM)
            {
                case ContorlMove.Left:
                    p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y));
                    if (ms.X > p.X - 5 && ms.X < p.X + 5)
                    {
                        if (ms.Y >= p.Y)
                        {
                            b = true;
                            CON.Cursor = Cursors.SizeWE;
                        }
                    }
                    break;
                case ContorlMove.Right:
                    p = FORM.PointToScreen(new Point(CON.Location.X + CON.Width, CON.Location.Y));
                    if (ms.X > p.X - 5 && ms.X < p.X + 5)
                    {
                        if (ms.Y >= p.Y)
                        {
                            b = true;
                            CON.Cursor = Cursors.SizeWE;
                        }
                    }
                    break;
                case ContorlMove.Top:
                    p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y));

                    if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)
                    {
                        if (ms.X >= p.X)
                        {
                            b = true;
                            CON.Cursor = Cursors.SizeNS;
                        }
                    }
                    break;
                case ContorlMove.Down:
                    p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y + CON.Height));

                    if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)
                    {
                        if (ms.X >= p.X)
                        {
                            b = true;
                            CON.Cursor = Cursors.SizeNS;
                        }
                    }
                    break;

                case ContorlMove.All://设置鼠标
                    p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y));
                    if (ms.X > p.X - 5 && ms.X < p.X + 5)//left
                    {
                        if (ms.Y >= p.Y)
                        {
                            b = true;
                            CON.Cursor = Cursors.SizeWE;
                            break;
                        }
                    }
                    else if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//top
                    {
                        if (ms.X >= p.X)
                        {
                            b = true; 
                            CON.Cursor = Cursors.SizeNS;
                            break;
                        }
                    }
                    p = FORM.PointToScreen(new Point(CON.Location.X + CON.Width, CON.Location.Y));
                    if (ms.X > p.X - 5 && ms.X < p.X + 5)//right
                    {
                        if (ms.Y >= p.Y)
                        {
                            b = true; 
                            CON.Cursor = Cursors.SizeWE;
                            break;
                        }
                    }
                    p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y + CON.Height));

                    if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//down
                    {
                        if (ms.X >= p.X)
                        {
                            b = true; 
                            CON.Cursor = Cursors.SizeNS; break;
                        }
                    }
                    break;
                default:
                    break;
            }

            if (!b && e.Button == MouseButtons.None)
            {
                CON.Cursor = Cursors.Default;
            }


            if (e.Button == MouseButtons.Left && moveflag)
            {
                if (Now_MOVEENUM!=ContorlMove.All)
                {
                    MOVEENUM = Now_MOVEENUM;
                }
                switch (MOVEENUM)
                {
                    case ContorlMove.Left:
                        if (CON.Width + -e.X > MinSize.Width && CON.Width + -e.X < MaxSize.Width)
                        {
                            InvokeInt(WH.Width, -e.X);//修改宽度
                        }
                        break;
                    case ContorlMove.Right:
                        if (e.X > MinSize.Width && e.X < MaxSize.Width)
                        {
                            InvokeInt(WH.Width, e.X);//修改宽度
                        }
                        break;
                    case ContorlMove.Top:
                        if (CON.Height + -e.Y > MinSize.Height && CON.Width + -e.Y < MaxSize.Height)
                        {
                            InvokeInt(WH.Height, -e.Y);
                        }
                        break;
                    case ContorlMove.Down:
                        if (e.Y > MinSize.Height && e.Y < MaxSize.Height)
                        {
                            InvokeInt(WH.Height, e.Y);
                        }
                        break;
                    default:
                        break;
                }

            }
        }
        private enum WH
        {
            Width,
            Height
        }

        private void InvokeInt(WH wh, int val)
        {
            if (CON.InvokeRequired)
            {
                Action<int> actionDelegate = (v) =>
                {
                    switch (wh)
                    {
                        case WH.Width:
                            if (MOVEENUM == ContorlMove.Right)
                            {
                                CON.Width = v;
                            }
                            else
                            {
                                CON.Width += v;
                                CON.Location = new Point(CON.Location.X - v, CON.Location.Y);

                            }
                            break;
                        case WH.Height:
                            if (MOVEENUM == ContorlMove.Down)
                            {
                                CON.Height = v;
                            }
                            else
                            {
                                CON.Height += v;
                                CON.Location = new Point(CON.Location.X, CON.Location.Y - v);
                            }
                            break;
                        default:
                            break;
                    }
                };
                CON.BeginInvoke(actionDelegate, val); //BeginInvoke方法是异步的, 它会另起一个子线程去完成工作线程
            }
            else
            {
                switch (wh)
                {
                    case WH.Width:
                        if (MOVEENUM == ContorlMove.Right)
                        {
                            CON.Width = val;
                        }
                        else
                        {
                            CON.Width += val;
                            CON.Location = new Point(CON.Location.X - val, CON.Location.Y);
                        }
                        break;
                    case WH.Height:
                        if (MOVEENUM == ContorlMove.Down)
                        {
                            CON.Height = val;
                        }
                        else
                        {
                            CON.Height += val; CON.Location = new Point(CON.Location.X, CON.Location.Y - val);

                        }
                        break;
                    default:
                        break;
                }
            }
        }
        private void MouseLeave(object sender, EventArgs e)
        {
            if (CON.Cursor == Cursors.SizeWE || CON.Cursor == Cursors.SizeNS)
            {
                CON.Cursor = Cursors.Default;
            }
        }
    }
}

   作者:兮去┓( ´∀` )┏博客
出处:https://www.cnblogs.com/bklsj/p/16784749.html
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

标签:Location,控件,C#,winfrom,Width,ContorlMove,ms,new,CON
From: https://www.cnblogs.com/bklsj/p/16912142.html

相关文章

  • docker容器互联
    百度网盘链接:https://pan.baidu.com/s/15t_TSH5RRpCFXV-93JHpNw?pwd=8od3 提取码:8od36docker容器互联6.1docker容器的网络基础6.1.1docker01.安装docker的时候,会......
  • React native ios上架
    1.申请开发者账号,去苹果开发者中心申请2.applicationloader集申请证书、真机调试、发布于一身,避免繁琐的官网申请过程http://kxdang.com/topic/appuploader/iosdev1.htm......
  • Centos 7升级系统内核版本
    步骤一:检查内核版本[root@master~]#uname-rsLinux3.10.0-1160.el7.x86_64 步骤二:升级内核CentOS允许使用ELRepo,这是一个第三方仓库,可以将内核升级到最新版本rp......
  • 1、Docker最新入门教程-Docker概述
    1.Docker概述Docker是一个用于开发、运输和运行应用程序的开放平台。Docker使您能够将应用程序与基础架构分开,以便您可以快速交付软件。使用Docker,您可以像管理应用程......
  • Rockwell EDI 850 采购订单报文详解
    罗克韦尔(Rockwell)自动化(中国)有限公司(NYSE:ROK)是全球最大的致力于工业自动化与信息化的公司,致力于帮助客户提高生产力,以及世界可持续发展。罗克韦尔自动化总部位于美国......
  • mac苹果电脑设置教程
    1.在底部,点系统偏好设置按钮 2.点击图中的网络 3.选择当前Wi-Fi,在右下角,点“高级” 4.点击图中【代理】,勾选【自动代理配置】一定要勾选啊!注意:鼠标一定要点......
  • JavaScript:世界上误解最深的语言
    JavaScript:TheWorld'sMostMisunderstoodProgrammingLanguageJavaScript:世界上误解最深的语言DouglasCrockfordwww.crockford.com翻译:袁晓辉JavaScript,akaMocha,a......
  • JFreeChart使用心得与例子
    一、JFreeChart的介绍与获取JFreeChart开发包是一个开源的Java图形开发包,它从柱形图,饼形图,到雷达图,蜡烛图等等无所不包,可以在c/s,b/s,甚至是实时环境都能一显身手。当......
  • Android错误之--Error retrieving parent for item: No resource found that matches
    改正错误(虽然内容较少,但是还是选择单独占用一篇)这个错误,可以说是困扰了我好久,然后就看到可以改变一下使用的Android版本,改成Android4.0,然后就去试了试,发现真的就好了耶!......
  • 华为云智能云接入ICA,助力企业轻松上云
    ​在云2.0时代,企业的业务和应用纷纷追求上云,但是由于网关实例等原因,企业上云存在一些困难,例如一对一专线成本高,不能就近入云以及管理运维麻烦等问题,这些难题阻碍了企业云上......