2012-11-12 44 views
0

我想知道是否可以繪製一個checkBox自定義顏色的文字和刻度?我聽說有可能通過重寫WndProc並使用WM_PAINT,但我沒有這樣做的經驗。重繪複選框

有人可以請指出我在正確的方向嗎?

+0

好的,那麼爲什麼不使用BackColor和ForeColor屬性? –

+0

我想要藍色滴答的顏色和文本的顏色黑色..我似乎無法實現這與backColor/foreColor。 – p0enkie

+0

這是正確的方向:http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/27932a94-b63b-4293-ae82-f10171691888/ – Blachshma

回答

2

這裏是你如何重繪複選框一個非常簡單的例子:

public class CustomCheckBox : CheckBox 
    { 
     public CustomCheckBox() 
     { 
      this.SetStyle(ControlStyles.UserPaint, true); 
      this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     } 

     protected override void OnPaint(PaintEventArgs pevent) 
     { 
      base.OnPaint(pevent); 
      if (this.Checked) 
      { 
       pevent.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(0, 0, 16, 16)); 
      } 
      else 
      { 
       pevent.Graphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, 16, 16)); 
      } 
     } 
    } 

它的工作原理,但它的四周邊緣非常粗糙!然而,它確實說明了你如何繪製自定義控件。