如何将图片框在C#中? [英] How to move PictureBox in C#?

查看:129
本文介绍了如何将图片框在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这个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.

这篇关于如何将图片框在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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