简体   繁体   中英

How can I display on a label the mouse coordinates over a pictureBox but as PointF and not as int?

In the paint event i have:

foreach (PointF pt in extendedPoints)
            {
                e.FillEllipse(Brushes.Red, (pt.X - distance) * (float)currentFactor, pt.Y * (float)currentFactor, 4f, 4f);//pt.X, pt.Y, 4f, 4f);
            }

extendedPoints is List for example, now pt.X = 181.856888 I draw some points over the pictureBox.

In form1 I did:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label4.Visible = true;
            label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
        }

What I want is to compare some points locations on the pictureBox with the locations/coordinates in the List extendedPoints.

But in Form1 eX and eY are type int. So instead 181.856888 I see only 181 and there are some points on the coordinate 181

Mouse/Touch coordinates are pixel based so they are always integral numbers.

If you are trying to allow user to click on graph, first translate raw values to on-screen coordinates (it should also account for scale factor).

So your data type would look somewhat like:

struct dataPoint
{
   float rawX, rawY;
   int X,Y;
}

and then in event you will compare click/tap data with on-screen coordinates.

foreach (dataPoint data in dataPointCollection)
{
  if (data.X==e.X && data.Y==e.Y)
  {
     //your code
  }
}

Pixels on the screen (and on the form) are actually only integers. There's no fractions in picture coordinates. For that reason, mouse pointer's coordinate can only be integers.

When you draw a shape based on float coordinates, the shape is anyway drawn using pixels again - it uses certain formulas to calculate where to put pixel and where not.

So to sum up, even though you're using float values, the pixels on the screen are always integers. You should translate your float values to integers for pixel-based comparisons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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