【问题标题】:OwnerDraw ComboBox with VisualStyles带有 VisualStyles 的 OwnerDraw 组合框
【发布时间】:2009-12-17 06:37:07
【问题描述】:

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

【问题讨论】:

    标签: c# winforms combobox


    【解决方案1】:

    owner-draw 的缺点是当你打开它时,所有者(你)必须绘制所有东西。你几乎完全靠自己。

    如果你想要视觉样式,那么你必须直接调用 VisualStyles API 来做你想做的事。如果你想显示选中、聚焦、启用/禁用状态,那么你必须编写代码来处理它们。

    这不是您的组合框问题的直接答案,但作为如何使用 VisualStyles 的示例,以下是我如何在所有者绘制的 TreeView 中使用 VisualStyles 来绘制加号/减号图标:

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

    【讨论】:

    • 我尝试使用 VisualStyleRenderer 来绘制零件,但我能找到的唯一 VisualStyleElement 是 ComboBox.Button。我将对此进行更多调查,或者干脆放弃并使它们都没有样式。 :)
    猜你喜欢
    • 2014-08-12
    • 2014-10-12
    • 2010-09-27
    • 1970-01-01
    • 2020-01-20
    • 2019-03-02
    • 2013-06-15
    • 2014-12-25
    • 2020-04-01
    相关资源
    最近更新 更多