在C#窗体中如何实现图片的动态移动,即鼠标可拖动图片到任意位置,能否给出具体C#代码?多谢了

解决方案 »

  1.   

    你就控制picturebox的位置就行了,mousedown记录初始位置,mousemove记录新坐标
      

  2.   

    http://blog.163.com/lightyue_leyuan/blog/static/7513607720096299102502/
     楼主去看看吧 这有你要的例子
      

  3.   

    窗体的MouseMove事件        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                pictureBox1.Left = e.X;
                pictureBox1.Top = e.Y;
                this.Refresh();
            }
      

  4.   

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                pictureBox1.Paint+=new PaintEventHandler(pictureBox1_Paint);
                this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
            }
            float zoom;
            Point oldPoint,newPoint;
            bool IsDown;
            bool IsLoadBmp;
            Bitmap bmp;
            private void Form1_Load(object sender, EventArgs e)
            {
                IsLoadBmp = IsDown = false;
                zoom = 1.0f;
                oldPoint = new Point(0,0);
                newPoint = new Point(0,0);
            }
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {           
                Graphics g = e.Graphics;
                if (IsLoadBmp)
                {
                    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    rect.X = newPoint.X;
                    rect.Y = newPoint.Y;
                    rect.Width = (int)(rect.Width * zoom);
                    rect.Height = (int)(rect.Height * zoom);
                    g.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
                    g.DrawImage(bmp, rect, new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                }
                
            }
            private void OpenImg_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofg = new OpenFileDialog();
                ofg.Filter = "图片文件(*.bmp)|*.bmp;*.jpg;*.jpeg;*.png;*.gif";
                ofg.Multiselect = false;
                if (ofg.ShowDialog() == DialogResult.OK)
                {
                    Form1_Load(null, null);
                    Image img = Image.FromFile(ofg.FileName);
                    bmp = new Bitmap(img);
                    if (pictureBox1.Width < bmp.Width || pictureBox1.Height < bmp.Height)
                        zoom = Math.Min((float)pictureBox1.Width / (float)bmp.Width, (float)pictureBox1.Height / (float)bmp.Height);
                    else
                        zoom = 1.0f;
                    img.Dispose();
                    IsLoadBmp = true;
                    pictureBox1.Refresh();
                }
            }
            private void Form1_MouseWheel(object sender, MouseEventArgs e)
            {
                zoom += (float)e.Delta/1000;
                if (zoom < 0)
                    zoom = 0.0f;
                if (zoom > 100)
                    zoom = 10.0f;
                pictureBox1.Refresh();
            }        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                IsDown = true;
                oldPoint.X =e.Location.X-newPoint.X;
                oldPoint.Y = e.Location.Y - newPoint.Y;
                this.Cursor = Cursors.Hand;
            }        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (IsDown)
                {
                    Point temp = e.Location;
                    newPoint.X=temp.X - oldPoint.X;
                    newPoint.Y=temp.Y - oldPoint.Y;
                    pictureBox1.Refresh();
                }
            }        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                IsDown = false;
                this.Cursor = Cursors.Default;
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.google.com");
            }
        }
      

  5.   

    完整代码:
            int x0, y0;
            bool lbtclk = false;
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                x0 = e.X;
                y0 = e.Y;
                lbtclk = (e.Button == System.Windows.Forms.MouseButtons.Left);
            }        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (lbtclk)
                {
                    pictureBox1.Left += (e.X - x0);
                    pictureBox1.Top += (e.Y - y0);
     
                }
            }        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                lbtclk = false;
            }
      

  6.   

    在窗体中添加了一个panel,在panel中添加了多个picturebox,每个有一张图片,想实现鼠标任意拖动每一张图片。
      

  7.   

    弄个panel 随鼠标移动 ,图片放pannel上