2013-12-18 224 views
0

我嘗試防止TextBox閃爍,迄今爲止沒有成功。
TextBox是多行只讀的。如何防止TextBox閃爍?

此代碼每秒運行幾次。該文本有大約10k個字符。

int ss = txt.SelectionStart; 
int sl = txt.SelectionLength; 
txt.Text = s; 
txt.SelectionStart = ss; 
txt.SelectionLength = sl; 

Resarching問題給了我以下可能的解決方案
- 但他們沒有工作。

1)LockWindowUpdate

[System.Runtime.InteropServices.DllImport("user32.dll")] 
private static extern bool LockWindowUpdate(IntPtr hWndLock); 

//... 

LockWindowUpdate(txt.Handle); 
txt.Text = someText; 
LockWindowUpdate(IntPtr.Zero); 

2)的SetStyle

this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); 

3)SuspendLayout/ResumeLayout(猜它無關油漆 - 但只是一種嘗試)

txt.SuspendLayout(); 
txt.Text = s; 
txt.ResumeLayout(); 
+0

爲什麼你首先閃爍?設置Text屬性不應該導致閃爍。你在做什麼,有什麼問題? –

+0

我看到閃爍的唯一時間是當你正在對文本框做幾個小的更新時。如果這就是你正在做的事情,也許你應該使用StringBuilder對它們進行批處理? –

回答

0

事實證明,父窗體的CreateParams必須使用WS_EX_COMPOSITED標誌:

protected override CreateParams CreateParams { 
     get { 
      new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand(); 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED 
      return cp; 
     } 
    }