用鼠标移动图片框

发布于 2024-07-14 03:21:19 字数 409 浏览 13 评论 0原文

我正在开发一个适用于 Windows Mobile(Compact Framework 2.0)的应用程序。 它有一个带有 PictureBox 的 WinForms。

我想移动PictureBox的图像,但我不知道该怎么做,所以我选择移动PictureBox的洞。

为此,我使用此事件:

private void imagenMapa_MouseMove(object sender, MouseEventArgs e)
{
      imagenMapa.Left = e.X;
      imagenMapa.Top = e.Y;
      this.Refresh();
}

但是当我移动 PictureBox 时,它会闪烁并移动到各个位置。

我做错了什么?

I'm developing an app for windows mobile (Compact Framework 2.0). It has a WinForms with a PictureBox.

I want to move the image of the PictureBox but I don't know how to do it so I choose to move the hole PictureBox.

To do it I use this event:

private void imagenMapa_MouseMove(object sender, MouseEventArgs e)
{
      imagenMapa.Left = e.X;
      imagenMapa.Top = e.Y;
      this.Refresh();
}

But when I move the PictureBox it blinks and moves every where.

What I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦中楼上月下 2024-07-21 03:21:19

实际代码(需要 .NET Framework 3.5 及更高版本,不确定在 Compact Framework 中是否可用)...

// Global Variables
private int _xPos;
private int _yPos;
private bool _dragging;

// Register mouse events
pictureBox.MouseUp += (sender, args) =>
{
    var c = sender as PictureBox;
    if (null == c) return;
    _dragging = false;
};

pictureBox.MouseDown += (sender, args) =>
{
    if (args.Button != MouseButtons.Left) return;
    _dragging = true;
    _xPos = args.X;
    _yPos = args.Y;
};

pictureBox.MouseMove += (sender, args) =>
{
    var c = sender as PictureBox;
    if (!_dragging || null == c) return;
    c.Top = args.Y + c.Top - _yPos;
    c.Left = args.X + c.Left - _xPos;
};

Actual Code (Requires .NET Framework 3.5 and beyond, not sure if this is available in the Compact Framework)...

// Global Variables
private int _xPos;
private int _yPos;
private bool _dragging;

// Register mouse events
pictureBox.MouseUp += (sender, args) =>
{
    var c = sender as PictureBox;
    if (null == c) return;
    _dragging = false;
};

pictureBox.MouseDown += (sender, args) =>
{
    if (args.Button != MouseButtons.Left) return;
    _dragging = true;
    _xPos = args.X;
    _yPos = args.Y;
};

pictureBox.MouseMove += (sender, args) =>
{
    var c = sender as PictureBox;
    if (!_dragging || null == c) return;
    c.Top = args.Y + c.Top - _yPos;
    c.Left = args.X + c.Left - _xPos;
};
情未る 2024-07-21 03:21:19

eXeY 是相对于图片框的(例如,如果鼠标位于图片框的左上角,则为 0,0)。

imagenMapa.LeftimagenMapa.Top 的值是相对于表单(或包含 imagenMapa 的任何控件)的,

如果您尝试混合以下值这两个系统没有转换,你会得到跳跃(就像你所看到的)。

您最好将鼠标位置转换为包含图片框的物体所使用的相同坐标系。

您可以使用 imagenMapa.PointToScreen< /code>获取屏幕坐标中的鼠标坐标(或 Cursor.Position 直接获取位置),以及 yourForm.PointToClient 将它们恢复到表单坐标中。

请注意,根据您的需要,您可以通过覆盖/处理 控件的Paint事件并自己​​绘制图像。 如果这样做,您可以将所有内容保留在图片框坐标中,因为这些可能是您调用 graphicsObject.DrawImage

The e.X and e.Y are relative to the picture box (e.g. if the mouse is in the upper left of the picture box, that's 0,0) .

The values for imagenMapa.Left and imagenMapa.Top are relative to the form (or whatever control contains imagenMapa)

If you try to mix values from these two systems without conversion, you're going to get jumps (like you're seeing).

You're probably better off converting the mouse position to the same coordinate system used by the thing that contains the picture box.

You could use imagenMapa.PointToScreen to get the mouse coordinates in screen coordinates (or Cursor.Position to get the position directly), and yourForm.PointToClient to get them back in the form coordinates.

Note that depending on your needs, you could accomplish "moving an image within a control" by overriding/handling the Paint event of a control and drawing the image yourself. If you did this, you could keep everything in the picturebox coordinates, since those are likely what you would use when you called graphicsObject.DrawImage.

等往事风中吹 2024-07-21 03:21:19

eX& eY在pictureBox的坐标空间中,imagenMapa.Left & imagenMapa.Top位于Form的坐标空间中。 :-)

e.X & e.Y is in the coordinate space of the pictureBox, imagenMapa.Left & imagenMapa.Top is in the coordinate space of the Form. :-)

无法言说的痛 2024-07-21 03:21:19

另外不要忘记将表单设置为双缓冲,这可能有助于解决闪烁问题,但对于它的实际定位,我喜欢 Daniel L 的建议

Also don't forget to set your form to double buffered, that might help with the flickering, but for the actual positioning of it, I like Daniel L's suggestion

我们只是彼此的过ke 2024-07-21 03:21:19

拥抱数学!

control.Left = control.Left - (_lastMousePos.X - currentMousePos.X);
control.Top = control.Top - (_lastMousePos.Y - currentMousePos.Y);

快速解释:
您可以从鼠标位置获得差异并将其应用到您想要移动的对象。

例子:
如果旧的鼠标 X 位置是 382,新的鼠标 X 位置是 385,则差值是 -3。 如果控件当前 X 位置为 10,则 10 - (-3) = 13

为什么:
它适用于任何事情,并且比不断地来回转换坐标要便宜得多。

Embrace math!

control.Left = control.Left - (_lastMousePos.X - currentMousePos.X);
control.Top = control.Top - (_lastMousePos.Y - currentMousePos.Y);

Quick explanation:
You get the difference from the mouse positions and apply it to the object you want to move.

Example:
If the old mouse X position is 382, and the new one is 385, then the difference is -3. If the controls current X position is 10 then 10 - (-3) = 13

Why:
It works for anything, and is much cheaper than constantly converting coordinates back and forth.

梨涡 2024-07-21 03:21:19

其实你所做的都是正确的。 但是您将 MouseMove 属性赋予了图片框。 您应该将该属性赋予表单(背景)。

前任:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
  imagenMapa.Left = e.X;
  imagenMapa.Top = e.Y;
}

Actually what you have done is correct. But you gave the MouseMove property to the picturebox. You should give that property to the Form(background).

ex:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
  imagenMapa.Left = e.X;
  imagenMapa.Top = e.Y;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文