[C#] - MJColorComboBox Part 1: Basic definitions

  • Hi All,

    Sorry I haven't posted in a while, but I've been incredibly busy working on goodies for Bitmight.

    This project started because I wanted a right-click popup menu on the drop-down part of a .NET Combobox control. So I did what I always do:

    I started googling, looking for combobox issues and soon realized that it was all but impossible with the existing combobox control.

    That means a complete rewrite from the ground up. As scary as that sounds, only when the journey is all but complete do you realize how much you've learned.

    I've even gone so far as defining my own color type to simplify conversions and to add a certain amount of convenience.

    So let's talk about colors. There are literally thousands of them, with many of them having descriptive names. (e.g: AliceBlue, PeachPuff, etc.)

    Each color has an associated 32 bit value, based on the Windows COLORREF. The upper 8 bits is called the "Alpha channel". This is followed by the

    Red, Green and Blue bytes respectively (AARRGGBB)

    In a COLORREF, if the upper 8 bits are zero, then the color is fully opaque. A .NET color is a little different: If the Alpha byte is 255, the color is opaque.

    This can be used to our advantage by allowing us to define an 'Empty' or 'Null' Color. This value ends up being 0xFF000000.

    All we have to do is subtract our alpha from 255 to make it compatible with .NET colors. By doing this, our 'Empty' color ends up becoming Color.FromArgb(0,0,0,0), which is an invisible color.

    Let's talk about Windows System colors. To get a System Color requires a level of indirection; the actual values are stored inside the currently installed Windows theme.

    To get the color, we call an exported function called GetSysColor(). This is in User32.dll, so you need to declare it.

    You call it with an index which specifies the system color you want.

    Take a look here:


    Here are the values for the color indices.



    When you call GetSysColor(), you may notice that the colors appear incorrect. As a matter of fact, they are.

    The Red and Blue bytes are swapped. You will have to swap them manually.

    If you're using Win32.cs, this is done for you by calling Win32.CorrectedGetSysColor().

    If not, here's the code.

    I call GetSysColor() every time the user needs a system color. This eliminates the problem of stale colors caused by theme changes.

    More to follow...