2013-06-07 3 views
0

마우스를 사용하여 영역을 선택하는 시스템을 만들었습니다. 내가 선택했을 때처럼 그러나이 지역은 같습니다, 나는 브라질 해요펜 C의 오류 #

http://i.imgur.com/xxc0ayn.png

죄송합니다 내 영어 ...

내 코드 :

private void ResizeSelection() 
    { 

     if (CurrentAction == ClickAction.LeftSizing) 
     { 

      if (Cursor.Position.X < CurrentBottomRight.X - 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentTopLeft.X = Cursor.Position.X; 
       RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 

     } 
     if (CurrentAction == ClickAction.TopLeftSizing) 
     { 

      if (Cursor.Position.X < CurrentBottomRight.X - 10 && Cursor.Position.Y < CurrentBottomRight.Y - 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentTopLeft.X = Cursor.Position.X; 
       CurrentTopLeft.Y = Cursor.Position.Y; 
       RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X; 
       RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 
     } 
     if (CurrentAction == ClickAction.BottomLeftSizing) 
     { 

      if (Cursor.Position.X < CurrentBottomRight.X - 10 && Cursor.Position.Y > CurrentTopLeft.Y + 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentTopLeft.X = Cursor.Position.X; 
       CurrentBottomRight.Y = Cursor.Position.Y; 
       RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X; 
       RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 

     } 
     if (CurrentAction == ClickAction.RightSizing) 
     { 

      if (Cursor.Position.X > CurrentTopLeft.X + 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentBottomRight.X = Cursor.Position.X; 
       RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 
     } 
     if (CurrentAction == ClickAction.TopRightSizing) 
     { 

      if (Cursor.Position.X > CurrentTopLeft.X + 10 && Cursor.Position.Y < CurrentBottomRight.Y - 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentBottomRight.X = Cursor.Position.X; 
       CurrentTopLeft.Y = Cursor.Position.Y; 
       RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X; 
       RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 
     } 
     if (CurrentAction == ClickAction.BottomRightSizing) 
     { 

      if (Cursor.Position.X > CurrentTopLeft.X + 10 && Cursor.Position.Y > CurrentTopLeft.Y + 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentBottomRight.X = Cursor.Position.X; 
       CurrentBottomRight.Y = Cursor.Position.Y; 
       RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X; 
       RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 
     } 
     if (CurrentAction == ClickAction.TopSizing) 
     { 

      if (Cursor.Position.Y < CurrentBottomRight.Y - 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentTopLeft.Y = Cursor.Position.Y; 
       RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 
     } 
     if (CurrentAction == ClickAction.BottomSizing) 
     { 

      if (Cursor.Position.Y > CurrentTopLeft.Y + 10) 
      { 

       //Erase the previous rectangle 
       g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 
       CurrentBottomRight.Y = Cursor.Position.Y; 
       RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y; 
       g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight); 

      } 

     } 

    } 

가 궁금 이 문제를 해결하거나 사각형의 가장자리 만 보이도록 투명하게 만드는 방법. 감사합니다.

+0

'g.Clear()'함수는 현재 직사각형이 다시 그려지기 전에 직사각형 화면을 지울 수 있습니까? – gwin003

+0

아니요, 테스트했습니다. 그것은 Color.Transparent를 사용하여 검은 색의 전체 화면을 남겨 둡니다. –

답변

2

Graphics을 잘못 사용하고 있습니다.

컨트롤에 그리려면 CreateGraphics()을 호출하면 안됩니다. 다음 페인트에서 지울 것입니다.

대신 Paint 이벤트를 처리하고 모든 다시 칠할 때 필요한 모든 것을 그려야합니다.

마우스가 움직일 때 Invalidate()으로 전화를 걸어 강제로 다시 칠하십시오.

+0

다시 선택하면 직사각형을 삭제합니다.이 사각형은 그대로 유지해야하며 필요에 따라 다시 만들어야합니다. –

+0

@MarlonRobertoColhado : 각 'Paint' 이벤트에서 _everything_을 다시 그리기해야합니다. 'List '에 그릴 것들 목록을 저장하십시오. 이렇게하지 않으면 일이 무작위로 사라집니다. – SLaks

+0

나는 지우개를 수정했다 : http://i.imgur.com/tQWY3xT.png 그러나, 일부는 놓치기도하고, 직사각형 안의 글자들은, 지우개가 붉은 색 다음에 투명도를 가진 색을 만들게 할 것이다. 펜? 또는 지우개의 불투명도. –