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

Pet*_*ion 32 .net c# winforms

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

需要明确的是,这适用于启用了视觉样式的系统.此外,除了禁用的控件周围的边框外,控件的所有部分都会正确呈现,这与禁用的ComboBox边框颜色不匹配.

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

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

Pat*_*son 11

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

  1. VisualStyleRenderer类(MSDN)
  2. 如何:呈现视觉样式元素(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);
}
Run Code Online (Sandbox Code Playgroud)

然后在绘图时这样做:

if(renderer != null)
{
    // Use visual style renderer.
}
else
{
    // Use ControlPaint renderer.
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


Nic*_*ick 1

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