用鼠标移动图片框
我正在开发一个适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
实际代码(需要 .NET Framework 3.5 及更高版本,不确定在 Compact Framework 中是否可用)...
Actual Code (Requires .NET Framework 3.5 and beyond, not sure if this is available in the Compact Framework)...
eX
和eY
是相对于图片框的(例如,如果鼠标位于图片框的左上角,则为 0,0)。imagenMapa.Left
和imagenMapa.Top
的值是相对于表单(或包含imagenMapa
的任何控件)的,如果您尝试混合以下值这两个系统没有转换,你会得到跳跃(就像你所看到的)。
您最好将鼠标位置转换为包含图片框的物体所使用的相同坐标系。
您可以使用
imagenMapa.PointToScreen< /code>
获取屏幕坐标中的鼠标坐标(或
Cursor.Position
直接获取位置),以及yourForm.PointToClient
将它们恢复到表单坐标中。请注意,根据您的需要,您可以通过覆盖/处理 控件的
Paint
事件并自己绘制图像。 如果这样做,您可以将所有内容保留在图片框坐标中,因为这些可能是您调用graphicsObject.DrawImage
。The
e.X
ande.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
andimagenMapa.Top
are relative to the form (or whatever control containsimagenMapa
)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 (orCursor.Position
to get the position directly), andyourForm.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 calledgraphicsObject.DrawImage
.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. :-)
另外不要忘记将表单设置为双缓冲,这可能有助于解决闪烁问题,但对于它的实际定位,我喜欢 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
拥抱数学!
快速解释:
您可以从鼠标位置获得差异并将其应用到您想要移动的对象。
例子:
如果旧的鼠标 X 位置是 382,新的鼠标 X 位置是 385,则差值是 -3。 如果控件当前 X 位置为 10,则 10 - (-3) = 13
为什么:
它适用于任何事情,并且比不断地来回转换坐标要便宜得多。
Embrace math!
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.
其实你所做的都是正确的。 但是您将 MouseMove 属性赋予了图片框。 您应该将该属性赋予表单(背景)。
前任:
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: