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


    在winform项目开发中,偶尔需要用到边框拖拽。

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

     

     

     上代码

    1
    2
    3
    4
    5
    6
    7
    8
    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);
          }

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    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
        {
            ///
            /// 左边可以拉动宽度调整
            ///
            Left,
            ///
            /// 右边可以拉动宽度调整
            ///
            Right,
            ///
            /// 上边框可以拉动高度调整
            ///
            Top,
            ///
            /// 下边框可以拉动高度调整
            ///
            Down,
            ///
            /// 四个边都可以调整
            ///
            All
            ///
            /// 左上斜角
            ///
            //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
    版权:本文版权归作者和博客园共有
    转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

  • 相关阅读:
    【Python 零基础入门】变量 & 数据类型
    Oracle技术分享:检查是否存在坏块的一些方法
    NX二次开发-ufusr和ufsta等用户入口使用说明
    uniapp中使用编辑器editor
    网络安全(黑客)自学
    Docker的常用命令
    R语言STAN贝叶斯线性回归模型分析气候变化影响北半球海冰范围和可视化检查模型收敛性...
    03_瑞萨GUI(LVGL)移植实战教程之驱动触摸屏(I2C)
    Unity使用UnityWebRequest读取音频长度不对的解决方法
    C++pimer第2章基础
  • 原文地址:https://www.cnblogs.com/bklsj/p/16912142.html