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(你希望改变的控件);方法就行了。

时间: 2024-10-13 03:05:19

C#自定义大小与改变大下的方法的相关文章

如何压缩图片大小不改变像素

压缩图片实际上就是将图片的体积在一定的条件下进行压缩变小,这一过程一般都会对图片本身带来一定的改变,选择压缩的方法或者是工具的时候一定要考虑效果,有些压缩之后的图片已经完全不是之前的样子了,但是想要完全不改变像素也是不太实际的,下面小编为大家介绍一个图片压缩的软件,告诉你如何压缩图片大小不改变像素!具体方法如下:在线进行图片的压缩1:在浏览器中搜索在线图片压缩,打开网站之后点击在线图片压缩就可以进入操作界面. 2:添加要进行压缩的图片,点击或者拖拽都可以添加图片. 3:点击开始压缩的按钮,系统会

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

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

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

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

修改自定义大小的桌面屏幕分辨率

在ubuntu14.04虚拟机上修改自定义大小的桌面屏幕分辨率,使用的命令:cvt,xrandr 0.首先查看下当前已经提供的分辨率设置:xrandr -q [email protected]:/home/xxx/Desktop# xrandr -qScreen 0: minimum 1 x 1, current 1504 x 768, maximum 8192 x 8192Virtual1 connected primary 1504x768+0+0 (normal left inverted

Linux培训教程 linux系统下分割大文件的方法

在linux中分割大文件,比如一个5gb日志文件,需要把它分成多个小文件,分割后以利于普通的文本编辑器读取. 有时,需要传输20gb的大文件,Linux培训 教程件到另一台服务器,也需要把它分割成多个文件,这样便于传输数据. 以下通过五个不同的例子,来讲解Linux下分割大文件的方法,供大家参考. 例1.以每个文件1000行分割 split命令分割文件成每个文件1000行,并且文件名依次为 [前缀]aa,[前缀]ab, [前缀]ac等,默认的前缀是X,每个文件的行数为1000行. 命令: 复制代

SSIS:使用自定义的变量 改变 原有数据库连接的相应参数的值

1.创建变量,并设置初始值 2.在Connection Manager 中创建数据库连接 3.打开新建数据库连接的属性(单击该新建链接,按F4,或右击,选项 属性),点击Expressions 后面的 ...按钮 4.在弹出的Property Expressions Editor 窗体中,Property列中,在DropDownList中选择InitialCatelog和ServerName参数,然后点击相对应的Expression 列的 ...按钮 5.在新弹出的Expression Buil

不使用软件在Win7下查找大文件的方法

最近发现了一种可以不使用软件在win7系统下查找大文件的方法,分享如下: 第一步:打开我的电脑,进入要查找文件的磁盘.然后找到上边的搜索栏,如图: 第二步:在搜索栏中输入"大小:",如图: 第三步:输入完成后,在搜索栏下会弹出一个选项栏,可以选择查找文件的大小范围,如图: 第四步:可以直接选择需要查找的文件大小范围,如我选择巨大的查找结果如图: 第五步:如果感觉巨大的范围还是不够大,可以自己填写软件大小的查找范围,如"大小:>3GB"或"大小:>

最近开发老遇到莫名其妙的问题,dialog自定义大小,setAttributes这个方法没反应是肿么一回事

============问题描述============ 最近开发老遇到莫名其妙的问题,dialog自定义大小,setAttributes这个方法没反应是肿么一回事,我只想让dialog显示一部分,但是居然全屏占满了,很费解,以前开发都是这样写的,现在居然不可以了 这是dialog的code DownLoaderDialog dialog=new DownLoaderDialog(MainActivity.this,R.style.dialog); Window mwindow=dialog.g

如何使框架(Frame)大小无法改变

<html> <frameset rows="50%,50%"><frame noresize="noresize" src="a.html"><frame noresize="noresize" src="b.html"></frameset></frameset> </html> 如何使框架(Frame)大小无法改变,布