无为清净楼资源网 Design By www.qnjia.com

复制代码 代码如下:
using System.Windows.Forms;

namespace WinFormsApp_DragControls

{
    public class DragControl

    {

        //待拖动的控件

        private Control m_Control;

        //鼠标按下时的x,y坐标

        private int m_X;

        private int m_Y;

        public DragControl(Control control)
        {
            m_Control = control;

            m_Control.MouseDown += new MouseEventHandler(control_MouseDown);

            m_Control.MouseMove += new MouseEventHandler(contro_MouseMove);

        }

        private void control_MouseDown(object sender, MouseEventArgs e)
        {

            m_X = e.X;

            m_Y = e.Y;

        }
        private void contro_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                int x = e.X - m_X;

                int y = e.Y - m_Y;

                this.m_Control.Left += x;

                this.m_Control.Top += y;
            }
        }
    }
}

调用:

DragControl obj1 = new DragControl(button1);

则表示在运行的界面上,支持随意拖动button1

另外还可以进一步实现改变控件大小、GDI+实现加边界脚点、保存控件的位置到xml下次可以读取(布局)以及自动布局N个Control的算法等,想进一步了解可与本人联系,此处不多叙述..

标签:
随意,拖动,控件

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

更新日志