2013-06-29 6 views
-2

Windows Forms 응용 프로그램을 만들고 버튼 클릭시 모양을 그려야합니다. Button_Click 이벤트에서 Form_Paint를 호출하려면 어떻게해야합니까?단추 모양에 도형 그리기

답변

0

각 "도형"을 클래스 수준 목록의 GraphicsPath로 저장하는 빠른 예가 있습니다. 각 경로는 양식의 Paint() 이벤트에서 제공된 Graphics를 사용하여 그려집니다. 임의의 사각형은 자신을 다시 그리도록 강제하는 형태에 대해 호출 될 때마다 버튼을 클릭하고 새로 고침()와 함께> 목록 <에 추가됩니다

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
     this.Paint += new PaintEventHandler(Form1_Paint); 
    } 

    private Random R = new Random(); 
    private List<System.Drawing.Drawing2D.GraphicsPath> Paths = new List<System.Drawing.Drawing2D.GraphicsPath>(); 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Point pt1 = new Point(R.Next(this.Width), R.Next(this.Height)); 
     Point pt2 = new Point(R.Next(this.Width), R.Next(this.Height)); 

     System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath(); 
     shape.AddRectangle(new Rectangle(new Point(Math.Min(pt1.X,pt2.X), Math.Min(pt1.Y, pt2.Y)), new Size(Math.Abs(pt2.X - pt1.X), Math.Abs(pt2.Y - pt1.Y)))); 
     Paths.Add(shape); 

     this.Refresh(); 
    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     Graphics G = e.Graphics; 
     foreach (System.Drawing.Drawing2D.GraphicsPath Path in Paths) 
     { 
      G.DrawPath(Pens.Black, Path); 
     } 
    } 

} 
0

는 기본적으로 (이 SO 게시물을 읽을 심지어 손으로 페인트를 올리려면 무효 인

SO post: How do I call paint event?

그러나() 메소드), 당신은 아마 당신은 버튼 클릭에/명확한 플래그 내부 "drawshape"의 일종을 가지고해야합니다 전화하여 페인트, 심지어 처리기 메서드 내부 확인 . 이 플래그는 페인트 이벤트 핸들러를 호출하여 모양을 계속 그리거나 모양을 전혀 그리지 않습니다 (양식 페인트가 호출 될 때마다)