C#怎么用鼠标画框创建控件

发布时间:2021-08-20 20:07:45 作者:chen
来源:亿速云 阅读:164

本篇内容主要讲解“C#怎么用鼠标画框创建控件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#怎么用鼠标画框创建控件”吧!

首先定义Control _Owner,使用是的Control类型是因为我们不仅仅需要在Winform上增加控件,也需要在其它容器,比如Panel,GroupBox等上面增加容器,比如Panel,GroupBox等上面增加容器。

然后重新Control上的鼠标事件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 鼠标拖动创建控件
    /// <summary>
    /// 鼠标拖动创建事件
    /// </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上的鼠标事件
        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
                {
                    //这里是拖动鼠标,选择控件,这里将会在后续的介绍中给出
                }
                
            }
            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控件来说不能设置.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)
        {
            //这里简单返回控件名,如果需要,可以通过修改这个函数做特殊处理
            return control.GetType().Name;
        }
        #endregion
    }
    #endregion
}

外部调用方法

        //用于拖动控件的函数类,在窗体初始化时直接实例
        private MouseHook _MouseHook;

            //初始化Panel显示动态巡更点
            this._MouseHook = new MouseHook(pnlRouteDetail);

                //创建用户控件实例
                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#怎么用鼠标画框创建控件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Python OpenCV如何实现鼠标画框效果
  2. python opencv鼠标事件如何实现画框圈定目标获取坐标信息

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:docker容器技术是什么

下一篇:SQL2008中的MERGE语句用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》