Click here to Skip to main content
13,087,235 members (60,129 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 22 Dec 2004

Correct theme support for the .NET CheckBox control

, 22 Dec 2004
Rate this:
Please Sign up or sign in to vote.
An article on adding correct theme support to the .NET CheckBox control without taking over the drawing of the control, in C#.


This article addresses the issue and adds transparency support to the CheckBox control. It makes the control draw in XP Style, when the FlatStyle is set to FlatStyle.System in correct manner.

There are some articles on the same topic, for e.g., True Transparency support for the .NET CheckBox control. But these articles advice us take over the drawing of the control. I found out a way to avoid this and use only standard Windows procedure for ComboBox.

The Solution

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CustomControls 
  public class ThemedCheckBox : CheckBox 
    Bitmap backBufferImage = null;
    protected override void OnLayout(LayoutEventArgs levent)

      if (levent.AffectedProperty == "Bounds") 
        backBufferImage = new Bitmap(Width, Height);
    protected override void WndProc(ref Message m)
      if (FlatStyle != FlatStyle.System)
         base.WndProc(ref m);
        case 0xF:
          if (backBufferImage == null)
            backBufferImage = new Bitmap(Width, Height);
          Graphics bg = Graphics.FromImage(backBufferImage); 
          IntPtr hdc = bg.GetHdc();
          Message bm = Message.Create(Handle, 0xF, hdc, IntPtr.Zero);
          base.WndProc(ref bm);
          PAINTSTRUCT ps = new PAINTSTRUCT();
          hdc = m.WParam == IntPtr.Zero ? BeginPaint(Handle, ref ps) : m.WParam;
          Graphics fg = Graphics.FromHdc(hdc);
          fg.DrawImage(backBufferImage, 0, 0);
          if (m.WParam != IntPtr.Zero)
            EndPaint(Handle, ref ps);
        case 0x14:
          base.WndProc(ref m); break;

    #region API Declares 
    [StructLayout(LayoutKind.Sequential, Pack=4)]
    public struct PAINTSTRUCT 
      public IntPtr hdc;
      public bool fErase;
      public Rectangle rcPaint;
      public bool fRestore;
      public bool fIncUpdate;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] 
      public byte[] rgbReserved;
    private static extern IntPtr BeginPaint(IntPtr hwnd, ref PAINTSTRUCT lpPaint);

    private static extern bool EndPaint(IntPtr hwnd, ref PAINTSTRUCT lpPaint);

    #endregion API Declares


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Boris Byk
Web Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 22 Dec 2004
Article Copyright 2004 by Boris Byk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid