溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#怎么用鼠標(biāo)畫框創(chuàng)建控件

發(fā)布時間:2021-08-20 20:07:45 來源:億速云 閱讀:165 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“C#怎么用鼠標(biāo)畫框創(chuàng)建控件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“C#怎么用鼠標(biāo)畫框創(chuàng)建控件”吧!

首先定義Control _Owner,使用是的Control類型是因為我們不僅僅需要在Winform上增加控件,也需要在其它容器,比如Panel,GroupBox等上面增加容器,比如Panel,GroupBox等上面增加容器。

然后重新Control上的鼠標(biāo)事件Control_MouseDown,Control_MouseMove,Control_MouseUp,Control_MouseEnter。

直接上源碼

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace SumBLL.DrawControl
{
    #region 鼠標(biāo)拖動創(chuàng)建控件
    /// <summary>
    /// 鼠標(biāo)拖動創(chuàng)建事件
    /// </summary>
    public class MouseHook
    {
        Control _Owner;
        //要畫的子控件
        private Control _childCtrl;

        private bool _autosize;

        private int _CLickAtX;
        private int _ClickAtY;
        private int _MoveAtX;
        private int _MoveAtY;
        private bool _BeginDrag;
        private bool _BeginDrawControl;

        /// <summary>
        /// 對外公共類
        /// </summary>
        public Control DrawCtrl
        {
            get
            {
                return _childCtrl;
            }
            set
            {
                _childCtrl = value;
            }
        }
        /// <summary>
        /// 這里Owner使用的是Control類型,是因為我們不僅僅需要在Winform上增加控件,
        /// 也需要在其它容器,比如Panel,GroupBox等上面增加容器
        /// </summary>
        /// <param name="Owner"></param>
        public MouseHook(Control Owner, bool autosize = true)
        {
            this._Owner = Owner;
            this._Owner.MouseDown += new MouseEventHandler(this.Control_MouseDown);
            this._Owner.MouseMove += new MouseEventHandler(this.Control_MouseMove);
            this._Owner.MouseUp += new MouseEventHandler(this.Control_MouseUp);
            this._Owner.MouseEnter += new EventHandler(this.Control_MouseEnter);
            this._BeginDrawControl = false;
            this._autosize = autosize;
        }
        #region Control上的鼠標(biāo)事件
        void Control_MouseDown(object sender, MouseEventArgs e)
        {
            //判斷選擇控件,還是選擇畫筆
            if (_childCtrl != null)
            {
                this._CLickAtX = e.X;
                this._ClickAtY = e.Y;
                this._MoveAtX = e.X;
                this._MoveAtY = e.Y;
                this._BeginDrag = true;
                if (_childCtrl != null)
                {
                    this._BeginDrawControl = true;
                }
                else
                {
                    this._BeginDrawControl = false;
                }
            }
        }
        void Control_MouseMove(object sender, MouseEventArgs e)
        {
            if(_childCtrl != null)
            {
                if(this._BeginDrag)
                {
                    //取消上次繪制的選擇框
                    int iLeft, iTop, iWidth, iHeight;
                    Pen pen;
                    Rectangle rect;
                    pen = new Pen(this._Owner.BackColor);
                    if(this._BeginDrawControl == true)
                    {
                        pen.DashStyle = DashStyle.Solid;
                        pen.Width = 2;
                    }
                    else
                    {
                        pen.DashStyle = DashStyle.Dot;
                    }
                    iLeft = this._CLickAtX < this._MoveAtX ? this._CLickAtX : this._MoveAtX;
                    iTop = this._ClickAtY < this._MoveAtY ? this._ClickAtY : this._MoveAtY;
                    iWidth = Math.Abs(this._MoveAtX - this._CLickAtX);
                    iHeight = Math.Abs(this._MoveAtY - this._ClickAtY);
                    rect = new Rectangle(iLeft, iTop, iWidth, iHeight);
                    this._Owner.CreateGraphics().DrawRectangle(pen, rect);
                    //重新繪制選擇框
                    this._MoveAtX = e.X;
                    this._MoveAtY = e.Y;
                    pen = new Pen(Color.Black);
                    if(this._BeginDrawControl == true)
                    {
                        pen.DashStyle = DashStyle.Solid;
                        pen.Width = 2;
                    }
                    else
                    {
                        pen.DashStyle = DashStyle.Dot;
                    }
                    iLeft = this._CLickAtX < this._MoveAtX ? this._CLickAtX : this._MoveAtX;
                    iTop = this._ClickAtY < this._MoveAtY ? this._ClickAtY : this._MoveAtY;
                    iWidth = Math.Abs(this._MoveAtX - this._CLickAtX);
                    iHeight = Math.Abs(this._MoveAtY - this._ClickAtY);
                    rect = new Rectangle(iLeft, iTop, iWidth, iHeight);
                    this._Owner.CreateGraphics().DrawRectangle(pen, rect);
                }
            }
            
        }
        void Control_MouseUp(object sender, MouseEventArgs e)
        {
            this._BeginDrag = false;
            this._Owner.SuspendLayout();
            if(_childCtrl != null)
            {
                //取消上次繪制的選擇框
                int iLeft, iTop, iWidth, iHeight;
                Pen pen;
                Rectangle rect;
                pen = new Pen(this._Owner.BackColor);
                pen.DashStyle = DashStyle.Dot;
                iLeft = this._CLickAtX < this._MoveAtX ? this._CLickAtX : this._MoveAtX;
                iTop = this._ClickAtY < this._MoveAtY ? this._ClickAtY : this._MoveAtY;
                iWidth = Math.Abs(this._MoveAtX - this._CLickAtX);
                iHeight = Math.Abs(this._MoveAtY - this._ClickAtY);
                rect = new Rectangle(iLeft, iTop, iWidth, iHeight);
                this._Owner.CreateGraphics().DrawRectangle(pen, rect);
                if(_childCtrl != null)
                {
                    AddControl(_childCtrl, rect);
                }
                else
                {
                    //這里是拖動鼠標(biāo),選擇控件,這里將會在后續(xù)的介紹中給出
                }
                
            }
            this._Owner.Refresh();
            this._Owner.ResumeLayout();
        }
        void Control_MouseEnter(object sender, EventArgs e)
        {
            if(_childCtrl != null)
            {
                this._Owner.Cursor = Cursors.Cross;
            }
            else
            {
                this._Owner.Cursor = Cursors.Default;
            }
        }
        private void AddControl(Control control, Rectangle rect)
        {
            try
            {
                control.Location = rect.Location;
                if(!_autosize)
                    control.Size = rect.Size;
                //control.Name = GetControlName(control);
                //因為對于DataTimePiker控件來說不能設(shè)置.Text為非日期型,所以忽略錯誤
                try
                {
                    control.Text = GetControlType(control);
                }
                catch { }
                this._Owner.Controls.Add(control);
                control.Visible = true;
                this._Owner.Cursor = Cursors.Default;
                _childCtrl = null;
            }
            catch(Exception e)
            {
                this._Owner.Cursor = Cursors.Default;
                _childCtrl = null;
            }
        }
        private string GetControlType(Control ctrl)
        {
            string strType = ctrl.GetType().ToString();
            string strControlType;
            string[] strArr = strType.Split(".".ToCharArray());
            strControlType = strArr[strArr.Length - 1].Trim();
            return strControlType;
        }
        private string GetControlName(Control control)
        {
            //這里簡單返回控件名,如果需要,可以通過修改這個函數(shù)做特殊處理
            return control.GetType().Name;
        }
        #endregion
    }
    #endregion
}

外部調(diào)用方法

        //用于拖動控件的函數(shù)類,在窗體初始化時直接實例
        private MouseHook _MouseHook;

            //初始化Panel顯示動態(tài)巡更點
            this._MouseHook = new MouseHook(pnlRouteDetail);

                //創(chuàng)建用戶控件實例
                UCPatRouteDetail ucrd = new UCPatRouteDetail(_nowPatRouteDetail);
                ucrd.Name = _nowPatRouteDetail.RouteID + _nowPatRouteDetail.DetailID;
                ucrd.MouseDown += MouseDown;
                ucrd.MouseMove += MouseMove;
                ucrd.MouseUp += MouseUp;
                ucrd.Delevent += DelUserControl;
                //給畫圖地方賦值新實例
                _MouseHook.DrawCtrl = ucrd;

到此,相信大家對“C#怎么用鼠標(biāo)畫框創(chuàng)建控件”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI