如何获得相对于鼠标位置的坐标位置? [英] How to get the position of a Coordinate with respect to Mouse position?
问题描述
我在Wpf上,并且有一个坐标列表,可在其中将它们绘制在位图图像上.我的位图文件是1000 * 1000
,它已填充在680 * 440
图像控件中.现在,我要做的是突出显示鼠标悬停在我的图像上时鼠标光标附近的坐标.
I am on Wpf and I have a list of coordinates where I draw them on a Bitmap Image. My Bitmap file is 1000 * 1000
and it gets filled in a 680 * 440
Image control. Now what I am trying to accomplish is to highlight the coordinates that are near the mouse cursor, when mouse is hoovering my Image.
在MouseMove()
事件处理程序上,我调用此函数并将鼠标相对于Image控件的位置传递给该函数:
on MouseMove()
event handler, I call this function and pass to it my mouse position with respect to the Image control:
public void HighLightNearbyDots(Point MousePosition)
{
int Distance;
CoordPoint temp = new CoordPoint();
temp.X = MousePosition.X;
temp.Y = MousePosition.Y;
foreach (var point in myDisplayedCoords)
{
Distance = (int)(temp - point); // using subtraction operator that I wrote
if (Distance < 10)
{
point.Color = Colors.Blue;
}
else
{
point.Color = InitialCoordColor; // Aqua
}
}
DrawImage();
}
是的,我在每次通话时都会重新绘制图像以反映所做的更改.也许问题是我需要缩放或计算1000 * 1000
文件大小和680 * 440
控件大小之间的某个比率才能达到确切的像素..但是我不确定是什么问题.自从早上以来,下面的当前结果就使我丧命.有人可以帮我解决这个问题吗?
Yes I redraw my image on every call to reflect the changes. Maybe the issue is that I need to scale or calculate some ratio between the 1000 * 1000
file size and the 680 * 440
control size to hit the exact pixel.. But I am not sure what is the issue. Below is the current result which is killing me since the morning. Could any one help me approach that?
推荐答案
Based on this How to scale a coordinate system? Now we know the equation. Then I use it this way:
int Distance;
CoordPoint temp = new CoordPoint();
temp.X = MousePosition.X / 660 * Bitmap.Width;
temp.Y = Bitmap.Height - (MousePosition.Y / 440 * Bitmap.Height); // y is flipped
这篇关于如何获得相对于鼠标位置的坐标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!