본문 바로가기
프로그래밍

[C#] 더블버퍼링으로 화면 깜빡임 해결하기

by minimax95 2020. 11. 7.

오늘은 윈폼에서 화면 전환 시 깜빡임 현상을 해결하는 방법에 대해서 정리해 보겠습니다.

 

MFC에서는 메모리 DC를 생성해서 Bitmap을 먼저 올리거나 그리고 DC에 그려서 깜빡임 현상을 해결하지만 C#에서는 간단하게 Form 속성에서 이 문제를 간단하게 해결할 수 있습니다.

 

Form의 속성을 살펴보면 DoubleBuffered 속성이 아래 그림과 같이 있으며 속성 값을 True로 해주면 일단 Form의 깜빡임 현상을 해결할 수 있습니다. 그러나 Form 위에 Panel을 올려서 레이아웃을 구성하였을 경우 Panel의 깜빡임 현상은 별도로 Panel 클래스를 재정의 해주어야 깜빡임 현상을 해결할 수 있습니다.

 

☞ Form 속성에서 DoubleBufferd 속성 변경하기

 

☞ Panel 클래스 재정의하기

먼저 솔루션 탐색기에서 새 항목 추가하기로 클래스를 선택하고 이름은 "DoubleBufferedPanel"로 선언해 줍니다.

DoubleBufferedPanel 클래스에서는 아래와 같이 Panel 클래스를 상속하고 생성자에서 SetStyle 속성을 변경해 줍니다.

public class DoubleBufferPanel : Panel

{

     public DoubleBufferPanel( )

    {

        this.SetStyle(ControlStyles.OptimizedDoubleBuffer 

                 | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

        this.UpdateStyles( );

    }

}

 

이제 실제로 사용할 Panel을 우리가 재정의한 DoubleBufferedPanel 클래스로 바꾸어 줍니다.

사용하는 Form의 Designer.cs를 열고 소스코드 상에서 Panel 클래스를 주석 처리하고 우리가 재정의한 DoubleBufferedPanel 클래스로 다시 선언하고 생성해 주면 Panel의 깜빡임 문제까지 깔끔하게 해결됩니다.

//this.pn_menu = new System.Windows.Forms.Panel( );

//this.pnbtn_config_reset = new System.Windows.Forms.Panel( );

//this.pnbtn_config_cbank = new System.Windows.Forms.Panel( );

//this.pnbtn_config_system = new System.Windows.Forms.Panel( );

//this.pnbtn_config_hmi = new System.Windows.Forms.Panel( );

this.pn_menu = new DoubleBufferPanel();

this.ppnbtn_config_reset = new DoubleBufferPanel();

this.pnbtn_config_cbank = new DoubleBufferPanel();

this.pnbtn_config_system = new DoubleBufferPanel();

this.ppnbtn_config_hmi = new DoubleBufferPanel();

 

Panel에 투명 바탕의 이미지를 올릴 경우 특히 깜빡임 현상을 확인할 수 있는데 우리가 만든 DoubleBufferedPanel 클래스를 재사용한다면 매우 유용한 클래스가 될 것입니다.

아래의 이미지는 KeyPanel 사용자 정의 컨트롤을 만들고 여기서 Panel을 버튼으로 처리하기 위해 투명 바탕의 이미지를 올려 보았습니다. 실제 DoubleBufferPanel 패널로 교체하면 깜빡임이 완벽하게 해결 된 것을 확인할 수 있었습니다.

 

이상 포스팅을 마치겠습니다.

감사합니다.

 

댓글