2012-10-01 5 views
2

시작점과 끝점 대신 시작점과 끝점이있는 사각형을 그릴 방법이 있습니까? 나는 마우스를 통해 양식에 사각형을 그립니다, 다음 코드를 사용하고 있습니다 :마우스로 직사각형 그리기

System.Drawing.Graphics formGraphics; 
    bool isDown = false; 
    int initialX; 
    int initialY; 

    private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     isDown = true; 
     initialX = e.X; 
     initialY = e.Y; 
    } 

    private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (isDown == true) 
     { 
      this.Refresh(); 
      Pen drwaPen = new Pen(Color.Navy,1); 
      int width = e.X - initialX, height = e.Y - initialY; 
      //if (Math.Sign (width) == -1) width = width 
      //Rectangle rect = new Rectangle(initialPt.X, initialPt.Y, Cursor.Position.X - initialPt.X, Cursor.Position.Y - initialPt.Y); 
      Rectangle rect = new Rectangle(initialX, initialY, width * Math.Sign(width), height * Math.Sign(height)); 

      formGraphics = this.CreateGraphics(); 
      formGraphics.DrawRectangle(drwaPen, rect); 
     } 
    } 
    private void Form1_MouseUp(object sender, MouseEventArgs e) 
    { 
     isDown = false; 
    } 

내가 출발점에서 다시 마우스를 이동할 때 사각형이 플립 수 있지만 대신이 일을해야한다,이 코드를 사용하여 사각형을 그릴 수 있습니다 내 사각형이 마우스 커서의 반대 방향으로 그려집니다. 짧은 있음이 코드는 정방향으로 직사각형을 그리는 동안 잘 작동하지만 뒤로 방향으로는 작동하지 않습니다.

+1

'OnPaint'에서 페인팅을하고'MouseMove' 이벤트에서 repaininting이 필요한 폼 영역을'Invalidate'해야합니다. 이렇게하면 깜박임을 멈추게됩니다. – Jamiec

답변

6

Jamiec 언급 한 바와 같이, 단지 MouseMove 핸들러에서 Invalidate를 호출하고 올바른 사각형 전달을 그립니다 OnPaint 방법/Paint 이벤트 핸들러

에 드로잉을하거나 뒤로,이 시도 :

Rectangle rect = new Rectangle(Math.Min(e.X, initialX), 
           Math.Min(e.Y, initialY), 
           Math.Abs(e.X - initialX), 
           Math.Abs(e.Y - initialY)); 
+0

그래, 이걸 확인하고 잘 작동합니다. 더 단순한 것보다 더 나은 방법 전체 양식을 무효화하면 사각형이 무효화됩니다. – Jamiec

+0

예. 이 잘 작동합니다. +1 –

0

이 코드를 사용하면 마우스를 사용하여 시작점과 끝점이있는 양식에 직사각형 또는 타원을 그릴 수 있습니다. Form에는 콤보 상자 만 있습니다.

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace FormsTestBed1 
{ 
    public partial class Form1 : Form 
    { 
     int _pressedLocationX; 
     int _pressedLocationY; 
     int _releasedLocationX; 
     int _releasedLocationY; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_MouseDown(object sender, MouseEventArgs e) 
     { 
      _pressedLocationX = e.X; 
      _pressedLocationY = e.Y; 
     } 

     private void Form1_MouseUp(object sender, MouseEventArgs e) 
     { 
      _releasedLocationX = e.X; 
      _releasedLocationY = e.Y; 

      using (Graphics graphics = base.CreateGraphics()) 
      using (SolidBrush solidbrush = new SolidBrush(Color.Magenta)) 
      using (Pen pen = new Pen(Color.DarkRed)) 
      { 
       graphics.Clear(SystemColors.Control); 

       switch (comboBox1.SelectedItem.ToString()) 
       { 
        case "Filled Square": 
         graphics.FillRectangle(solidbrush, 
          Math.Min(_pressedLocationX, _releasedLocationX), // Left 
          Math.Min(_pressedLocationY, _releasedLocationY), // Top 
          Math.Abs(_releasedLocationX - _pressedLocationX), // Width 
          Math.Abs(_releasedLocationY - _pressedLocationY)); // Height 
         break; 

        case "Filled Circle": 
         graphics.FillEllipse(solidbrush, 
          Math.Min(_pressedLocationX, _releasedLocationX), // Left 
          Math.Min(_pressedLocationY, _releasedLocationY), // Top 
          Math.Abs(_releasedLocationX - _pressedLocationX), // Width 
          Math.Abs(_releasedLocationY - _pressedLocationY)); // Height 
         break; 

        case "Line Square": 
         graphics.DrawRectangle(pen, 
          Math.Min(_pressedLocationX, _releasedLocationX), // Left 
          Math.Min(_pressedLocationY, _releasedLocationY), // Top 
          Math.Abs(_releasedLocationX - _pressedLocationX), // Width 
          Math.Abs(_releasedLocationY - _pressedLocationY)); // Height 
         break; 

        case "Line Circle": 
         graphics.DrawEllipse(pen, 
          Math.Min(_pressedLocationX, _releasedLocationX), // Left 
          Math.Min(_pressedLocationY, _releasedLocationY), // Top 
          Math.Abs(_releasedLocationX - _pressedLocationX), // Width 
          Math.Abs(_releasedLocationY - _pressedLocationY)); // Height 
         break; 
       } 
      } 
     } 
    } 
} 
+0

이 코드 스 니펫은 환영하며 도움을 줄 수도 있지만 * how * 및 * why * this에 대한 설명이 있으면 크게 향상됩니다 (// meta.stackexchange.com/q/114762). 문제를 해결합니다. 지금 묻는 사람뿐만 아니라 앞으로 독자에게 질문에 답하고 있다는 것을 기억하십시오! 설명을 추가하려면 답을 편집하고 어떤 제한 및 가정이 적용되는지 표시하십시오. –

관련 문제