0

私は現在 C# を学んでおり、単純なカラー ピッカー コントロールを作成するプロジェクトとして選択しました。しかし、私が最後にコードの記述を調べてから、状況は大幅に変化し、この問題に遭遇しました。

コントロールで Mousedown イベントを使用して、マウス座標を Point として取得しています。これは正常に機能しており、期待どおりの結果を返します。ただし、コントロールの位置をチェックしようとすると、フォームに対するコントロールの位置を示す Point として値が返されます。特定のケースでは、マウス座標が境界外になることがあります。コントロールの相対開始位置 IE コントロールのピクセル 1,1 をクリックします。マウスの位置は 1,1 ですが、コントロールはフォームに対して 9,9 の位置にあるため、マウスの位置は境界よりも小さくなります。コントロールの-これを修正する方法がまったくわかりません。

4

1 に答える 1

0

私はこれを自分で整理することができたので、答えを投稿すると思いました。うまくいけば、他の誰かを助けることができます。同じピクセルの原点に相対的な Point 値を取得する際に問題がありました。これによりソートされました。

 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 に答える