日期:2014-05-17  浏览次数:21284 次

C# Winform Panel 拖动图片到Panel外
在C#的Winform中放一个panel;
在Panel中动态生成图片;
想要把图片拖出Panel外,且图片跟着鼠标移动;
利用MouseDown,MouseMove,MouseUp对图片进行拖动;
但不管怎么拖动图片,图片都不能离开Panel
跪求各位大师给个建议吧

 
Point p = new Point();
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            p.X = e.Location.X;
            p.Y = e.Location.Y;
            pictureBox1.MouseMove +=pictureBox1_MouseMove;
        }

        private void pictureBox1_MouseMove(object sender ,MouseEventArgs e) 
        {
            pictureBox1.Location = new Point(pictureBox1.Location.X + e.Location.X - p.X, pictureBox1.Location.Y + e.Location.Y - p.Y);
            pictureBox1.MouseUp += pictureBox1_MouseUp;
        }

        private  void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pictureBox1.Location = new Point(p.X, p.Y);
            pictureBox1.MouseMove -= pictureBox1_MouseMove;
        }
C#? Winform ?Panel? 拖动图片

------解决方案--------------------
使用Drag 和DropDown试试吧,将图片移动出去,相当于将图片从panel中删除,放到其他panel中,或者其他地方
------解决方案--------------------
想要把图片拖出Panel外,且图片跟着鼠标移动,请在鼠标移出Panel时,设置pictureBox.Parent为移入的容器
------解决方案--------------------
                Rectangle r1 = panel1.Bounds;
                Rectangle r2 = new Rectangle(panel1.Bounds.X + pictureBox1.Bounds.X, panel1.Bounds.Y + pictureBox1.Bounds.Y, pictureBox1.Bounds.Width,