如何在C#中移动PictureBox?

SHi*_*Hiv 4 .net c# picturebox winforms

我已经使用此代码移动pictureBox_MouseMove事件上的图片框

pictureBox.Location = new System.Drawing.Point(e.Location);
Run Code Online (Sandbox Code Playgroud)

但是当我试图执行图片框闪烁时,无法识别确切的位置.你们可以帮助我吗?我希望图片框稳定......

Han*_*ant 5

您想要按鼠标移动的数量移动控件:

    Point mousePos;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
        mousePos = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            int dx = e.X - mousePos.X;
            int dy = e.Y - mousePos.Y;
            pictureBox1.Location = new Point(pictureBox1.Left + dx, pictureBox1.Top + dy);
        }
    }
Run Code Online (Sandbox Code Playgroud)

请注意,此代码并没有更新的MouseMove的mousePos结构变量.移动控件后需要更改鼠标光标的相对位置.