如何将图片框在C#中?如何将、图片

2023-09-02 21:12:18 作者:迷途不遇

我已经使用这个code键移动上的 pictureBox_MouseMove 事件图片框

i have used this code to move picture box on the pictureBox_MouseMove event

pictureBox.Location = new System.Drawing.Point(e.Location);

但是当我尝试执行该图片框和闪烁的确切位置无法确定。你们可以帮我一下吧。我要的照片框平稳...

but when i try to execute the picture box flickers and the exact position cannot be identified. can you guys help me with it. I want the picture box to be steady...

推荐答案

您希望通过鼠标移动的移动量控制:

You want to move the control by the amount that the mouse moved:

    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);
        }
    }

请注意,此code做的没有的更新的MouseMove的mousePos结构变量。必要的,因为移动控制改变鼠标光标的相对位置。

Note that this code does not update the mousePos variable in MouseMove. Necessary since moving the control changes the relative position of the mouse cursor.