C# Windows Forms에서 TableLayoutPanel이 깜박거리는 문제는 주로 Double Buffering이 비활성화되어 발생합니다. 이를 해결하기 위해 Double Buffering을 활성화하는 방법을 사용하면 깜박임을 줄일 수 있습니다.
해결 방법 1: Double Buffering 활성화
- 폼의 생성자에서 Double Buffering을 강제로 활성화:
public Form1()
{
InitializeComponent();
EnableDoubleBuffering(tableLayoutPanel1);
EnableDoubleBuffering(tableLayoutPanel2);
}
private void EnableDoubleBuffering(Control control)
{
typeof(Control).InvokeMember("DoubleBuffered",
System.Reflection.BindingFlags.SetProperty |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic,
null, control, new object[] { true });
}
깜빡거리는 모든 TableLayoutPanel에 대해 이 메서드를 호출하십시오.
// 깜빡임 방지 사용 예제 :
private void InitializeCustomUI()
{
// 메인 레이아웃 (TableLayoutPanel)
mainLayout = new TableLayoutPanel();
mainLayout.BackColor = Color.WhiteSmoke;
this.Controls.Add(mainLayout); // 폼에 추가
EnableDoubleBuffering(mainLayout); <<<
uiPanel = new TableLayoutPanel();
uiPanel!.BackColor = Color.FromArgb(130, Color.BurlyWood);
mainLayout.Controls.Add(uiPanel, 1, 0);
EnableDoubleBuffering(uiPanel); <<<
}
해결 방법 2: SuspendLayout/ResumeLayout 사용
- SuspendLayout과 ResumeLayout으로 깜박임을 줄일 수 있습니다:
tableLayoutPanel1.SuspendLayout();
// 테이블 레이아웃 패널의 변경 작업
tableLayoutPanel1.ResumeLayout();
tableLayoutPanel1.PerformLayout();
해결 방법 3: ControlStyles OptimizedDoubleBuffer 설정
- 사용자 정의 TableLayoutPanel을 생성하고 Double Buffering을 강제 활성화:
- public class BufferedTableLayoutPanel : TableLayoutPanel
{
public BufferedTableLayoutPanel()
{
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.UpdateStyles();
}
}
- 이후 TableLayoutPanel 대신 BufferedTableLayoutPanel을 사용하면 깜박임이 줄어듭니다.
해결 방법 4: protected override CreateParams CreateParams 삽입
클래스 시작 부분에 아래 깜빡임 방지 코드 삽입
namespace BadukGame
{
public partial class BadukGameMain : Form
{
//화면 깜빡임 방지
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
또는