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. 本地文件直接访问html,document.cookie总是显示空字符串

    当file:///E:/browser.html访问时,我们设置document.cookie,读取时一直会显示空字符串! 而开一下Apache时:localhost/browser.html或者12 ...

  2. java三大工厂结果总览

    2018-11-02 21:27:18 开始写 谢谢.Thank you.Salamat Do(撒拉玛特朵).あリがCám o*n(嘉蒙)とゥ(阿里嘎都).감사합니다 (勘三哈咪瘩).terima K ...

  3. MyBatis学习笔记(二)——使用MyBatis对表执行CRUD操作

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4262895.html 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用My ...

  4. 《nodejs开发指南》微博实例express4.x版

    之前一直执着于前端开发,最近几天,开始学起了nodejs.作为一名前端开发者,见到这样一门用javascript写的后台自然是很激动的.但是,后台毕竟不同于前端,在学习的过程中,还是会遇到不少问题. ...

  5. SQL数据分析概览——Hive、Impala、Spark SQL、Drill、HAWQ 以及Presto+druid

    转自infoQ! 根据 O’Reilly 2016年数据科学薪资调查显示,SQL 是数据科学领域使用最广泛的语言.大部分项目都需要一些SQL 操作,甚至有一些只需要SQL. 本文涵盖了6个开源领导者: ...

  6. C#中对Web.Config、App.Config字符串加密与解密的方法

    我们平常的项目里面的配置文件通常都是明文形式的存在,现在就是为了项目安全性增强,同时又显得高逼格点, 我们可以采用加密的方式,而我们C#很强大,因为他内置的一些指令方式,很方便而且使用起来还不用解密, ...

  7. linux OS与SQL修改时区,系统时间

    linux修改系统时间和linux查看时区.修改时区的方法 一.查看和修改Linux的时区 1. 查看当前时区命令 : "date -R" 2. 修改设置Linux服务器时区方法 ...

  8. Web开发笔记 #08# Jackson组合多个对象的属性构成JSON(以及添加自定义属性)

    参考文档:https://github.com/FasterXML/jackson-databind 关于ObjectMapper的线程安全 截自官方文档: 组合多个对象的属性构成JSON(以及添加自 ...

  9. selenium使用location定位元素坐标偏差

    python+selenium+Chromedriver使用location定位元素坐标偏差使用xpath定位元素,用.location获取坐标值,截取网页截图的一部分出现偏差. 之所以会出现这个坐标 ...

  10. 微信h5支付“网站域名ICP备案主体与商户号主体不一致”的解决方法,H5微信支付 授权函下载

    如下图所示: 微信h5支付“网站域名ICP备案主体与商户号主体不一致”: 需提交H5微信支付 授权函 下载地址:https://download.csdn.net/download/a72400815 ...