• winform 控件拖拽和缩放


    核心类:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Drawing;
      4 using System.Linq;
      5 using System.Text;
      6 using System.Threading.Tasks;
      7 using System.Windows.Forms;
      8 
      9 namespace ControlTest
     10     public class AddControlDragAttr
     11     {
     12         //委托事件  用于支持将其他的可拖拉的控件描点置隐藏
     13         public event EventHandler OtherLabelsVisibleEvent;
     14 
     15         /// <summary>
     16         /// 当前控件
     17         /// </summary>
     18         public Control CurrControl { get; private set; }
     19 
     20         /// <summary>
     21         /// 当前鼠标点击坐标
     22         /// </summary>
     23         Point downPoint;
     24         /// <summary>
     25         /// 当前控件的左右宽高
     26         /// </summary>
     27         int normalLeft, normalTop, normalWidth, normalHeight;
     28 
     29         /// <summary>
     30         /// 是否允许拖拉
     31         /// </summary>
     32         bool draging;
     33 
     34         /// <summary>
     35         /// 控件最小值
     36         /// </summary>
     37         public int ControlMinSize => 10;
     38 
     39         /// <summary>
     40         /// 是否已点击
     41         /// </summary>
     42         bool isDown = false;
     43         /// <summary>
     44         /// 点击可拖动的描点的控件
     45         /// </summary>
     46         Label[] labels = new Label[8];
     47 
     48         private Color barColor = Color.Black;
     49         /// <summary>
     50         /// 拖动块的颜色
     51         /// </summary>
     52         public Color BarColor
     53         {
     54             get => this.barColor;
     55             set
     56             {
     57                 for (int i = 0; i < labels.Length; i++)
     58                 {
     59                     labels[i].BackColor = value;
     60                 }
     61                 this.barColor = value;
     62             }
     63         }
     64         private bool labsVisible = false;
     65         /// <summary>
     66         /// 是否显示拖动块的描点
     67         /// </summary>
     68         public bool LabsVisible
     69         {
     70             get => labsVisible;
     71             set
     72             {
     73                 foreach (var lab in labels)
     74                 {
     75                     lab.Visible = value;
     76                 }
     77 
     78                 labsVisible = value;
     79             }
     80         }
     81         
     82         private int barSize = 7;
     83         /// <summary>
     84         /// 拖动块描点的大小
     85         /// </summary>
     86         public int BarSize
     87         {
     88             get => this.barSize;
     89             set
     90             {
     91                 foreach (var item in labels)
     92                 {
     93                     item.Size = new Size(value, value);
     94                 }
     95                 this.barSize = value;
     96             }
     97         }
     98         
     99         /// <summary>
    100         ///系统自带的可拖动描点
    101         /// </summary>
    102         Cursor[] cursors = new Cursor[]
    103         {
    104               //获取双向对角线(西北/东南)大小调整光标
    105               Cursors.SizeNWSE,
    106               //获取双向垂直(北/南)大小调整光标
    107               Cursors.SizeNS,
    108               //获取双向对角线(东北/西南)大小调整光标
    109               Cursors.SizeNESW,
    110               //获取双向水平(西/东)大小调整光标
    111               Cursors.SizeWE,
    112               Cursors.SizeNWSE,
    113               Cursors.SizeNS,
    114               Cursors.SizeNESW,
    115               Cursors.SizeWE,
    116         };
    117         
    118          public AddControlDragAttr(Control control)
    119         {
    120             CurrControl = control;
    121             CurrControl.MouseDown += control_MouseDown;
    122             CurrControl.MouseUp += control_MouseUp;
    123             CurrControl.MouseMove += control_MouseMove;
    124             for (int i = 0; i < labels.Length; i++)
    125             {
    126                 labels[i] = new Label
    127                 {
    128                     TabIndex = i,
    129                     BackColor = BarColor,
    130                     Cursor = cursors[i],
    131                     Text = "",
    132                     Visible = false,
    133                     //置顶
    134                 };
    135                 labels[i].MouseDown += label_MouseDown;
    136                 labels[i].MouseMove += label_MouseMove;
    137                 labels[i].MouseUp += label_MouseUp;
    138 
    139                 CurrControl.Parent.Controls.Add(labels[i]);
    140             }
    141         }
    142         
    143         //控件事件
    144         private void control_MouseDown(object sender, MouseEventArgs e)
    145         {
    146             downPoint = e.Location;
    147             isDown = true;
    148             LabsVisible = false;
    149 
    150         }
    151         
    152         private void control_MouseMove(object sender, MouseEventArgs e)
    153         {
    154             if (isDown)
    155             {
    156                 int x = CurrControl.Location.X + e.Location.X - downPoint.X;
    157                 int y = CurrControl.Location.Y + e.Location.Y - downPoint.Y;
    158 
    159                 CurrControl.Location = new Point(x, y);
    160                 LabsVisible = false;
    161             }
    162         }
    163         
    164         private void control_MouseUp(object sender, MouseEventArgs e)
    165         {
    166             int x = CurrControl.Location.X + e.Location.X - downPoint.X;
    167             int y = CurrControl.Location.Y + e.Location.Y - downPoint.Y;
    168 
    169             CurrControl.Location = new Point(x, y);
    170 
    171             SetBarBounds();
    172             isDown = false;
    173 
    174             LabsVisible = true;
    175 
    176             OtherLabelsVisibleEvent?.Invoke(null,null);
    177         }
    178         
    179         //控件拖动块描点事件
    180         private void label_MouseDown(object sender, MouseEventArgs e)
    181         {
    182             this.draging = true;
    183             this.normalLeft = CurrControl.Left;
    184             this.normalTop = CurrControl.Top;
    185             this.normalWidth = CurrControl.Width;
    186             this.normalHeight = CurrControl.Height;
    187             this.LabsVisible = false;
    188         }
    189         
    190         /// <summary>
    191         /// 0  1  2   
    192         /// 7     3   
    193         /// 6  5  4   
    194         /// </summary>
    195         /// <param name="sender"></param>
    196         /// <param name="e"></param>
    197         private void label_MouseMove(object sender, MouseEventArgs e)
    198         {
    199             int left = CurrControl.Left;
    200             int width = CurrControl.Width;
    201             int top = CurrControl.Top;
    202             int height = CurrControl.Height;
    203             if (draging)
    204             {
    205                 switch (((Control)sender).TabIndex)
    206                 {
    207                     case 0:
    208                         left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
    209                         top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
    210                         width = normalLeft + normalWidth - CurrControl.Left;
    211                         height = normalTop + normalHeight - CurrControl.Top;
    212                         break;
    213                     case 1:
    214                         top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
    215                         height = normalTop + normalHeight - CurrControl.Top;
    216                         break;
    217                     case 2:
    218                         width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
    219                         top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
    220                         height = normalTop + normalHeight - CurrControl.Top;
    221                         break;
    222                     case 3:
    223                         width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
    224                         break;
    225                     case 4:
    226                         width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
    227                         height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
    228                         break;
    229                     case 5:
    230                         height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
    231                         break;
    232                     case 6:
    233                         left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
    234                         width = normalLeft + normalWidth - CurrControl.Left;
    235                         height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
    236                         break;
    237                     case 7:
    238                         left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
    239                         width = normalLeft + normalWidth - CurrControl.Left;
    240                         break;
    241                 }
    242                 left = (left < 0) ? 0 : left;
    243                 top = (top < 0) ? 0 : top;
    244                 CurrControl.SetBounds(left, top, width, height);
    245             }
    246         }
    247         
    248         private void label_MouseUp(object sender, MouseEventArgs e)
    249         {
    250             draging = false;
    251             SetBarBounds();
    252             this.LabsVisible = true;
    253         }
    254         
    255         //设置拖动块描点的位置
    256         private void SetBarBounds()
    257         {
    258             int x = CurrControl.Left - BarSize;
    259             int y = CurrControl.Top - BarSize;
    260             int w = CurrControl.Width + BarSize;
    261             int h = CurrControl.Height + BarSize;
    262             int b = BarSize / 2;
    263             var px = new List<int>
    264             {
    265                 x + b,
    266                 x + w / 2,
    267                 x + w - b,
    268                 x + w - b,
    269                 x + w - b,
    270                 x + w / 2,
    271                 x + b,
    272                 x + b,
    273             };
    274             var py = new List<int>
    275             {
    276                 y + b,
    277                 y + b,
    278                 y + b,
    279                 y + h / 2,
    280                 y + h - b,
    281                 y + h - b,
    282                 y + h - b,
    283                 y + h / 2
    284             };
    285             
    286             for (int i = 0; i < labels.Length; i++)
    287             {
    288                 labels[i].SetBounds(px[i], py[i], BarSize, BarSize);
    289             }
    290         }
    291     }
    292 }

    调用实例:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.IO;
     7 using System.Linq;
     8 using System.Text;
     9 using System.Threading.Tasks;
    10 using System.Windows.Forms;
    11 
    12 namespace ControlTest
    13 {
    14     public partial class Form1 : Form
    15     {
    16         List<AddControlDragAttr> listcontrolsattr = new List<AddControlDragAttr>();
    17 
    18         public Form1()
    19         {
    20             InitializeComponent();
    21         }
    22 
    23         protected override void OnLoad(EventArgs e)
    24         {
    25             base.OnLoad(e);
    26             this.Controls.AddRange(new Control[] {
    27                 new UserControl1{ Name = "usercontrol1" },
    28                 new Button{ Name ="btn1", Size = new Size(250,100)
    29                 ,Text= "马克思和恩格斯对社会主义发展出了他们的理论体系,亦认为社会主义社会是资本主义社会向共产主义社会过渡的社会形态" },
    30                 new PictureBox{ Name="pic1", Size = new Size(200,100), Location = new Point(100,50)
    31                 ,BackgroundImage = Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "img.jpg"))
    32                 ,BackgroundImageLayout = ImageLayout.Stretch}
    33             });
    34             
    35             foreach (Control control in this.Controls)
    36             {
    37                 var controlattr = new AddControlDragAttr(control);
    38 
    39                 //将其他控件的描点隐藏
    40                 controlattr.OtherLabelsVisibleEvent += (sender1,e1) => {
    41                     SetOtherLabelVisible(control);
    42                 };
    43 
    44                 listcontrolsattr.Add(controlattr);
    45             }
    46         }
    47 
    48         public void SetOtherLabelVisible(Control control)
    49         {
    50             foreach (AddControlDragAttr item in listcontrolsattr)
    51             {
    52                 if (item.CurrControl.Name != control.Name)
    53                     item.LabsVisible = false;
    54             }
    55         }
    56 
    57     }
    58 }
  • 相关阅读:
    数学杂谈 #22
    Windows 下查看端口占用情况
    [转] VUE 的常用指令2
    [转] VUE 的常用指令
    26种sourcemap看花了眼?别急,理解这几个全弄懂
    [转] webpack 中的 loader
    [转] VUE 的常用指令3
    [转]VUE 之 Webpack 打包构建
    [转]VUE devTools 安装方法
    [转] webpack devtool 配置之 Source Map
  • 原文地址:https://www.cnblogs.com/chongde/p/11737690.html
Copyright © 2020-2023  润新知