2011-05-02 57 views
1

我有一個窗體,它有其他控件(按鈕,自定義控件,標籤,面板,gridview)的音調。你可以猜到我有閃爍的問題。我嘗試了雙緩衝,但無法解決。最後我試過這一個:DataGridView繪製錯誤

protected override CreateParams CreateParams 
{ 
    get 
    { 
     // Activate double buffering at the form level. All child controls will be double buffered as well. 
     CreateParams cp = base.CreateParams; 
     cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED 
     return cp; 
    } 
} 

閃爍不見了,但我的datagridview繪製錯誤。它顯示CellBorders,BorderColors錯誤。其實這段代碼在背景圖片,線條和其他東西上存在一些問題。爲什麼是這樣以及如何修復?

回答

3

我知道這個問題是有點老了,但遲到總比不到好...

這裏有一個變通停止當用戶改變形式閃爍,但沒有搞亂的控制繪圖如DataGridView的。前提是你的窗體名稱是「Form1的」:

int intOriginalExStyle = -1; 
bool bEnableAntiFlicker = true; 

public Form1() 
{ 
    ToggleAntiFlicker(false); 
    InitializeComponent(); 
    this.ResizeBegin += new EventHandler(Form1_ResizeBegin); 
    this.ResizeEnd += new EventHandler(Form1_ResizeEnd); 
} 

protected override CreateParams CreateParams 
{ 
    get 
    { 
     if (intOriginalExStyle == -1) 
     { 
      intOriginalExStyle = base.CreateParams.ExStyle; 
     } 
     CreateParams cp = base.CreateParams; 

     if (bEnableAntiFlicker) 
     { 
      cp.ExStyle |= 0x02000000; //WS_EX_COMPOSITED 
     } 
     else 
     { 
      cp.ExStyle = intOriginalExStyle; 
     } 

     return cp; 
    } 
} 

private void Form1_ResizeBegin(object sender, EventArgs e) 
{ 
    ToggleAntiFlicker(true); 
} 

private void Form1_ResizeEnd(object sender, EventArgs e) 
{ 
    ToggleAntiFlicker(false); 
} 

private void ToggleAntiFlicker(bool Enable) 
{ 
    bEnableAntiFlicker = Enable; 
    //hacky, but works 
    this.MaximizeBox = true; 
} 
1

我找到了竅門有很好的平滑調整大小和顯示我的網線,是爲了增加一個額外的標誌,如果我的應用程序在Windows XP或Windows Server 2003上運行:

protected override CreateParams CreateParams 
{ 
    get 
    { 
     CreateParams cp = base.CreateParams; 

     cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED 

     if (this.IsXpOr2003 == true) 
      cp.ExStyle |= 0x00080000; // Turn on WS_EX_LAYERED 

     return cp; 
    } 
} 

private Boolean IsXpOr2003 
{ 
    get 
    { 
     OperatingSystem os = Environment.OSVersion; 
     Version vs = os.Version; 

     if (os.Platform == PlatformID.Win32NT) 
      if ((vs.Major == 5) && (vs.Minor != 0)) 
       return true; 
      else 
       return false; 
     else 
      return false; 
    } 
}