[C#] - MJColorComboBox Part 2: Basic Control Design.

  • Hi everyone.


    Lets talk about the basic structure of a .NET control.


    There are many other things done in the control constructor. If you want to see the exact code, please download the source.

    I'll post a link when I'm done blathering on about the design.


    Let's talk about the control styles used.


    AllPaintingInWmPaint, OptimizedDoubleBuffer, UserPaint, Opaque, ResizeRedraw


    These five control styles allow us to completely take over all of the drawing in the control. "OptimizedDoubleBuffer" enables double buffering, which reduces flicker when the control is redrawn.


    For this to work, you must also add "AllPaintingInWmPaint","UserPaint" and "Opaque". These flags force all drawing to be performed in the control OnPaint handler, and eliminate background painting.

    For veteran Windows programmers, basically this suppresses WM_ERASEBKGND messages.


    "ResizeRedraw" basically tells the OS to send a WM_PAINT message when the control is resized.

    Notice that "DoubleBuffered" is also set to true. Think of it as insurance against flicker. :)


    If you place this control skeleton into a C# project, it will produce a black square when you put it onto a form. There is no paint handler in place, so that's to be expected.

    Also, we haven't set the dimensions of the control. In my implementation, I have a function which accurately measures the size of a string. I use this to size the control.


    Here it is.



    At some point, I will be adding a TextBox for autocomplete and editing. If you don't add the TextBox to the controls' list of children, the TextBox will never be shown.

    To do that, add this line after you've created the control.


    Csharp
    1. Controls.Add(InplaceEditor);



    Let's talk a little bit about Dispose() and what gets handled there. Anything that gets added to the control child list is automatically disposed of by the control itself.

    But things like timers need to be disposed of by us. There are four timers used in my design. One handles mouse repeat. Another handles tool tips.

    The other two handle cleanup after one of the respective context menus are invoked.


    These timers must be disposed by us. Here's how it's done.



    In the next post, I'll talk about event handlers, and overriding the ones that are important to our control.


    See you then...