在表单内拖动图片框 [英] Drag Picture box inside a form

查看:90
本文介绍了在表单内拖动图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 class Sensor:PictureBox
    {  public Sensor():base()
        {
            this.Size = new System.Drawing.Size(50, 50);
            this.Location = new System.Drawing.Point(50, 50);
            this.BackColor = Color.Red;
        }
}



Form1.cs


In Form1.cs

Sensor sn = new Sensor();
       public Form1()
       {
           InitializeComponent();
           this.Controls.Add(sn)
       }



这是我的课我希望在Form上拖动这个传感器。这个代码应该在类中,这可能吗?任何最短的方法?


this is my class i want Drag this sensor on Form . the code of this should be in the class itself Is that possible ? any shortest method ?

推荐答案

请从这里开始:

http://msdn.microsoft.com/en-us/library/ms171546%28v=vs.110%29.aspx [< a href =http://msdn.microsoft.com/en-us/library/ms171546%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ] ,

http://msdn.microsoft .com / zh-CN / library / aa984430%28v = vs.71%29.aspx [ ^ ]。



-SA
Please start here:
http://msdn.microsoft.com/en-us/library/ms171546%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/aa984430%28v=vs.71%29.aspx[^].

—SA


 class Sensor:PictureBox
    {
        private Point init_mouse, Curnt_mouse,init_loc;
        private bool IsDragable;
       
        public Sensor():base()
        {
            this.Size = new System.Drawing.Size(50, 50);
            this.Location = new System.Drawing.Point(50, 50);
            this.BackColor = Color.Red;
            this.MouseDown += new MouseEventHandler(Sensor_MouseDown);
            this.MouseUp += new MouseEventHandler(Sensor_MouseUp);
            this.MouseMove += new MouseEventHandler(Sensor_MouseMove);
            IsDragable = false;
           
        }

     
        void Sensor_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsDragable)
            {
                Curnt_mouse = Cursor.Position;
                int dx = Curnt_mouse.X - init_mouse.X;
                int dy = Curnt_mouse.Y - init_mouse.Y;
                this.Location = new Point(init_loc.X + dx, init_loc.Y + dy);
            }
        }

        void Sensor_MouseUp(object sender, MouseEventArgs e)
        {
            IsDragable = false;
            
        }

        void Sensor_MouseDown(object sender, MouseEventArgs e)
        {
            IsDragable = true;
            init_mouse= Cursor.Position;
            init_loc = this.Location;
        }

    }
}





试过这个并且工作了

如果有更好的选择,请建议。



tried this and worked
if any better option please suggest.


这篇关于在表单内拖动图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆