http://www.360doc.com/content/18/0516/12/55659281_754382494.shtml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms; namespace Toolcs
{
    class DragComponent
    {
        #region 实现窗体内的控件拖动
        //用法:在Form初始化或者Form_Load时先执行
        //DragComponent a = new DragComponent();
        //a.initProperty(groupBox1);
        //将界面groupBox1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件。          private Control control;
        const int Band = 5;
        const int MinWidth = 10;
        const int MinHeight = 10;
        private EnumMousePointPosition m_MousePointPosition;
        private Point p , p1;         private enum EnumMousePointPosition
        {             MouseSizeNone = 0 , //'无             MouseSizeRight = 1 , //'拉伸右边框             MouseSizeLeft = 2 , //'拉伸左边框             MouseSizeBottom = 3 , //'拉伸下边框             MouseSizeTop = 4 , //'拉伸上边框             MouseSizeTopLeft = 5 , //'拉伸左上角             MouseSizeTopRight = 6 , //'拉伸右上角             MouseSizeBottomLeft = 7 , //'拉伸左下角             MouseSizeBottomRight = 8 , //'拉伸右下角             MouseDrag = 9 // '鼠标拖动         }         private void MyMouseDown ( object sender , System.Windows.Forms.MouseEventArgs e )
        {             p.X = e.X;             p.Y = e.Y;             p1.X = e.X;             p1.Y = e.Y;         }         private void MyMouseLeave ( object sender , System.EventArgs e )
        {             m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;             control.Cursor = Cursors.Arrow;         }
        
        private EnumMousePointPosition MousePointPosition ( Size size , System.Windows.Forms.MouseEventArgs e )
        {             if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
            {                 if (e.X < Band)
                {                     if (e.Y < Band)
                    {
                        return EnumMousePointPosition.MouseSizeTopLeft;
                    }                     else
                    {                         if (e.Y > -1 * Band + size.Height)
                        {
                            return EnumMousePointPosition.MouseSizeBottomLeft;
                        }                         else
                        {
                            return EnumMousePointPosition.MouseSizeLeft;
                        }                     }                 }                 else
                {                     if (e.X > -1 * Band + size.Width)
                    {                         if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTopRight;
                        }                         else
                        {                             if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottomRight;
                            }                             else
                            {
                                return EnumMousePointPosition.MouseSizeRight;
                            }                         }                     }                     else
                    {                         if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTop;
                        }                         else
                        {                             if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottom;
                            }                             else
                            {
                                return EnumMousePointPosition.MouseDrag;
                            }                         }                     }                 }             }             else
            {
                return EnumMousePointPosition.MouseSizeNone;
            }         }         private void MyMouseMove ( object sender , System.Windows.Forms.MouseEventArgs e )
        {             Control lCtrl = (sender as Control);             if (e.Button == MouseButtons.Left)
            {                 switch (m_MousePointPosition)
                {                     case EnumMousePointPosition.MouseDrag:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Top = lCtrl.Top + e.Y - p.Y;                         break;                     case EnumMousePointPosition.MouseSizeBottom:                         lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeBottomRight:                         lCtrl.Width = lCtrl.Width + e.X - p1.X;                         lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeRight:                         lCtrl.Width = lCtrl.Width + e.X - p1.X;                         //      lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeTop:                         lCtrl.Top = lCtrl.Top + (e.Y - p.Y);                         lCtrl.Height = lCtrl.Height - (e.Y - p.Y);                         break;                     case EnumMousePointPosition.MouseSizeLeft:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Width = lCtrl.Width - (e.X - p.X);                         break;                     case EnumMousePointPosition.MouseSizeBottomLeft:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Width = lCtrl.Width - (e.X - p.X);                         lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeTopRight:                         lCtrl.Top = lCtrl.Top + (e.Y - p.Y);                         lCtrl.Width = lCtrl.Width + (e.X - p1.X);                         lCtrl.Height = lCtrl.Height - (e.Y - p.Y);                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeTopLeft:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Top = lCtrl.Top + (e.Y - p.Y);                         lCtrl.Width = lCtrl.Width - (e.X - p.X);                         lCtrl.Height = lCtrl.Height - (e.Y - p.Y);                         break;                     default:                         break;                 }                 if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;                 if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;             }             else
            {                 m_MousePointPosition = MousePointPosition(lCtrl.Size , e); //'判断光标的位置状态                 switch (m_MousePointPosition) //'改变光标
                {                     case EnumMousePointPosition.MouseSizeNone:                         control.Cursor = Cursors.Arrow;       //'箭头                         break;                     case EnumMousePointPosition.MouseDrag:                         control.Cursor = Cursors.SizeAll;     //'四方向                         break;                     case EnumMousePointPosition.MouseSizeBottom:                         control.Cursor = Cursors.SizeNS;      //'南北                         break;                     case EnumMousePointPosition.MouseSizeTop:                         control.Cursor = Cursors.SizeNS;      //'南北                         break;                     case EnumMousePointPosition.MouseSizeLeft:                         control.Cursor = Cursors.SizeWE;      //'东西                         break;                     case EnumMousePointPosition.MouseSizeRight:                         control.Cursor = Cursors.SizeWE;      //'东西                         break;                     case EnumMousePointPosition.MouseSizeBottomLeft:                         control.Cursor = Cursors.SizeNESW;    //'东北到南西                         break;                     case EnumMousePointPosition.MouseSizeBottomRight:                         control.Cursor = Cursors.SizeNWSE;    //'东南到西北                         break;                     case EnumMousePointPosition.MouseSizeTopLeft:                         control.Cursor = Cursors.SizeNWSE;    //'东南到西北                         break;                     case EnumMousePointPosition.MouseSizeTopRight:                         control.Cursor = Cursors.SizeNESW;    //'东北到南西                         break;                     default:                         break;                 }             }
        }         public  void initProperty (Control ctl)
        {
            control = ctl;             for (int i = 0 ; i < control.Controls.Count ; i++)
            {                 control.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);                 control.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave);                 control.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
            }
        }         #endregion     }
} 个人觉的这有问题独自修改
 class DragComponent
    {
        #region 实现窗体内的控件拖动
        //用法:在Form初始化或者Form_Load时先执行
        //DragComponent a = new DragComponent();
        //a.initProperty(groupBox1);
        //将界面groupBox1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件。         private Control control;
        const int Band = 5;
        const int MinWidth = 10;
        const int MinHeight = 10;
        private EnumMousePointPosition m_MousePointPosition;
        private Point p, p1;         public enum EnumMousePointPosition
        {             MouseSizeNone = 0, //'无             MouseSizeRight = 1, //'拉伸右边框             MouseSizeLeft = 2, //'拉伸左边框             MouseSizeBottom = 3, //'拉伸下边框             MouseSizeTop = 4, //'拉伸上边框             MouseSizeTopLeft = 5, //'拉伸左上角             MouseSizeTopRight = 6, //'拉伸右上角             MouseSizeBottomLeft = 7, //'拉伸左下角             MouseSizeBottomRight = 8, //'拉伸右下角             MouseDrag = 9 // '鼠标拖动         }         public void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {             p.X = e.X;             p.Y = e.Y;             p1.X = e.X;             p1.Y = e.Y;         }         public void MyMouseLeave(object sender, System.EventArgs e)
        {             m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;             control.Cursor = Cursors.Arrow;         }         public EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
        {             if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
            {                 if (e.X < Band)
                {                     if (e.Y < Band)
                    {
                        return EnumMousePointPosition.MouseSizeTopLeft;
                    }                     else
                    {                         if (e.Y > -1 * Band + size.Height)
                        {
                            return EnumMousePointPosition.MouseSizeBottomLeft;
                        }                         else
                        {
                            return EnumMousePointPosition.MouseSizeLeft;
                        }                     }                 }                 else
                {                     if (e.X > -1 * Band + size.Width)
                    {                         if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTopRight;
                        }                         else
                        {                             if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottomRight;
                            }                             else
                            {
                                return EnumMousePointPosition.MouseSizeRight;
                            }                         }                     }                     else
                    {                         if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTop;
                        }                         else
                        {                             if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottom;
                            }                             else
                            {
                                return EnumMousePointPosition.MouseDrag;
                            }                         }                     }                 }             }             else
            {
                return EnumMousePointPosition.MouseSizeNone;
            }         }         public void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {             Control lCtrl = (sender as Control);
            if (e.Button == MouseButtons.Left)
            {                 switch (m_MousePointPosition)
                {                     case EnumMousePointPosition.MouseDrag:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Top = lCtrl.Top + e.Y - p.Y;                         break;                     case EnumMousePointPosition.MouseSizeBottom:                         lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeBottomRight:                         lCtrl.Width = lCtrl.Width + e.X - p1.X;                         lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeRight:                         lCtrl.Width = lCtrl.Width + e.X - p1.X;                         //      lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeTop:                         lCtrl.Top = lCtrl.Top + (e.Y - p.Y);                         lCtrl.Height = lCtrl.Height - (e.Y - p.Y);                         break;                     case EnumMousePointPosition.MouseSizeLeft:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Width = lCtrl.Width - (e.X - p.X);                         break;                     case EnumMousePointPosition.MouseSizeBottomLeft:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Width = lCtrl.Width - (e.X - p.X);                         lCtrl.Height = lCtrl.Height + e.Y - p1.Y;                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeTopRight:                         lCtrl.Top = lCtrl.Top + (e.Y - p.Y);                         lCtrl.Width = lCtrl.Width + (e.X - p1.X);                         lCtrl.Height = lCtrl.Height - (e.Y - p.Y);                         p1.X = e.X;                         p1.Y = e.Y; //'记录光标拖动的当前点                         break;                     case EnumMousePointPosition.MouseSizeTopLeft:                         lCtrl.Left = lCtrl.Left + e.X - p.X;                         lCtrl.Top = lCtrl.Top + (e.Y - p.Y);                         lCtrl.Width = lCtrl.Width - (e.X - p.X);                         lCtrl.Height = lCtrl.Height - (e.Y - p.Y);                         break;                     default:                         break;                 }                 if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;                 if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;             }             else
            {                 m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态                 switch (m_MousePointPosition) //'改变光标
                {                     case EnumMousePointPosition.MouseSizeNone:                         control.Cursor = Cursors.Arrow;       //'箭头                         break;                     case EnumMousePointPosition.MouseDrag:                         control.Cursor = Cursors.SizeAll;     //'四方向                         break;                     case EnumMousePointPosition.MouseSizeBottom:                         control.Cursor = Cursors.SizeNS;      //'南北                         break;                     case EnumMousePointPosition.MouseSizeTop:                         control.Cursor = Cursors.SizeNS;      //'南北                         break;                     case EnumMousePointPosition.MouseSizeLeft:                         control.Cursor = Cursors.SizeWE;      //'东西                         break;                     case EnumMousePointPosition.MouseSizeRight:                         control.Cursor = Cursors.SizeWE;      //'东西                         break;                     case EnumMousePointPosition.MouseSizeBottomLeft:                         control.Cursor = Cursors.SizeNESW;    //'东北到南西                         break;                     case EnumMousePointPosition.MouseSizeBottomRight:                         control.Cursor = Cursors.SizeNWSE;    //'东南到西北                         break;                     case EnumMousePointPosition.MouseSizeTopLeft:                         control.Cursor = Cursors.SizeNWSE;    //'东南到西北                         break;                     case EnumMousePointPosition.MouseSizeTopRight:                         control.Cursor = Cursors.SizeNESW;    //'东北到南西                         break;                     default:                         break;                 }             }
        }         public void initProperty(Control ctl)
        {
            control = ctl;
            control.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
            control.MouseLeave +=  new System.EventHandler(MyMouseLeave);
            control.MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
           
        }         #endregion     }

C# 各种控件实现可拖动和调整大小的更多相关文章

  1. 【教程】【FLEX】#006 控件位置的拖动

    上一篇笔记学习了怎么从 把一个控件拖放到另外一个控件里面(即 A --> B里面). 而现在呢,则是学习  怎么把 A 拖到另外一个位置. (A -->A位置改变): 先说一下实现的思路( ...

  2. DEV 之 有些控件不允许拖动。

    DEV 之 有些控件不允许拖动.  设置一个参数即可解决问题

  3. WPF 控件库——可拖动选项卡的TabControl

    WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...

  4. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  5. C#设置一个控件可以鼠标拖动

    C#设置一个控件可以鼠标拖动: 新建一个C#项目, 创建一个label控件, 设置label的鼠标按下和抬起事件分别为:label1_MouseDown和label1_MouseUp. 对代码进行如下 ...

  6. 【转】C# 控件的自定义拖动、改变大小方法

    在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...

  7. C# 控件的自定义拖动、改变大小方法

    在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...

  8. Telerik 控件事例(鼠标拖动行,拖动列,设置行对齐,行宽,是否显示)

    People.cs using System;using System.Collections.Generic;using System.Data;using System.Linq;using Sy ...

  9. WinForm中TreeView控件实现鼠标拖动节点(可实现同级节点位置互换,或拖到目标子节点)

    ;//1:不同级, 不为1:拖同级 private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) { if (e.Button ...

随机推荐

  1. uvalive 3887 Slim Span

    题意: 一棵生成树的苗条度被定义为最长边与最小边的差. 给出一个图,求其中生成树的最小苗条度. 思路: 最开始想用二分,始终想不到二分终止的条件,所以尝试暴力枚举最小边的长度,然后就AC了. 粗略估计 ...

  2. Block 实践

    OC版 函数中无参无返回值 /* 作为函数参数类型的格式 返回值类型 (^)(形参列表) */ CZPerson.h - (void) test:(void (^)(void))block; CZPe ...

  3. Spark学习之路 (三)Spark之RDD

    一.RDD的概述 1.1 什么是RDD? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素 ...

  4. The Little Prince-12/03

    The Little Prince-12/03 These days, I am always busy with my things, including experiment and others ...

  5. 从实践出发:微服务布道师告诉你Spring Cloud与Spring Boot他如何选择

    背景 随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. 系统架 ...

  6. TCP/IP协议三次握手与四次挥手

    一.标志位和序号 seq序号 :发送方随机生成的 ack确认序号:ack=seq+1 标志位ACK=1时确认序号有效 SYN标志位:发起一个新连接 ACK标志位:确认序号有效 FIN标志位:断开连接 ...

  7. mysql判断两个时间段是否有交集

    //判断两个时间段是否有交集 private function checkTimeCross($start_time,$end_time){ $sql ) AND ((start_time > ...

  8. Linux内核Socket参数调优

    可调优的内核变量存在两种主要接口:sysctl命令和/proc文件系统,proc中与进程无关的所有信息都被移植到sysfs中.IPV4协议栈的sysctl参数主要是sysctl.net.core.sy ...

  9. 02:saltstack-api使用详解

    1.1 salt-api安装   参考博客:https://www.jianshu.com/p/012ccdff93cc 1.介绍 1. saltsatck本身就提供了一套算完整的api,使用 Che ...

  10. Linux基础笔记—— 走进Linux

    走进Linux 操作系统 操作系统是计算机中必不可少的基础系统软件,他的作用是管理和控制计算机系统中的硬件和软件资源,合理有效的组织系统的工作流程,在计算机系统(硬件)与使用者之间提供接口作用. 操作 ...