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

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

using System;
using System.Windows.Forms;
using System.Drawing;
namespace ControlSizeChangeEx
{
    /// <summary>
    /// This class implements sizing and moving functions for
    /// runtime editing of graphic controls
    /// </summary>
    public class PickBox
    {
        //////////////////////////////////////////////////////////////////
        // PRIVATE CONSTANTS AND VARIABLES
        //////////////////////////////////////////////////////////////////
        private const int BOX_SIZE = 8;
        private Color BOX_COLOR = Color.White;
        private ContainerControl m_container;
        private Control m_control;
        private Label[] lbl = new Label[8];
        private int startl;
        private int startt;
        private int startw;
        private int starth;
        private int startx;
        private int starty;
        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;
        //
        // Constructor creates 8 sizing handles & wires mouse events
        // to each that implement sizing functions
        //
        public PickBox()
        {
            for (int i = 0; i < 8; i++)
            {
                lbl[i] = new Label();
                lbl[i].TabIndex = i;
                lbl[i].FlatStyle = 0;
                lbl[i].BorderStyle = BorderStyle.FixedSingle;
                lbl[i].BackColor = BOX_COLOR;
                lbl[i].Cursor = arrArrow[i];
                lbl[i].Text = "";
                lbl[i].BringToFront();
                lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown);
                lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove);
                lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp);
            }
        }
        //////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //////////////////////////////////////////////////////////////////
        //
        // Wires a Click event handler to the passed Control
        // that attaches a pick box to the control when it is clicked
        //
        public void WireControl(Control ctl)
        {
            ctl.Click += new EventHandler(this.SelectControl);
        }
        /////////////////////////////////////////////////////////////////
        // PRIVATE METHODS
        /////////////////////////////////////////////////////////////////
        //
        // Attaches a pick box to the sender Control
        //
        private void SelectControl(object sender, EventArgs e)
        {
            if (m_control is Control)
            {
                m_control.Cursor = oldCursor;
                //Remove event any pre-existing event handlers appended by this class
                m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
                m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
                m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
                m_control = null;
            }
            m_control = (Control)sender;
            //Add event handlers for moving the selected control around
            m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
            m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
            m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp);
            //Add sizing handles to Control‘s <a href="http://lib.csdn.net/base/docker" class=‘replace_word‘ title="Docker知识库" target=‘_blank‘ style=‘color:#df3434; font-weight:bold;‘>Container</a> (Form or PictureBox)
            for (int i = 0; i < 8; i++)
            {
                m_control.Parent.Controls.Add(lbl[i]);
                lbl[i].BringToFront();
            }
            //Position sizing handles around Control
            MoveHandles();
            //Display sizing handles
            ShowHandles();
            oldCursor = m_control.Cursor;
            m_control.Cursor = Cursors.SizeAll;
        }
        public void Remove()
        {
            HideHandles();
            m_control.Cursor = oldCursor;
        }
        private void ShowHandles()
        {
            if (m_control != null)
            {
                for (int i = 0; i < 8; i++)
                {
                    lbl[i].Visible = true;
                }
            }
        }
        private void HideHandles()
        {
            for (int i = 0; i < 8; i++)
            {
                lbl[i].Visible = false;
            }
        }
        private void MoveHandles()
        {
            int sX = m_control.Left - BOX_SIZE;
            int sY = m_control.Top - BOX_SIZE;
            int sW = m_control.Width + BOX_SIZE;
            int sH = m_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++)
                lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
        }
        /////////////////////////////////////////////////////////////////
        // MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL
        /////////////////////////////////////////////////////////////////
        //
        // Store control position and size when mouse button pushed over
        // any sizing handle
        //
        private void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            startl = m_control.Left;
            startt = m_control.Top;
            startw = m_control.Width;
            starth = m_control.Height;
            HideHandles();
        }
        //
        // Size the picked control in accordance with sizing handle being dragged
        // 0   1   2
        //  7       3
        //  6   5   4
        //
        private void lbl_MouseMove(object sender, MouseEventArgs e)
        {
            int l = m_control.Left;
            int w = m_control.Width;
            int t = m_control.Top;
            int h = m_control.Height;
            if (dragging)
            {
                switch (((Label)sender).TabIndex)
                {
                    case 0: // Dragging top-left sizing box
                        l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
                        t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
                        w = startl + startw - m_control.Left;
                        h = startt + starth - m_control.Top;
                        break;
                    case 1: // Dragging top-center sizing box
                        t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
                        h = startt + starth - m_control.Top;
                        break;
                    case 2: // Dragging top-right sizing box
                        w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
                        t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
                        h = startt + starth - m_control.Top;
                        break;
                    case 3: // Dragging right-middle sizing box
                        w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
                        break;
                    case 4: // Dragging right-bottom sizing box
                        w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
                        h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
                        break;
                    case 5: // Dragging center-bottom sizing box
                        h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
                        break;
                    case 6: // Dragging left-bottom sizing box
                        l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
                        w = startl + startw - m_control.Left;
                        h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
                        break;
                    case 7: // Dragging left-middle sizing box
                        l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
                        w = startl + startw - m_control.Left;
                        break;
                }
                l = (l < 0) ? 0 : l;
                t = (t < 0) ? 0 : t;
                m_control.SetBounds(l, t, w, h);
            }
        }
        //
        // Display sizing handles around picked control once sizing has completed
        //
        private void lbl_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
            MoveHandles();
            ShowHandles();
        }
        /////////////////////////////////////////////////////////////////
        // MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM
        /////////////////////////////////////////////////////////////////
        //
        // Get mouse pointer starting position on mouse down and hide sizing handles
        //
        private void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            startx = e.X;
            starty = e.Y;
            HideHandles();
        }
        //
        // Reposition the dragged control
        //
        private void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                int l = m_control.Left + e.X - startx;
                int t = m_control.Top + e.Y - starty;
                int w = m_control.Width;
                int h = m_control.Height;
                l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ?
                  m_control.Parent.ClientRectangle.Width - w : l);
                t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ?
                m_control.Parent.ClientRectangle.Height - h : t);
                m_control.Left = l;
                m_control.Top = t;
            }
        }
        //
        // Display sizing handles around picked control once dragging has completed
        //
        private void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
            MoveHandles();
            ShowHandles();
        }
    }
} 

控件自定义拖动、改变大小

创建一个PickBox对象 ,  调用此对象的WireControl(你希望改变的控件);方法就行了。

转自:http://blog.csdn.net/ihaolau/article/details/6447991

时间: 2025-01-15 20:57:05

【转】C# 控件的自定义拖动、改变大小方法的相关文章

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

在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Windows.Forms; using System.Drawing; namespace ControlSizeChangeEx { /// <summary> /// This class implements sizing and moving functions for /// runtime

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

上一篇笔记学习了怎么从 把一个控件拖放到另外一个控件里面(即 A --> B里面). 而现在呢,则是学习  怎么把 A 拖到另外一个位置. (A -->A位置改变): 先说一下实现的思路(主要也就是思路,不是什么技术问题): 1.在控件拖动前要先注册好控件的MouseDown事件.(可以在控件创建好的时候注册,获取其他你觉得合适的时候) 2.当鼠标MouseDown下的时候(鼠标点击下去,未放开鼠标的时候),注册控件的面板的MouseMove和ROLL_UP事件以及控件的MouseUP. 3.

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

原文:WPF 控件库--可拖动选项卡的TabControl 一.先看看效果 二.原理 1.选项卡大小和位置 这次给大家介绍的控件是比较常用的TabControl,网上常见的TabControl样式有很多,其中一部分也支持拖动选项卡,但是带动画效果的很少见.这也是有原因的,因为想要做一个不失原有功能,还需要添加动画效果的控件可不是一行代码的事.要做成上图中的效果,我们不能一蹴而就,最忌讳的是一上来就想实现所有效果. 一开始,我们最好先用Blend看看原生的TabControl样式模板部分是如何实现

winform中,如何控制控件位置随窗体的大小改变而改变

winform中,如何控制控件位置随窗体的大小改变而改变 有如下3种方法: 方法1 [csharp] view plaincopy using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MarkPrint

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

DEV 之 有些控件不允许拖动.  设置一个参数即可解决问题 原文地址:https://www.cnblogs.com/struggle-cs/p/9935939.html

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

C#设置一个控件可以鼠标拖动: 新建一个C#项目, 创建一个label控件, 设置label的鼠标按下和抬起事件分别为:label1_MouseDown和label1_MouseUp. 对代码进行如下修改. public partial class Form1 : Form { private Point mouse_offset; public Form1() { InitializeComponent(); } private void label1_MouseUp(object sende

WP移动设备压缩与解压控件Xceed Zip for .NET Compact Framework控件下载及详细介绍使用方法

Xceed Zip for .NET Compact Framework 控件是一款健全的文件压缩和解压缩控件,提供了灵活的ZIP.gZip.流压缩,分割和合并ZIP,创建自定义解压文件. 具体功能: 完全支持.NET Compact Framework 2.0以及以上 100%可管理的代码,由C#编写,面向对象设计 支持在硬盘.内存.FTP站点里创建新的ZIP文件,或者更新存在的ZIP文件 完全兼容WinZip12,支持LZMA算法 支持Zip64 Zip文件格式,对文件大小没有限制 支持从硬

DELPHI控件:DBLookupComboBOX组件的使用方法

在许多数据表中,数据是以代码方式存放的,如在班级编码数据表tB03(表5.5)中,系部字段TB0309采用编码方式存放,系部真实名称则存放在系部编码表TB06.使用代码的好处是,用户可在编码表TB06中改变TB0602字段的系部名称,而不会影响使用该编码的其他数据表(如TB03)的运行6其缺点是当用户输人数据编码时,必须查询编码所表示的含义,如"OO"代表"基础部"."1 O"表示"机械系"等,这给数据录入带来很大的麻烦.最好

多功能节点连线绘图控件Nevron Diagram for .NET使用方法及下载地址

Nevron Diagram for .NET是一个功能强大,世界上顶级的.NET图表控件.可扩展的图形报表构架,可以帮您创建功能丰富的Winforms及Webforms图表解决方案.这个产品构建于Nevron表述层框架之上,能为您提供令人激动的视觉冲击,您无法通过其它产品体验到 - 独一无二的商业图表应用程序.Nevron Diagram for .NET专门根据广泛的自定义需求而设计,它提供了高扩展性的对象模型,其API更加细化本地化及直观性.产品本身大量利用现代的设计模式,使其具有更高的可