2012-03-28 161 views
2

我正在創建一個自定義DataGridView,其中CheckBox在引發MouseHover時顯示邊框。在C#winform中繪圖很慢

這是我到目前爲止所做的。

void checkBox_MouseLeave(object sender, EventArgs e) 
    { 
     //showBorder defines whether the border is drawn. 
     this.showBorder = false; 
     this.DataGridView.InvalidateCell(this); 
    } 

    void CheckBoxMouseHover(object sender, EventArgs e) 
    { 
     this.showBorder = true; 
     this.CheckBox.BringToFront(); 
     this.DataGridView.InvalidateCell(this); 
    } 

    protected override void Paint(...........) 
    { 
     .......... 
     if (showBorder) 
     { 
      GraphicsPath border=new GraphicsPath(); 
      border.AddRectangle(new Rectangle(checkBoxPosition.X-1,checkBoxPosition.Y-1,checkBoxSize.Width+1,checkBoxSize.Height+1)); 
      graphics.DrawPath(new Pen(borderColor,1),border); 
     } 
    } 

但是速度太慢,我不得不等待大約半秒鐘才能看到邊框顯示。 無論如何,MouseLeave工作正常。 那麼我該如何改善這裏的表現呢?

另外,如何定製複選框?例如背景顏色等。

+1

你是否熟悉雙緩衝? http://www.bobpowell.net/doublebuffer.htm – 2012-03-28 01:37:47

+0

我試過自動雙緩衝,並沒有區別。 – user840866 2012-03-28 01:59:38

回答

4

您正在使用MouseHover事件來控制鼠標。嘗試使用MouseEnter。在鼠標停留在控件上一段時間後,鼠標懸停會被觸發。 MouseEnter是即時

+0

謝謝。它運行良好。 – user840866 2012-03-28 02:12:54