2013-07-11 8 views
0

저는 현재 C#을 배우고 있으며 간단한 색상 선택기 컨트롤을 작성하는 프로젝트로 선택했습니다. 그러나 필자는 코드를 작성하는 것을 마지막으로 살펴본 이후로이 문제를 겪었습니다.마우스 비교를위한 컨트롤에 의존하는 컨트롤 바운드 좌표를 올바르게 얻으려면 어떻게해야합니까?

마우스 컨트롤을 Point로 사용하기 위해 Mousedown 이벤트를 사용하고 있습니다. 이것은 정상적으로 작동하고 정확히 무엇이 기대되는지를 나타냅니다. 그러나 내가 시도하고 컨트롤 위치에 대한 검사를 할 때 나는 값이 작을 것입니다 때문에 어떤 경우에는 마우스 좌표가 범위를 벗어나는 양식에 상대적인 내 컨트롤의 위치를 ​​보여주는 지점으로 값을 반환합니다. 컨트롤 IE의 상대 시작 위치 컨트롤에서 픽셀 1,1을 클릭합니다. 마우스 위치는 1,1이지만 컨트롤은 폼에 상대적으로 9,9에 위치하기 때문에 마우스의 위치가 경계보다 작습니다 이 문제를 해결하는 방법을 전혀 모릅니다. PointToClient와 PointToScreen을 사용하여 별난 시도를 해왔습니다. 누군가가 내게 정신을 차릴 수 있도록 도와주세요.

+0

코드 예제가 도움이 될 것입니다. :) – RedEyedMonster

답변

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); 
     } 
    } 
    }