SPATIUM Mobile
주소복사
About Operating System Languages Tools Favorites Notice Visit TEST  
     Android (3)
     Linux (1)
     MacOS (1)
     OS (1)
     Solaris10 (15)
     Windows (1)
     Windows Server (2)
     Windows XP (3)
   ID  
   Password  
  |  
  Location United States
  IP Address 3.15.221.67
2024. 04
123456
78910111213
141516171819
20
21222324252627
282930
Category  Languages, C#
Writer 김태우 Date 2016-01-28 17:24:13 Visit 5351
Flicker해결(DoubleBuffer) on C#

Flicker 해결 

 

Paint(VC++에서는 WM_PAINT)는 다른 창에 의해 어플리케이션이 가려지거나 최소화 시킨 후 복원할때 등등

어플리케이션이 화면에 다시 그려져야할때 자동으로 호출되는

아주 유용한 이벤트로 Paint의 이벤트핸들러가 바로 OnPaint()이다.

화면이 다시 그려지는 시점을 프로그래머가 직접 잡아내는 것은 굉장히 까다로운 일이기에

일반적으로 화면에 그려져야하는 내용을 OnPaint()에 담는 것이 보통이다.

또한 OnPaint의 매개변수에는 현 환경의 디스플레이 정보가 담긴 Graphics 객체가 있어

이것을 이용하여 적절하게 화면에 원하는 대로 그릴 수 있게 된다.

 

그런데 어플리케이션 프로그래밍을 한 사람이라면 다 알겠지만

OnPaint()를 호출하여 이미지를 그리면 화면이 깜빡이는 현상이 나타나는데

이는 OnPaint() 자체가 화면을 그릴때 기존의 화면을 지운 후 다시 그리기 때문에 발생하는 문제이다.

 

이를 해결하는 방법이 바로 더블버퍼링(Doublebuffering)!!

하나의 버퍼를 더 마련해두고 그 곳에 그리려하는 이미지를 미리 그려놓은 후,

그 그려진 결과를 화면에 뿌리는 방법이다.

다른 언어에서는 더블버퍼링을 구현하기 위해 여러 작업을 거져야하나

C#에서는 아주 간단하게 구현할 수 있다.

 

아래의 코드를 폼이 로드되는 부분에 집어넣으면 된다.

this.SetStyle(ControlStyles.DoubleBuffer, true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

this.SetStyle(ControlStyles.UserPaint, true);

 

만약에 특정 컨트롤에 더블버퍼링을 적용시키고 싶을때는

해당 컨트롤을 상속받은 클래스를 만들어 위의 코드를 넣어주고 생성시켜주면 된다.

예를 들어 Panel에 더블버퍼링을 적용시키고 싶다면 이와같이 클래스를 선언한후,

 

    public class DoubleBufferPanel : Panel
    {
        public DoubleBufferPanel()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.SetStyle(ControlStyles.UserPaint, true);           

            this.UpdateStyles();

         }
     }

 

Windows Form 디자이너로 가서 컨트롤을 생성시키는 부분을 수정해주면 된다.

이 경우에는 System.Windows.Forms.Panel을 DoubleBufferPanel로 바꿔주면 된다.

 

Tags  Flicker해결, DoubleBuffer, c#
  Relation Articles
[Languages-C#] TextBox(with Border) on C# (2016-04-05 17:00:59)
[Languages-C#] ComboBox(with Border) on C# (2016-04-05 16:54:40)
[Languages-C#] Flicker해결(DoubleBuffer) on C# (2016-01-28 17:24:13)
  Your Opinion
Member ID
150 letters
Copyright (C) SPATIUM. All rights reserved.
[SPATIUM]WebMaster Mail