2013-04-12 4 views
5

저는 C# 프로그래밍에 익숙하지 않고 약간의 도움을 원합니다. 현재 마우스 왼쪽 단추로 Windows 응용 프로그램 양식에 그리는 색으로 채워진 사각형을 이동하려고하는데 마우스 오른쪽 단추를 사용하여 다른 위치로 끌어서 놓으려고합니다. 현재 직사각형을 그릴 수 있지만 마우스 오른쪽 버튼을 클릭하면 전체 양식이 드래그됩니다. 난 단지 마우스 오른쪽 버튼으로 사각형을 드래그 앤 드롭 할 필요가C#에서 마우스를 사용하여 도형을 그리고 이동하는 방법

public partial class Form1 : Form 
{ 
    private Point MouseDownLocation; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.DoubleBuffered = true; 
    } 
    Rectangle rec = new Rectangle(0, 0, 0, 0); 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec); 
    } 


    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      rec = new Rectangle(e.X, e.Y, 0, 0); 
      Invalidate(); 

     } 
     if (e.Button == MouseButtons.Right) 
     { 
      MouseDownLocation = e.Location; 
     } 
    } 
    protected override void OnMouseMove(MouseEventArgs e) 
    {    

     if (e.Button == MouseButtons.Left) 
     { 
      rec.Width = e.X - rec.X; 
      rec.Height = e.Y - rec.Y; 
      Invalidate(); 
     } 

     if (e.Button == MouseButtons.Right) 
     { 
      this.Left = e.X + this.Left - MouseDownLocation.X; 
      this.Top = e.Y + this.Top - MouseDownLocation.Y; 

     } 
    } 

} 

:

여기 내 코드입니다.

편집 : 당신이 내가 여기에 내 대답은 매우 신속하고있어 모두에게 감사 작동 코드입니다 :

public partial class Form1 : Form 
{ 
    private Point MouseDownLocation; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.DoubleBuffered = true;    
    } 

    Rectangle rec = new Rectangle(0, 0, 0, 0); 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec); 
     //Generates the shape    
    } 

    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     //can also use this one: 
     //if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      rec = new Rectangle(e.X, e.Y, 0, 0); 
      Invalidate(); 

     } 
     if (e.Button == MouseButtons.Right) 
     { 
      MouseDownLocation = e.Location; 
     } 
    } 
    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      rec.Width = e.X - rec.X; 
      rec.Height = e.Y - rec.Y; 
      this.Invalidate(); 
     } 

     if (e.Button == MouseButtons.Right) 
     { 
      rec.Location = new Point((e.X - MouseDownLocation.X) + rec.Left, (e.Y - MouseDownLocation.Y) + rec.Top); 
      MouseDownLocation = e.Location; 
      this.Invalidate(); 
     } 
    } 

} 

답변

2

이 작동하는 것 같다

protected override void OnMouseMove(MouseEventArgs e) 
    { 


     if (e.Button == MouseButtons.Left) 
     { 
      rec.Width = e.X - rec.X; 
      rec.Height = e.Y - rec.Y; 
      this.Invalidate(); 
     } 

     if (e.Button == MouseButtons.Right) 
     { 
      rec.Location = new Point((e.X-MouseDownLocation.X) + rec.Left, (e.Y-MouseDownLocation.Y) + rec.Top); 
      MouseDownLocation = e.Location; 
      this.Invalidate(); 
     } 
    } 
+0

작동합니다! 고맙습니다. –

2

이 하나의 시도 : 공지 사항을 그 Timertick이 Refresh()를 호출하는 마우스 이벤트에서 무효화하므로 양식 이 자신을 페인트합니다. 각 틱에서 ..

public partial class Form1 : Form 
{ 
    private Point MouseDownLocation; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.DoubleBuffered = true; 
     timer1.Start(); // add timer to the form 
    } 
    Rectangle rec = new Rectangle(0, 0, 0, 0); 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     Refresh(); 
    } 


    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
     rec.Width = e.X - rec.X; 
     rec.Height = e.Y - rec.Y; 
     } 
     else if (e.Button == MouseButtons.Right) 
     { 
     rec.X = e.X - MouseDownLocation.X; 
     rec.Y = e.Y - MouseDownLocation.Y; 
     } 
    } 

    protected override void OnMouseUp(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Right) 
      MouseDownLocation = e.Location; 
    } 
} 
+0

메신저로 나는 아직 타이머를 사용하지 않았습니다. 그것을 추가하는 것은 논리적 인 것처럼 보입니다. 그러나이 경우 Timer1을 어떻게 정의합니까? –

+0

WinForms에서 디자인보기로 가서 타이머 컨트롤을 양식 으로 드래그하면 아래에 표시됩니다. timerTick 메서드 을 두 번 클릭하기 만하면 폼이로드 될 때 또는 타이머가 시작될 때 타이머를 시작할 수 있습니다. timer.Start() 또는 timer.Enabled = true를 사용하여 c'tor를 작성하십시오. timer.Stop() 또는 timer.Enabled = false를 사용할 수있는 타이머를 중지하려면 – Elior

+0

이제 그 모든 작업을 완료했습니다. 시작하지만 아무것도 그려지지 않습니다 –