2010-05-06 5 views
0

어떤 도움이 필요합니까? 여기 내 코드는 다음과 같습니다마우스가 예상되는 위치에 그림을 그리지 않습니다. 그것은 컨트롤이 아닌 폼을 페인트합니다.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace Client 
{ 
    public partial class MainChat : Form 
    { 
     bool ShouldPaint = false; 

     public MainChat() 
     { 
      InitializeComponent(); 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
      this.ClientSize = new System.Drawing.Size(400, 400); 

      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); 
      this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp); 
      this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove); 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (ShouldPaint) 
      { 
       Graphics graphics = CreateGraphics(); 
       graphics.FillEllipse(new SolidBrush(Color.Black), e.X, e.Y, 10, 10); 
      } 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      ShouldPaint = false; 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      ShouldPaint = true; 
     } 
    } 
} 

답변

2

선을 수정

Graphics graphics = CreateGraphics(); 

Graphics graphics = pictureBox1.CreateGraphics(); 

당신이가에 그리는 이유 this.CreateGraphics() 말과 같은 형태의 그래픽을 얻을 수 있었다 방법

에 형태. 모든 컨트롤에는 사용자 정의 페인팅을위한 CreatGraphics() 메소드가 있습니다.

+0

고마워요! 매력처럼 일했습니다! 10 분이 지나면 이것을 해결할 것입니다. 대기 타이머가 있습니다. –

0

코드가 컨트롤이 아닌 폼을 페인트하도록 요청하고 있습니다.

받고있는 그래픽 객체는 형식이며 제어 할 수 없습니다.

관련 문제