基本信息
源码名称:C#拖拉控件及拖拽文件上传
源码大小:0.21M
文件格式:.rar
开发语言:C#
更新时间:2019-05-08
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
C#拖拉控件及拖拽文件上传,拖拉控件改变大小及改变位置不会超出父容器边界

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

namespace DropForms
{
    class DragFrame
    {
        private const int Box_Size = 8;
        private Color Box_Color = Color.White;
        private Control control;
        private Label[] label = new Label[8];

        private int Initial_Left;
        private int Initial_Top;

        private int Initial_Width;
        private int Initial_Height;

        private int Initial_X;
        private int Initial_Y;

        private bool dragging;
        private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,
   Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,
   Cursors.SizeNESW, Cursors.SizeWE};
        private Cursor oldCursor;

        private const int Min_Size = 20;

        public DragFrame()
        {
            for(int i=0; i < 8; i  )
            {
                label[i] = new Label();
                label[i].TabIndex = i;
                label[i].FlatStyle = 0;
                label[i].BorderStyle = BorderStyle.FixedSingle;
                label[i].BackColor = Box_Color;
                label[i].Cursor = arrArrow[i];
                label[i].Text = "";
                label[i].BringToFront();
                label[i].MouseDown  = new MouseEventHandler(label_MouseDown);
                label[i].MouseMove  = new MouseEventHandler(this.label_MouseMove);
                label[i].MouseUp  = new MouseEventHandler(this.label_MouseUp);
            }
        }

        public void WireControl(Control ctl)
        {
            ctl.Click  = new EventHandler(this.SelectControl);
        }

        private void SelectControl(object sender, EventArgs e)
        {
            if(control is Control)
            {
                control.Cursor = oldCursor;
                //移除任何预先存在的事件处理程序
                control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
                control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
                control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
                control = null;
            }
            control = (Control)sender;
            //添加用于移动所选控件的事件处理程序
            control.MouseDown  = new MouseEventHandler(this.ctl_MouseDown);
            control.MouseMove  = new MouseEventHandler(this.ctl_MouseMove);
            control.MouseUp  = new MouseEventHandler(this.ctl_MouseUp);

            //将大小调整手柄添加到控件的容器
            for (int i = 0; i < 8; i  )
            {
                control.Parent.Controls.Add(label[i]);
                label[i].BringToFront();
            }
            //在控件周围放置大小调整手柄
            MoveHandles();
            //显示大小调整手柄
            ShowHandles();
            oldCursor = control.Cursor;
            control.Cursor = Cursors.SizeAll;
        }

        //设置大小调整手柄位置和大小
        private void MoveHandles()
        {
            int sX = control.Left - Box_Size;
            int sY = control.Top - Box_Size;
            int sW = control.Width   Box_Size;
            int sH = control.Height   Box_Size;
            int hB = Box_Size / 2;
            int[] arrPosX = new int[] { sX   hB, sX   sW / 2, sX   sW - hB, sX   sW - hB, sX   sW - hB, sX   sW / 2, sX   hB, sX   hB };
            int[] arrPosY = new int[] { sY   hB, sY   hB, sY   hB, sY   sH / 2, sY   sH - hB, sY   sH - hB, sY   sH - hB, sY   sH / 2 };
            for (int i = 0; i < 8; i  )
            {
                label[i].SetBounds(arrPosX[i], arrPosY[i], Box_Size, Box_Size);
            }                
        }

        //显示大小调整手柄
        private void ShowHandles()
        {
            if (control != null)
            {
                for (int i = 0; i < 8; i  )
                {
                    label[i].Visible = true;
                }
            }
        }

        //隐藏大小调整手柄
        private void HideHandles()
        {
            for (int i = 0; i < 8; i  )
            {
                label[i].Visible = false;
            }
        }
        
        /// <summary>
        /// 鼠标按下事件:记录当前控件位置和大小
        /// </summary>
        private void label_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            Initial_Left = control.Left;
            Initial_Top = control.Top;
            Initial_Height = control.Height;
            Initial_Width = control.Width;
            HideHandles();
        }

        /// <summary>
        /// 鼠标移动事件:根据拖动的大小调整手柄调整所选控件的大小
        /// 0   1   2
        /// 7       3
        /// 6   5   4
        /// </summary>
        private void label_MouseMove(object sender, MouseEventArgs e)
        {
            int left = control.Left;
            int top = control.Top;
            int width = control.Width;
            int height = control.Height;
            if (dragging)
            {
                switch (((Label)sender).TabIndex)
                {
                    case 0: // 拖动左上角的大小调整框
                        left = Initial_Left   e.X < Initial_Left   Initial_Width - Min_Size ? Initial_Left   e.X : Initial_Left   Initial_Width - Min_Size;
                        top = Initial_Top   e.Y < Initial_Top   Initial_Height - Min_Size ? Initial_Top   e.Y : Initial_Top   Initial_Height - Min_Size;
                        width = Initial_Left   Initial_Width - control.Left;
                        height = Initial_Top   Initial_Height - control.Top;
                        break;
                    case 1: // 拖动上中点大小调整框
                        top = Initial_Top   e.Y < Initial_Top   Initial_Height - Min_Size ? Initial_Top   e.Y : Initial_Top   Initial_Height - Min_Size;
                        height = Initial_Top   Initial_Height - control.Top;
                        break;
                    case 2: // 拖动右上角的大小调整框
                        top = Initial_Top   e.Y < Initial_Top   Initial_Height - Min_Size ? Initial_Top   e.Y : Initial_Top   Initial_Height - Min_Size;
                        width = (Min_Size > Initial_Width   e.X ? Min_Size : (Initial_Width   e.X   left > control.Parent.ClientRectangle.Width ? control.Parent.ClientRectangle.Width - left : Initial_Width   e.X));
                        height = Initial_Top   Initial_Height - control.Top;
                        break;
                    case 3: // 拖动右中大小调整框
                        width = (Min_Size > Initial_Width   e.X ? Min_Size : (Initial_Width   e.X   left > control.Parent.ClientRectangle.Width ? control.Parent.ClientRectangle.Width - left : Initial_Width   e.X));
                        break;
                    case 4: // 拖动右下角的大小调整框
                        width = (Min_Size > Initial_Width   e.X ? Min_Size : (Initial_Width   e.X   left > control.Parent.ClientRectangle.Width ? control.Parent.ClientRectangle.Width - left : Initial_Width   e.X));
                        height = Initial_Height   e.Y > Min_Size ? (Initial_Height   e.Y   top > control.Parent.ClientRectangle.Height ? control.Parent.ClientRectangle.Height - top : Initial_Height   e.Y) : Min_Size;
                        break;
                    case 5: // 拖动底部中心大小调整框
                        height = Initial_Height   e.Y > Min_Size ? (Initial_Height   e.Y   top > control.Parent.ClientRectangle.Height ? control.Parent.ClientRectangle.Height - top : Initial_Height   e.Y) : Min_Size;
                        break;
                    case 6: // 拖动左下调整框
                        left = Initial_Left   e.X < Initial_Left   Initial_Width - Min_Size ? Initial_Left   e.X : Initial_Left   Initial_Width - Min_Size;
                        width = Initial_Left   Initial_Width - control.Left;
                        height = Initial_Height   e.Y > Min_Size ? (Initial_Height   e.Y   top > control.Parent.ClientRectangle.Height ? control.Parent.ClientRectangle.Height - top : Initial_Height   e.Y) : Min_Size;
                        break;
                    case 7: // 拖动左中大小调整框
                        left = Initial_Left   e.X < Initial_Left   Initial_Width - Min_Size ? Initial_Left   e.X : Initial_Left   Initial_Width - Min_Size;
                        width = Initial_Left   Initial_Width - control.Left;
                        break;
                }
                left = (left < 0) ? 0 : left;
                top = (top < 0) ? 0 : top;
                control.SetBounds(left, top, width, height);
            }
        }

        /// <summary>
        /// 鼠标释放事件:显示大小调整完后所选控件周围的大小调整手柄
        /// </summary>
        private void label_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
            MoveHandles();
            ShowHandles();
        }

        /// <summary>
        /// 将鼠标指针置于鼠标下方的起始位置并隐藏大小调整手柄
        /// </summary>
        private void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            Initial_X = e.X;
            Initial_Y = e.Y;
            HideHandles();
        }

        /// <summary>
        /// 重新定位拖动的控件
        /// </summary>
        private void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                int left = control.Left   e.X - Initial_X;
                int top = control.Top   e.Y - Initial_Y;
                int width = control.Width;
                int height = control.Height;
                left = (left < 0) ? 0 : ((left   width > control.Parent.ClientRectangle.Width) ? control.Parent.ClientRectangle.Width - width : left);
                top = (top < 0) ? 0 : ((top   height > control.Parent.ClientRectangle.Height) ? control.Parent.ClientRectangle.Height - height : top);
                control.Left = left;
                control.Top = top;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;            
        }
    }
}