2013-05-23 2 views
0

저는 C# 프로그래밍 초보자이며 Windowsform 응용 프로그램에서 사각형을 그립니다. 나는 마이크로 소프트 비주얼 스튜디오 2012를 사용하고이 예제 코드를 가지고 :C# fillrectangle MVS 2012

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

namespace WindowsFormsApplication2 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

    } 
    private void FillRectangleRectangle(PaintEventArgs e) 
    { 

     // Create solid brush. 
     SolidBrush blueBrush = new SolidBrush(Color.Blue); 

     // Create rectangle. 
     Rectangle rect = new Rectangle(0, 0, 200, 200); 

     // Fill rectangle to screen. 
     e.Graphics.FillRectangle(blueBrush, rect); 
    } 




    } 
    } 

하지만이 나던 단어, 누군가가 나를 도울 수 있습니까?

+1

그럼 'FillRectangleRectangle'을 * 호출하려고합니까? 정확히 "작동하지 않는다"는 것은 무엇을 의미합니까? 무슨 일이야? –

+0

폼의 Paint 이벤트에서 함수를 호출 해보십시오. –

답변

2

양식에 페인트 이벤트를 추가하고 함수 코드를 삽입하십시오 (올바른 것). 은 다음과 같아야합니다

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    SolidBrush blueBrush = new SolidBrush(Color.Blue); 
    Rectangle rect = new Rectangle(0, 0, 200, 200); 
    e.Graphics.FillRectangle(blueBrush, rect); 
} 

당신이 당신의 양식을 다시 그려야 할 때마다, 나는 당신이 당신이 페인트하려고하는 개체를 새로 고칠 필요가 있다고 생각

Invalidate(); 
+0

그냥 this.- thx this.Paint + = new PaintEventHandler (FillRectangleRectangle); – user2413594

+0

디자이너에게 맡겨라. 이것이 바로 그 것이다. 일반적으로 코드에서 이벤트를 할당 할 필요는 없습니다. –

-1

를 사용합니다. 이 중 하나를 시도하십시오

form.Invalidate(); 
form.Refresh(); 
+0

'Paint' 이벤트가 할당되지 않으면 도움이되지 않습니다 ... –

+0

@ThorstenDittmar 아, 그걸 보지 못했습니다. 죄송합니다. – uriDium

0

페인트해야 할 코드가 어디에도 호출되지 않았습니다. 양식의 Paint 이벤트가 그 것입니다. 해당 이벤트를 할당해야하며 코드에서 다음을 수행하십시오.

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    FillRectangleRectangle(e); 
}