검색어 'Paint'에 대한 1 개의 검색 결과

  1. 2008/07/20 Paint 이벤트에 관한 고민을 하시나요? by 남정현

Paint 이벤트에서 그리는 함수가 아니지만 Paint 이벤트와 마찬가지로 작동하기 위해서는 그리는 방법을 다르게 구현해야 합니다. 이 문제 때문에 고민하시는 분이 꽤 있을 것으로 생각됩니다. 이 문제를 해결해줄 수 있는 클래스를 하나 소개합니다. 바로 Replay 클래스입니다.

Replay 클래스를 이용하여 멤버 메서드를 호출하면 모든 기록이 Replay 클래스 안에 보관되고, 나중에 ReplayWithoutResults 메서드를 호출하여 모든 메서드가 다시 호출될 수 있도록 하는 방식입니다. 이런 방법을 통하여 Paint 이벤트에서 그린 그림이 아니라도 복원이 가능합니다.

다음은 예제입니다.

// Graphics 객체를 Paint 이벤트가 아닌 곳에서 얻을 때,

// Replay 객체에 바로 집어넣습니다.

private void Form1_Load(object sender, EventArgs e)
{
    this.g = new Replay<Graphics>(Graphics.FromHwnd(this.panel1.Handle));
}

// Paint 이벤트에서는 Replay 객체가 가지고 있는 모든 메서드들을 호출합니다.

// g 객체의 ReplayWithoutResults 대신 정적 메서드를 부를 수도 있는데

// 이것은 Cross-Thread 호출을 위한 것입니다.

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Replay<Graphics>.ReplayWithoutResults(this, this.g);
}

private Random r = new Random();
private Replay<Graphics> g;

// Graphics.DrawLine을 Replay 컨테이너를 통해서 호출하도록 합니다.

private void timer1_Tick(object sender, EventArgs e)
{
    g.Invoke("DrawLine", new object[] {
        new Pen(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255))),
        new Point(r.Next(this.panel1.Width), r.Next(this.panel1.Height)),
        new Point(r.Next(this.panel1.Width), r.Next(this.panel1.Height)) });
}

이 소스 코드는 Apache License 버전 2.0 조건 아래에서 배포됩니다.

Creative Commons License
Creative Commons License
남정현 이 작성.

당신의 의견을 작성해 주세요.

[로그인][오픈아이디란?]
오픈아이디로만 댓글을 남길 수 있습니다