【问题标题】:How to render a control to look like ComboBox with Visual Styles enabled?如何使控件看起来像启用了 Visual Styles 的 ComboBox?
【发布时间】:2010-09-05 09:31:05
【问题描述】:

我有一个以 ComboBox 为模型的控件。我想渲染控件,使控件border看起来像标准的Windows ComboBox。具体来说,我遵循了 MSDN 文档,除了禁用控件时的渲染外,所有控件的呈现都是正确的。

明确一点,这是针对启用了视觉样式的系统。此外,除了禁用控件周围的边框与禁用的 ComboBox 边框颜色不匹配之外,控件的所有部分都正确呈现。

我正在使用 VisualStyleRenderer 类。 MSDN 建议对 ComboBox 控件的 TextBox 部分使用 VisualStyleElement.TextBox 元素,但标准禁用 TextBox 和标准禁用 ComboBox 绘制略有不同(一个有浅灰色边框,另一个有浅蓝色边框)。

如何在禁用状态下正确呈现控件?

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    我不能 100% 确定这是否是您要查找的内容,但您应该查看 System.Windows.Forms.VisualStyles 命名空间中的 VisualStyleRenderer

    1. VisualStyleRenderer class (MSDN)
    2. How to: Render a Visual Style Element (MSDN)
    3. VisualStyleElement.ComboBox.DropDownButton.Disabled (MSDN)

    由于如果用户没有启用视觉样式(他/她可能正在运行“经典模式”或 Windows XP 之前的操作系统),VisualStyleRenderer 将无法工作,因此您应该始终回退到 ControlPaint 类。

    // Create the renderer.
    if (VisualStyleInformation.IsSupportedByOS 
        && VisualStyleInformation.IsEnabledByUser) 
    {
        renderer = new VisualStyleRenderer(
            VisualStyleElement.ComboBox.DropDownButton.Disabled);
    }
    

    然后在画图的时候这样做:

    if(renderer != null)
    {
        // Use visual style renderer.
    }
    else
    {
        // Use ControlPaint renderer.
    }
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      任何ControlPaint 方法对此有用吗?这就是我通常用于自定义呈现控件的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-22
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        相关资源
        最近更新 更多