欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

C#实现控件拖动

介绍 评论 失效链接反馈

C#实现控件拖动 C#语言基础-第1张    public  class MoveControl
    {
        #region Constructors
        public MoveControl(Control ctrl,Form1 frm)
        {
            currentControl = ctrl;
            AddEvents();
        }
        #endregion        #region Fields
        private Control currentControl; //传入的控件
        private Point pPoint; //上个鼠标坐标
        private Point cPoint; //当前鼠标坐标
        FrameControl fc;//边框控件
        private Form1 form1 = null;        #endregion        #region Properties        #endregion        #region Methods
        /// <summary>
        /// 挂载事件
        /// </summary>
        private void AddEvents()
        {
            currentControl.MouseClick = new MouseEventHandler(MouseClick);
            currentControl.MouseDown = new MouseEventHandler(MouseDown);
            currentControl.MouseMove = new MouseEventHandler(MouseMove);
            currentControl.MouseUp = new MouseEventHandler(MouseUp);
        }        /// <summary>
        /// 绘制拖拉时的黑色边框
        /// </summary>
        public static void DrawDragBound(Control ctrl)
        {
            ctrl.Refresh();
            Graphics g = ctrl.CreateGraphics();
            int width = ctrl.Width;
            int height = ctrl.Height;
            Point[] ps = new Point[5]{new Point(0,0),new Point(width -1,0),
                new Point(width -1,height -1),new Point(0,height-1),new Point(0,0)};
            g.DrawLines(new Pen(Color.Black), ps);
        }           public  void RemoveFc()
        {
            currentControl = null;
            if (fc != null)
            {
                fc.Visible = false;
                fc.Dispose();
            }
        }        #endregion        #region Events
        /// <summary>
        /// 鼠标单击事件:用来显示边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MouseClick(object sender, MouseEventArgs e)
        {
            this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框
            this.currentControl.BringToFront();
            fc = new FrameControl(this.currentControl);
            this.currentControl.Parent.Controls.Add(fc);
            fc.Visible = true;
            fc.Draw();
            form1.moveControl = this;
        }        /// <summary>
        /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
        /// </summary>
        void MouseDown(object sender, MouseEventArgs e)
        {           
            pPoint = Cursor.Position;              
        }        /// <summary>
        /// 鼠标移动事件:让控件跟着鼠标移动
        /// </summary>
        void MouseMove(object sender, MouseEventArgs e)
        {
            Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll
            //当鼠标左键按下时才触发
            if (e.Button == MouseButtons.Left)
            {
                MoveControl.DrawDragBound(this.currentControl);
                if(fc != null ) fc.Visible = false; //先隐藏
                cPoint = Cursor.Position;//获得当前鼠标位置
                int x = cPoint.X - pPoint.X;
                int y = cPoint.Y - pPoint.Y;
                currentControl.Location = new Point(currentControl.Location.X x, currentControl.Location.Y y);
                pPoint = cPoint;
               
                Panel cr = (Panel)this.currentControl.Parent;//zxl
                Rectangle s = new Rectangle(1, 2, 1, 2);
                cr.Invalidate(s);
                cr.Update();
               
            }
        }        /// <summary>
        /// 鼠标弹起事件:让自定义的边框出现
        /// </summary>
        void MouseUp(object sender, MouseEventArgs e)
        {
            this.currentControl.Refresh();
            if (fc != null)
            {
                fc.Visible = true;
                fc.Draw();
            }
        }
        #endregion
    }

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复