C#如何在運行時通過滑鼠拖動改變控制項的大小

來源:互聯網
上載者:User

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

namespace WindowsApplication2
{

    class ResizeAction
    {
        bool IsMoving = false;
        int ctrlLastWidth = 0;
        int ctrlLastHeight = 0;
        int ctrlWidth;
        int ctrlHeight;
        int ctrlLeft;
        int ctrlTop;
        int cursorL;
        int cursorT;
        int ctrlLastLeft;
        int ctrlLastTop;
        int Htap;
        int Wtap;
        bool ctrlIsResizing = false;
        System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
        private Control ctrl;
        private Form frm;
        public ResizeAction(Control c, Form frm)
        {
            ctrl = c;
            this.frm = frm;
            this.Htap = this.frm.Height - this.frm.ClientRectangle.Height;
            this.Wtap = this.frm.Width - this.frm.ClientRectangle.Width;
            ctrl.MouseDown += new MouseEventHandler(MouseDown);
            ctrl.MouseMove += new MouseEventHandler(MouseMove);
            ctrl.MouseUp += new MouseEventHandler(MouseUp);
        }
        public void MouseMove(object sender, MouseEventArgs e)
        {
            if (frm == null)
                return;
            if (e.Button == MouseButtons.Left)
            {
                if (this.IsMoving)
                {
                    if (ctrlLastLeft == 0)
                        ctrlLastLeft = ctrlLeft;
                    if (ctrlLastTop == 0)
                        ctrlLastTop = ctrlTop;
                    int locationX = (Cursor.Position.X - this.cursorL + this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Location.X);
                    int locationY = (Cursor.Position.Y - this.cursorT + this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Location.Y);
                    if (locationX < this.frm.DesktopLocation.X + this.Wtap)
                        locationX = this.frm.DesktopLocation.X + this.Wtap;
                    if (locationY < this.frm.DesktopLocation.Y + this.Htap)
                        locationY = this.frm.DesktopLocation.Y + this.Htap;
                    this.ctrlLeft = locationX;
                    this.ctrlTop = locationY;
                    ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft, this.ctrlLastTop);
                    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                    ctrlLastLeft = ctrlLeft;
                    ctrlLastTop = ctrlTop;
                    ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft, ctrlTop);
                    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                    return;
                }
                int sizeageX = (Cursor.Position.X - this.frm.DesktopLocation.X - this.Wtap - this.ctrl.Location.X);
                int sizeageY = (Cursor.Position.Y - this.frm.DesktopLocation.Y - this.Htap - this.ctrl.Location.Y);
                if (sizeageX < 2)
                    sizeageX = 1;
                if (sizeageY < 2)
                    sizeageY = 1;
                ctrlWidth = sizeageX;
                ctrlHeight = sizeageY;
                if (ctrlLastWidth == 0)
                    ctrlLastWidth = ctrlWidth;
                if (ctrlLastHeight == 0)
                    ctrlLastHeight = ctrlHeight;
                if (ctrlIsResizing)
                {
                    ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.ctrl.Left + this.Wtap, this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top);
                    ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth, ctrlLastHeight);
                }
                ctrlIsResizing = true;
                ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                ctrlLastWidth = ctrlWidth;
                ctrlLastHeight = ctrlHeight;
                ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Left, this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top);
                ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
            }
        }
        public void MouseDown(object sender, MouseEventArgs e)
        {
            if (frm == null)
                return;
            if (e.X < this.ctrl.Width - 10 || e.Y < this.ctrl.Height - 10)
            {
                this.IsMoving = true;
                this.ctrlLeft = this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Left;
                this.ctrlTop = this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top;
                this.cursorL = Cursor.Position.X;
                this.cursorT = Cursor.Position.Y;
                this.ctrlWidth = this.ctrl.Width;
                this.ctrlHeight = this.ctrl.Height;
            }
            ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft, this.ctrlTop);
            ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
            ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
        }
        public void MouseUp(object sender, MouseEventArgs e)
        {
            if (frm == null)
                return;
            ctrlIsResizing = false;
            if (this.IsMoving)
            {
                ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft, this.ctrlTop);
                ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                this.ctrl.Left = this.ctrlLeft - this.frm.DesktopLocation.X - this.Wtap;
                this.ctrl.Top = this.ctrlTop - this.frm.DesktopLocation.Y - this.Htap;
                this.IsMoving = false;
                this.ctrl.Refresh();
                return;
            }
            ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Left, this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top);
            ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
            ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
            this.ctrl.Width = ctrlWidth;
            this.ctrl.Height = ctrlHeight;
            this.ctrl.Refresh();
        }

    }
}

調用:
 private void Form1_Load(object sender, EventArgs e)
        {
            //WindowsApplication2.ResizeAction rs = new WindowsApplication2.ResizeAction(this.label1,this);
            WindowsApplication2.ResizeAction rs = new WindowsApplication2.ResizeAction(this.button1, this);  
        }
參考:http://www.cnblogs.com/DS-CzY/archive/2007/06/30/801377.aspx

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/yanleigis/archive/2007/10/11/1819447.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.