How do I correctly get control bound coordinates relitive to the control for mouse comparison?

StackOverflow https://stackoverflow.com/questions/17596390

  •  02-06-2022
  •  | 
  •  

Question

I am presently learning C# and have chosen as a project to write a simple colour picker control ; however things have changed substantially since I last looked at writing code and I have struck this problem.

I am using the Mousedown event in my control to get the mouse coordinates as a Point - this is working fine and returns exactly what I would expect - the mouse coordinates relative to my control; however when I try and do a check against the control location I am returned a value as a Point showing the position of my control relative to the form which in certain cases the mouse coordinates will be outside the bounds of because their values will be less than the relative start position of the control I.E I click at pixel 1,1 in the control - the mouse position is 1,1 but because the control is located at 9,9 relative to the form the location of the mouse is less than the bounds of the control - I have absolutely no idea how to fix this, I have been attempting to sort it out with PointToClient and PointToScreen to no avail as they seem to come up with outlandish values can someone please help me out it's driving me insane.

Was it helpful?

Solution

I've managed to sort this out myself so thought I'd post the answer and it can hopefully help someone else. I was having problems getting Point values that were relative to the same pixel origin : this sorted it.

 private void ColourPicker_MouseDown(object sender, MouseEventArgs e)
 {   // Probably being paranoid but I am worried about scaling issues, this.Location
     // would return the same result as this mess but may not handle
     // scaling <I haven't checked>
     Point ControlCoord = this.PointToClient(this.PointToScreen(this.Location));
     int ControlXStartPosition = ControlCoord.X;
     int ControlYStartPosition = ControlCoord.Y;
     int ControlXCalculatedWidth = ((RectangleColumnsCount + 1) * WidthPerBlock ) + ControlXStartPosition;
     int ControlYCalculatedHeight = ((RectangleRowsCount   + 1) * HeightPerBlock) + ControlYStartPosition;

     // Ensure that the mouse coordinates are comparible to the control coordinates for boundry checks.
     Point ControlRelitiveMouseCoord  = this.ParentForm.PointToClient(this.PointToScreen(e.Location));
     int ControlRelitiveMouseXcoord = ControlRelitiveMouseCoord.X;
     int ControlRelitiveMouseYcoord = ControlRelitiveMouseCoord.Y;

     // Zero Relitive coordinates are used for caluculating the selected block location
     int ZeroRelitiveXMouseCoord = e.X;
     int ZeroRelitiveYMouseCoord = e.Y;

     // Ensure we are in the CALCULATED boundries of the control as the control maybe bigger than the painted area on
     // the design time form and we don't want to use unpaited area in our calculations.
     if((ControlRelitiveMouseXcoord > ControlXStartPosition) && (ControlRelitiveMouseXcoord < ControlXCalculatedWidth))
     {
        if((ControlRelitiveMouseYcoord > ControlYStartPosition) && (ControlRelitiveMouseYcoord < ControlYCalculatedHeight))
        {
            SetEvaluatedColourFromPosition(ZeroRelitiveXMouseCoord, ZeroRelitiveYMouseCoord);
        }
     }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top