0

我目前正在学习 C#,并选择作为一个项目来编写一个简单的颜色选择器控件;但是,自从我上次查看编写代码以来,情况发生了很大变化,并且我遇到了这个问题。

我在我的控件中使用 Mousedown 事件来获取鼠标坐标作为一个点 - 这工作正常并且完全返回我所期望的 - 相对于我的控件的鼠标坐标;但是,当我尝试对控件位置进行检查时,我返回一个值作为点,显示我的控件相对于表单的位置,在某些情况下,鼠标坐标将超出边界,因为它们的值将小于控件 IE 的相对起始位置我在控件中的像素 1,1 处单击 - 鼠标位置是 1,1 但因为控件相对于窗体位于 9,9 处,所以鼠标的位置小于边界控制的 - 我完全不知道如何解决这个问题,

4

1 回答 1

0

我自己设法解决了这个问题,所以我想我会发布答案,希望它可以帮助别人。我在获取相对于同一像素原点的点值时遇到问题:这对其进行了排序。

 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);
        }
     }
  }
于 2013-07-13T17:11:52.303 回答