简体   繁体   English

如何在图片框中的两点之间获取鼠标坐标

[英]How to get mouse coordinates between two points in a picture box

Can anyone help me on this? 谁可以帮我这个事?

I have a picture box with a image and this image have some coordinates. 我有一个带有图像的图片框,该图像具有一些坐标。 My X starts at 60 and end at 135 My Y stats at 75 and end at 120 我的X起始于60,结束于135我的Y统计值于75,结束于120

Because i have only the first and the last point, i want to calculate and see the coordinates when i mouse over my image. 因为我只有第一个点和最后一个点,所以当我将鼠标悬停在图像上时,我想计算并查看坐标。

I started with solving my first problem: i have to delimitate my start and my end. 我从解决我的第一个问题开始:我必须划定起点和终点。 So i tried a trackbar. 所以我尝试了一个轨迹栏。

Im trying first get the current X position: 我试图首先获得当前的X位置:

Set my picturebox at position x = 0; 将我的图片框设置在x = 0的位置;

Set my trackbar at position x = -10, so my first pin will be at position 0; 将轨迹栏设置在x = -10的位置,这样我的第一个图钉将在位置0;

Set my tracbar size.x = picturebox.x + 20, so my last pin will be at end of picture box. 设置我的笔迹大小。x = picturebox.x + 20,这样我的最后一个图钉将在图片框的结尾。

My trackbar have the current properties: Minimum = 60, Maximum = 135; 我的轨迹栏具有当前属性:最小值= 60,最大值= 135;

Set a mouse move event in my picturebox: 在我的图片框中设置鼠标移动事件:

 private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        double dblValue;
        dblValue = ((double)e.X/ (double)trackBar1.Width) * (trackBar1.Maximum - trackBar1.Minimum);
        dblValue = dblValue + 60;
        trackBar1.Value = Convert.ToInt32(dblValue);
        lblX.Text = dblValue.ToString();
    }

It's almost working, but still not very accurate. 它几乎可以正常工作,但仍然不是很准确。 Anyone have some idea that may work? 任何人都有可能可行的想法吗?

I'm not exactly sure what it is you are trying to do, but if you are trying to get to the coordinates within the picturebox, there is a function on the pictureBox class called PointToClient(Point) that computes the location of the specified screen point into client coordinates. 我不确定您要执行的操作是什么,但是如果您要获取PointToClient(Point)的坐标,则pictureBox类上有一个名为PointToClient(Point)的函数,该函数可以计算指定屏幕的位置指向客户坐标。 You can use the X and Y coordinates from the MouseEventArgs to create the Point object to pass to the function. 您可以使用MouseEventArgs的X和Y坐标来创建Point对象以传递给函数。

To clarify: 澄清:

The X and Y properties of the MouseEventArgs in the MouseMove event are the screen coordinates (0,0) being the upper left corner of the screen. MouseMove事件中MouseEventArgsXY属性是屏幕坐标(0,0),它是屏幕的左上角。

Many controls like the PictureBox control include a PointToClient method that will convert the screen coordinates to the local control's coordinates, where (0,0) will be the upper left corner of the control. 许多控件(例如PictureBox控件)都包含PointToClient方法,该方法会将屏幕坐标转换为本地控件的坐标,其中(0,0)将是控件的左上角。

So, for example, if your control is placed on the screen at location (60, 75) and has a bottom right coordinate of (135, 120). 因此,例如,如果您的控件放置在屏幕上的位置(60,75)处,并且其右下角坐标为(135,120)。 If your mouse is over the control, and is 10 pixels from the left and 20 pixels from the top, then the X and Y properties of the MouseEventArgs in the MouseMove event would be: X = 70 and Y = 95. If you convert these to the internal coordinates of the picturebox control using PointToClient , it will indicate that X = 10 and Y = 20. 如果鼠标悬停在控件上,并且位于左侧10像素和顶部20像素的位置,则MouseMove事件中MouseEventArgs的XY属性将为: X = 70和Y =95。如果将它们转换到使用PointToClient控件的内部坐标,它将指示X = 10和Y = 20。

Now, if you were wanting to have a TrackBar that displays a relative indication of where the X coordinate of the mouse is over some control, you would calculate it as follows: 现在,如果您想要一个TrackBar来显示鼠标X坐标位于某个控件上方的相对指示,则可以按以下方式进行计算:

// Set the minimum and maximum of the trackbar to 0 and 100 for a simple percentage.
trackBar1.Minimum = 0;
trackBar1.Maximum = 100;

// In the pictureBox1_MouseMove event have the following code:
trackBar1.Value = pictureBox1.PointToClient(new Point(e.X, e.Y)).X * 100 / pictureBox1.Width;

If you wanted to have the trackbar use screen coordinates to track the relative position of the X coordinate of the mouse over some control, you would calculate it as follows: 如果要让轨迹栏使用屏幕坐标来跟踪鼠标在某个控件上的X坐标的相对位置,则可以按以下方式进行计算:

// Set the minimum and maximum values of the track bar to the screen coordinates of the 
// control we want to track.
trackBar1.Minimum = pictureBox1.PointToScreen(0,0).X;
trackBar1.Maximum = pictureBox1.PointToScreen(pictureBox1.Width, 0).X;

// In the pictureBox1_MouseMove event have the following code:
trackBar1.Value = e.X;

If you wanted to have the trackbar use the internal coordinates of some control to track the internal position of the X coordinate of the mouse over that control, you would calculate it as follows: 如果要让轨迹栏使用某个控件的内部坐标来跟踪鼠标在该控件上方的X坐标的内部位置,则可以按以下方式进行计算:

// Set the minimum and maximum values of the track bar to zero and the width of the
// control we want to track.
trackBar1.Minimum = 0;
trackBar1.Maximum = pictureBox1.Width;

// In the pictureBox1_MouseMove event have the following code:
trackBar1.Value = pictureBox1.PointToClient(new Point(e.X, e.Y)).X;
// or - not recommended - read below.
trackBar1.Value = e.X - pictureBox1.Left;

Now, there is one caveat, and that is if you put controls inside other controls, like a panel inside a panel inside a panel, etc. then the 'world' coordinates of a control inside of another control are based upon their location within the parent control. 现在,有一个警告,就是如果您将控件放在其他控件内部,例如面板放在面板内部的面板中,等等,那么另一个控件内部的控件的“世界”坐标取决于它们在控件中的位置。父控件。 That is why it is a good idea to use the internal coordinates of the control via PointToClient and screen coordinates from internal coordinates via PointToScreen because otherwise you would have to work your way up through all of the containers until you reached the screen, keeping track of Top and Left coordinates all the way. 这就是为什么最好通过PointToClient使用控件的内部坐标,并通过PointToScreen来自内部坐标的屏幕坐标的原因,因为否则您将不得不在所有容器中向上移动,直到到达屏幕,并跟踪TopLeft始终保持坐标。

I hope this answers your question. 我希望这回答了你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM