Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OwnerDraw ComboBox with VisualStyles

I have a ComboBox that I have set DrawMode = DrawMode.OwnerDrawFixed. Then I handle the OnDrawItem event and everything works perfectly. However, it looks very different from a standard ComboBox because mine doesn't seem to be rendered using VisualStyles. Do I need to do something to specifically enable VisualStyle rendering for my owner drawn control? I have tried SetWindowTheme on my control, but I'm not sure what theme class to send. Any help would be much appreciated. Thanks!

like image 788
Jon Tackabury Avatar asked Dec 17 '09 06:12

Jon Tackabury


1 Answers

The down side of owner-draw is that when you turn it on, the owner (you) has to draw everything. You are almost completely on your own.

If you want visual styles, then you have to call the VisualStyles APIs directly to do what you want. If you want to show selected, focussed, enabled/disabled states, then you have to write code to deal with them all.

This isn't a direct answer for your combo-box issues, but as an example of how to use VisualStyles, here is how I've used VisualStyles in an owner-drawn TreeView to draw the Plus/Minus icon:

// Draw Expand (plus/minus) icon if required
if (ShowPlusMinus && e.Node.Nodes.Count > 0)
{
    // Use the VisualStyles renderer to use the proper OS-defined glyphs
    Rectangle expandRect = new Rectangle(iconLeft-1, midY - 7, 16, 16);

    VisualStyleElement element = (e.Node.IsExpanded) ? VisualStyleElement.TreeView.Glyph.Opened
                                                     : VisualStyleElement.TreeView.Glyph.Closed;

    VisualStyleRenderer renderer = new VisualStyleRenderer(element);
            renderer.DrawBackground(e.Graphics, expandRect);
}
like image 157
Jason Williams Avatar answered Sep 30 '22 21:09

Jason Williams