2012-07-31 70 views
1

我想在C#中有一個定製的複選框,它有一個漸變背景。我overrided的OnPaint(PaintEventArgs的E)如下:C中的自定義複選框#

Graphics g = e.Graphics; 

     base.OnPaint(e); 
     //// Fill the background 
     //SetControlSizes(); 

     // Paint the outer rounded rectangle 
     g.SmoothingMode = SmoothingMode.AntiAlias; 
     using (GraphicsPath outerPath = GeneralUtilities.RoundedRectangle(mLabelRect, 1, 0)) 
     { 
      using (LinearGradientBrush outerBrush = new LinearGradientBrush(mLabelRect, 
        mGradientTop, mGradientBottom, LinearGradientMode.Vertical)) 
      { 
       g.FillPath(outerBrush, outerPath); 
      } 
      using (Pen outlinePen = new Pen(mGradientTop, mRectOutlineWidth)) 
      { 
       outlinePen.Alignment = PenAlignment.Inset; 
       g.DrawPath(outlinePen, outerPath); 
      } 
     } 

     //// Paint the gel highlight 
     using (GraphicsPath innerPath = GeneralUtilities.RoundedRectangle(mHighlightRect, mRectCornerRadius, mHighlightRectOffset)) 
     { 
      using (LinearGradientBrush innerBrush = new LinearGradientBrush(mHighlightRect, 
        Color.FromArgb(mHighlightAlphaTop, Color.White), 
        Color.FromArgb(mHighlightAlphaBottom, Color.White), LinearGradientMode.Vertical)) 
      { 
       g.FillPath(innerBrush, innerPath); 
      } 
     } 
     // Paint the text 
     TextRenderer.DrawText(g, Text, Font, mLabelRect, Color.White, Color.Transparent, 
     TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis); 

它的工作原理,並作出gradiant背景,但gradiant下複選框消失,也無法訪問。現在,我該怎麼辦?請儘快幫我儘可能

回答

1

編輯:

OK,我知道什麼是錯的。該複選框會自動繪製一個覆蓋以前繪製的任何東西的底層背景。

在這種情況下,您必須自己繪製複選框的外觀(即選中狀態等)。


應覆蓋OnPaintBackground功能繪製的,而不是OnPaint背景。

另一種選擇是撥打base.OnPaint(e)你已經繪製了背景。

該複選框不會在漸變下「消失」,您仍然可以訪問它。您剛剛繪製的「背景」高於的「前景」。

基本控件繪製base.OnPaint(e)函數中複選框的外觀。如果您在調用它之後繪製任何東西,那麼這些東西將被繪製爲繪製複選框前面的「覆蓋圖」,這就是爲什麼您看不到複選框的外觀。


如果您要自己繪製文本,您也不希望顯示內部繪製的複選框文本。在這種情況下,您還需要自己繪製複選框的外觀。

正如我已經提到的,如果您只打算繪製自定義背景,請改爲使用OnPaintBackground

+0

謝謝艾爾文,你的解決方案沒有來得方便 – Mohsen 2012-07-31 09:45:48

+0

我叫'base.OnPaint(e);'在我的方法的底部,但它不工作 – Mohsen 2012-07-31 10:01:18

+0

我使用OnPaintBackground而不是OnPaint,但它does not無效 – Mohsen 2012-07-31 10:08:20