如何获得相对于鼠标位置的坐标位置? [英] How to get the position of a Coordinate with respect to Mouse position?

查看:171
本文介绍了如何获得相对于鼠标位置的坐标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

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