如何将控件呈现为启用了Visual Styles的ComboBox?

时间:2008-08-05 21:19:57

标签: c# .net winforms

我有一个以 ComboBox 为蓝本的控件。我想渲染控件,使控件 border 看起来像标准的 Windows ComboBox 。具体来说,我已经按照MSDN文档进行控制的所有渲染都是正确的,除非在禁用控件时进行渲染。

为了清楚起见,这适用于启用了视觉样式的系统。此外,除了禁用控件周围的边框外,控件的所有部分都会正确呈现,这与禁用的 ComboBox边框颜色不匹配。

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

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

2 个答案:

答案 0 :(得分:9)

我不是100%确定这是否是您要找的,但您应该查看System.Windows.Forms.VisualStyles-namespace中的 VisualStyleRenderer

  1. VisualStyleRenderer class(MSDN)
  2. How to: Render a Visual Style Element(MSDN)
  3. VisualStyleElement.ComboBox.DropDownButton.Disabled(MSDN)
  4. 如果用户没有启用视觉样式(他/她可能正在运行'经典模式'或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.
    }
    

    希望它有所帮助!

答案 1 :(得分:1)

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