俄罗斯贵宾会-俄罗斯贵宾会官网
做最好的网站

c# winform 一个可以用鼠标改变控件位置和大小的类,调用即可

                #region 越界处理
                if (mainFormPosition.X < 0)//左边越界
                {
                    mainFormPosition.X = 0;
                }
                else if (mainFormPosition.Y < 0)//上边越界
                {
                    mainFormPosition.Y = 0;
                }
                else if (mainFormPosition.X > workingAreaWidth - this.ClientSize.Width)//右边越界
                {
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                }
                else if (mainFormPosition.Y > workingAreaHeight - this.ClientSize.Height)//下边越界
                {
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                }
                //更新主窗口位置
                Location = mainFormPosition;
                #endregion
            }
        }
        #endregion

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace AllTest  
  10. {  
  11.     class BarcodeControl  
  12.     {  
  13.         #region private  
  14.   
  15.         private const int MIN_SIZE = 10; //对控件缩放的最小值   
  16.         private const int BOX_SIZE = 7;  //调整大小触模柄方框大小   
  17.   
  18.         public bool _IsCtrlKey = false;  
  19.         private TextBox _textbox;  
  20.         private Control _MControl = null;  
  21.         private bool _IsMouseDown = false;  
  22.         private Point _oPointClicked;  
  23.         private Color BOX_COLOR = Color.White;  
  24.         private Label[] _lbl = new Label[8];  
  25.         private int _startl, _startt, _startw, _starth;  
  26.         private bool _dragging;  
  27.         private Cursor[] _arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,   
  28.                                                     Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,   
  29.                                                     Cursors.SizeNESW, Cursors.SizeWE};  
  30.  
  31.         #endregion  
  32.   
  33.         #region 构造函数  
  34.   
  35.         /// <summary>   
  36.         /// 构造控件拖动对象   
  37.         /// </summary>   
  38.         /// <param name="moveControl">需要拖动的控件 </param>   
  39.         public BarcodeControl(Control moveControl)  
  40.         {  
  41.             //   
  42.             // TODO: 在此处添加构造函数逻辑   
  43.             //   
  44.             _MControl = moveControl;  
  45.             _MControl.MouseDown += new MouseEventHandler(this.Control_MouseDown);  
  46.             _MControl.MouseUp += new MouseEventHandler(this.Control_MouseUp);  
  47.             _MControl.MouseMove += new MouseEventHandler(this.Control_MouseMove);  
  48.             _MControl.Click += new System.EventHandler(this.Control_Click);  
  49.   
  50.             //构造8个调整大小触模柄   
  51.             for (int i = 0; i < 8; i++)  
  52.             {  
  53.                 _lbl[i] = new Label();  
  54.                 _lbl[i].TabIndex = i;  
  55.                 _lbl[i].FlatStyle = 0;  
  56.                 _lbl[i].BorderStyle = BorderStyle.FixedSingle;  
  57.                 _lbl[i].BackColor = BOX_COLOR;  
  58.                 _lbl[i].Cursor = _arrArrow[i];  
  59.                 _lbl[i].Text = "";  
  60.                 _lbl[i].BringToFront();  
  61.                 _lbl[i].MouseDown += new MouseEventHandler(this.handle_MouseDown);  
  62.                 _lbl[i].MouseMove += new MouseEventHandler(this.handle_MouseMove);  
  63.                 _lbl[i].MouseUp += new MouseEventHandler(this.handle_MouseUp);  
  64.             }  
  65.   
  66.             CreateTextBox();  
  67.             Create();  
  68.   
  69.             //Control_Click((object)sender, (System.EventArgs)e);   
  70.         }  
  71.  
  72.         #endregion  
  73.   
  74.         #region 需拖动控件键盘事件  
  75.   
  76.         private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)  
  77.         {  
  78.             if (e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 39 || e.KeyValue == 40)  
  79.             {  
  80.                 if (e.KeyValue == 37)  
  81.                     _MControl.Left -= 1;  
  82.                 if (e.KeyValue == 38)  
  83.                     _MControl.Top -= 1;  
  84.                 if (e.KeyValue == 39)  
  85.                     _MControl.Left += 1;  
  86.                 if (e.KeyValue == 40)  
  87.                     _MControl.Top += 1;  
  88.                 MoveHandles();  
  89.                 ControlLocality();  
  90.                 _MControl.Visible = true;  
  91.             }  
  92.   
  93.             if (e.KeyValue == 46)  
  94.             {  
  95.                 for (int i = 0; i < 8; i++)  
  96.                 {  
  97.                     _MControl.Parent.Controls.Remove(_lbl[i]);  
  98.                 }  
  99.                 _MControl.Parent.Controls.Remove(_MControl);  
  100.                 _textbox.Parent.Controls.Remove(_textbox);  
  101.             }  
  102.   
  103.             if (e.KeyValue == 17)  
  104.             {  
  105.                 _IsCtrlKey = true;  
  106.                 //MessageBox.Show("a");   
  107.             }  
  108.         }  
  109.  
  110.         #endregion  
  111.   
  112.         #region 需拖动控件鼠标事件  
  113.   
  114.         private void Control_Click(object sender, System.EventArgs e)  
  115.         {  
  116.             _textbox.Focus();  
  117.             _MControl = (sender as Control);  
  118.             MoveHandles();  
  119.   
  120.             if (_IsCtrlKey == false)  
  121.             {  
  122.                 for (int i = 0; i < _MControl.Parent.Controls.Count; i++)  
  123.                 {  
  124.                     if (_MControl.Parent.Controls[i].Text.Trim().Length == 0 && _MControl.Parent.Controls[i] is Label)  
  125.                     {  
  126.                         _MControl.Parent.Controls[i].Visible = false;  
  127.                     }  
  128.                 }  
  129.             }  
  130.         }  
  131.         private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)  
  132.         {  
  133.             _IsMouseDown = true;  
  134.             _oPointClicked = new Point(e.X, e.Y);  
  135.             for (int i = 0; i < 8; i++)  
  136.             {  
  137.                 _MControl.Parent.Controls.Add(_lbl[i]);  
  138.                 _lbl[i].BringToFront();  
  139.             }  
  140.             HideHandles();  
  141.         }  
  142.   
  143.         private void Control_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)  
  144.         {  
  145.             _IsMouseDown = false;  
  146.             MoveHandles();  
  147.             ShowHandles();  
  148.             _MControl.Visible = true;  
  149.         }  
  150.   
  151.         private void Control_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)  
  152.         {  
  153.             if (_IsMouseDown)  
  154.             {  
  155.                 int l = _MControl.Left + (e.X - _oPointClicked.X);  
  156.                 int t = _MControl.Top + (e.Y - _oPointClicked.Y);  
  157.                 int w = _MControl.Width;  
  158.                 int h = _MControl.Height;  
  159.                 l = (l < 0) ? 0 : ((l + w > _MControl.Parent.ClientRectangle.Width) ?  
  160.                     _MControl.Parent.ClientRectangle.Width - w : l);  
  161.                 t = (t < 0) ? 0 : ((t + h > _MControl.Parent.ClientRectangle.Height) ?  
  162.                     _MControl.Parent.ClientRectangle.Height - h : t);  
  163.                 _MControl.Left = l;  
  164.                 _MControl.Top = t;  
  165.                 ControlLocality();  
  166.             }  
  167.             //_MControl.Cursor=Cursors.SizeAll;   
  168.         }  
  169.  
  170.         #endregion  
  171.         #region 调整大小触模柄鼠标事件  
  172.   
  173.         private void handle_MouseDown(object sender, MouseEventArgs e)  
  174.         {  
  175.             _dragging = true;  
  176.             _startl = _MControl.Left;  
  177.             _startt = _MControl.Top;  
  178.             _startw = _MControl.Width;  
  179.             _starth = _MControl.Height;  
  180.             HideHandles();  
  181.         }  
  182.         // 通过触模柄调整控件大小   
  183.         //    0  1  2   
  184.         //  7      3   
  185.         //  6  5  4   
  186.         private void handle_MouseMove(object sender, MouseEventArgs e)  
  187.         {  
  188.             int l = _MControl.Left;  
  189.             int w = _MControl.Width;  
  190.             int t = _MControl.Top;  
  191.             int h = _MControl.Height;  
  192.             if (_dragging)  
  193.             {  
  194.                 switch (((Label)sender).TabIndex)  
  195.                 {  
  196.                     //l算法:控件左边X坐标 + 鼠标在触模柄X坐标 < 控件左边X坐标 + 父控件宽度 - 控件大小 ?控件左边X坐标 + 鼠标在触模柄X坐标 :控件左边X坐标 + 父控件宽度 - 控件大小    
  197.                     //t算法:   
  198.                     //w算法:   
  199.                     //h算法:   
  200.                     case 0: // _dragging top-left sizing box   
  201.                         l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;  
  202.                         t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;  
  203.                         w = _startl + _startw - _MControl.Left;  
  204.                         h = _startt + _starth - _MControl.Top;  
  205.                         break;  
  206.                     case 1: // _dragging top-center sizing box   
  207.                         t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;  
  208.                         h = _startt + _starth - _MControl.Top;  
  209.                         break;  
  210.                     case 2: // _dragging top-right sizing box   
  211.                         w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;  
  212.                         t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;  
  213.                         h = _startt + _starth - _MControl.Top;  
  214.                         break;  
  215.                     case 3: // _dragging right-middle sizing box   
  216.                         w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;  
  217.                         break;  
  218.                     case 4: // _dragging right-bottom sizing box   
  219.                         w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;  
  220.                         h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;  
  221.                         break;  
  222.                     case 5: // _dragging center-bottom sizing box   
  223.                         h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;  
  224.                         break;  
  225.                     case 6: // _dragging left-bottom sizing box   
  226.                         l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;  
  227.                         w = _startl + _startw - _MControl.Left;  
  228.                         h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;  
  229.                         break;  
  230.                     case 7: // _dragging left-middle sizing box   
  231.                         l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;  
  232.                         w = _startl + _startw - _MControl.Left;  
  233.                         break;  
  234.                 }  
  235.                 l = (l < 0) ? 0 : l;  
  236.                 t = (t < 0) ? 0 : t;  
  237.                 _MControl.SetBounds(l, t, w, h);  
  238.             }  
  239.         }  
  240.   
  241.         private void handle_MouseUp(object sender, MouseEventArgs e)  
  242.         {  
  243.             _dragging = false;  
  244.             MoveHandles();  
  245.             ShowHandles();  
  246.         }  
  247.  
  248.         #endregion  
  249.   
  250.         #region private方法  
  251.   
  252.         private void Create()  
  253.         {  
  254.             //_IsMouseDown = true;   
  255.             //_oPointClicked = new Point(e.X,e.Y);   
  256.             for (int i = 0; i < 8; i++)  
  257.             {  
  258.                 _MControl.Parent.Controls.Add(_lbl[i]);  
  259.                 _lbl[i].BringToFront();  
  260.             }  
  261.             HideHandles();  
  262.         }  
  263.   
  264.         private void CreateTextBox()  
  265.         {  
  266.             _textbox = new TextBox();  
  267.             _textbox.CreateControl();  
  268.             _textbox.Parent = _MControl.Parent;  
  269.             _textbox.Width = 0;  
  270.             _textbox.Height = 0;  
  271.             _textbox.TabStop = true;  
  272.             _textbox.KeyDown += new System.Windows.Forms.KeyEventHandler(textBox_KeyDown);  
  273.         }  
  274.   
  275.         private void ControlLocality()  
  276.         {  
  277.             if (_MControl.Location.X < 0)  
  278.             {  
  279.                 _MControl.Visible = false;  
  280.                 _MControl.Left = 0;  
  281.             }  
  282.             if (_MControl.Location.Y < 0)  
  283.             {  
  284.                 _MControl.Visible = false;  
  285.                 _MControl.Top = 0;  
  286.             }  
  287.             if (_MControl.Location.X + _MControl.Width > _MControl.Parent.Width)  
  288.             {  
  289.                 _MControl.Visible = false;  
  290.                 _MControl.Left = _MControl.Parent.Width - _MControl.Width;  
  291.             }  
  292.             if (_MControl.Location.Y + _MControl.Height > _MControl.Parent.Height)  
  293.             {  
  294.                 _MControl.Visible = false;  
  295.                 _MControl.Top = _MControl.Parent.Height - _MControl.Height;  
  296.             }  
  297.         }  
  298.   
  299.         private void HideHandles()  
  300.         {  
  301.             for (int i = 0; i < 8; i++)  
  302.             {  
  303.                 _lbl[i].Visible = false;  
  304.             }  
  305.         }  
  306.   
  307.         private void MoveHandles()  
  308.         {  
  309.             int sX = _MControl.Left - BOX_SIZE;  
  310.             int sY = _MControl.Top - BOX_SIZE;  
  311.             int sW = _MControl.Width + BOX_SIZE;  
  312.             int sH = _MControl.Height + BOX_SIZE;  
  313.             int hB = BOX_SIZE / 2;  
  314.             int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,   
  315.                                           sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};  
  316.             int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,   
  317.                                           sY + sH-hB, sY + sH-hB, sY + sH / 2};  
  318.             for (int i = 0; i < 8; i++)  
  319.             {  
  320.                 _lbl[俄罗斯贵宾会,i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);  
  321.             }  
  322.         }  
  323.   
  324.         private void ShowHandles()  
  325.         {  
  326.             if (_MControl != null)  
  327.             {  
  328.                 for (int i = 0; i < 8; i++)  
  329.                 {  
  330.                     _lbl[i].Visible = true;  
  331.                 }  
  332.             }  
  333.         }  
  334.  
  335.         #endregion   
  336.     }  
  337. }  

 俄罗斯贵宾会 1

        #region 鼠标在按钮Button1抬起事件
        private void Button1_MouseUp(object sender, MouseEventArgs e)
        {
            isFirstDown = true;
            //如果主窗体在这个位置,就判断允许主窗体在鼠标抬起后执行移动事件
            if (mainFormPosition.X == workingAreaWidth - this.ClientSize.Width)
            {
                isMoveOut = false;
            }
            if (mainFormPosition.X == 0)
            {
                isMoveOut = false;
            }
        }
        #endregion
    }
}

C#代码 

        //-------------------------按钮button1鼠标事件---------------------------------------------------------------
        #region 鼠标左键按下Button1按钮事件
        private void Button1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentMousePosition = Control.MousePosition;
                oldMousePosition = currentMousePosition;   //鼠标按下时、两个时刻的坐标变成一样,防止错误移动            
                mainFormPosition = Location;
                if (isFirstDown)//鼠标按下时,记录第一次鼠标按下相对主窗体的坐标
                {
                    relativetMousePosition.X = currentMousePosition.X - Location.X;
                    relativetMousePosition.Y = currentMousePosition.Y - Location.Y;
                    isFirstDown = false;
                }
                if (isMoveOut)//鼠标按下时,记录拖出时的初始坐标
                {
                    oldMove_X = currentMousePosition.X;
                }
            }
        }
        #endregion

C#代码 

动画效果如下:

  1. AllTest.BarcodeControl barcode = new BarcodeControl(控件); //传入控件即可看到效果  

对窗体加越界限制后,鼠标拖动窗体越界时,窗体不能动,鼠标位置可动,但窗体不再越界时,鼠标位置还能回到鼠标按下时相对窗体的坐标:
1、首先创建一个窗体Form1,然后在窗体上拖一个button1按钮(主要通过这个按钮来拖动窗体)

接下来在一个窗体上放一个控件和按钮 
在按钮的单击事件里写 

本文由俄罗斯贵宾会发布于编程,转载请注明出处:c# winform 一个可以用鼠标改变控件位置和大小的类,调用即可

您可能还会对下面的文章感兴趣: