带有VisualStyles的OwnerDraw ComboBox

时间:2009-12-17 06:37:08

标签: c# winforms combobox

我有一个ComboBox,我设置了 DrawMode = DrawMode.OwnerDrawFixed 。然后我处理OnDrawItem事件,一切都很完美。但是,它看起来与标准的ComboBox非常不同,因为我似乎没有使用VisualStyles渲染。我是否需要做一些事情来专门为我的所有者绘制控件启用VisualStyle渲染?我在我的控制下尝试过SetWindowTheme,但我不确定要发送什么样的主题类。任何帮助将非常感激。谢谢!

1 个答案:

答案 0 :(得分:6)

所有者抽奖的缺点是当你打开它时,所有者(你)必须画出一切。你几乎完全靠自己。

如果您需要视觉样式,则必须直接调用VisualStyles API来执行您想要的操作。如果要显示选定,聚焦,启用/禁用状态,则必须编写代码来处理所有状态。

这不是您的组合框问题的直接答案,但作为如何使用VisualStyles的示例,以下是我在所有者绘制的TreeView中使用VisualStyles绘制Plus / Minus图标的方法:< / p>

// 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);
}