C# 如何将控件呈现为启用了视觉样式的 ComboBox?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render a control to look like ComboBox with Visual Styles enabled?
提问by Peter Hession
I have a control that is modelled on a ComboBox. I want to render the control so that the control borderlooks like that of a standard Windows ComboBox. Specifically, I have followed the MSDN documentation and all the rendering of the control is correct except for rendering when the control is disabled.
我有一个以ComboBox为模型的控件。我想呈现控件,以便控件边框看起来像标准Windows ComboBox 的边框。具体来说,我遵循了 MSDN 文档,除了禁用控件时的渲染外,控件的所有渲染都是正确的。
Just to be clear, this is for a system with Visual Stylesenabled. Also, all parts of the control render properly except the border around a disabled control, which does not match the disabled ComboBox bordercolour.
需要明确的是,这是针对启用了视觉样式的系统。此外,除了禁用控件周围的边框外,控件的所有部分都正确呈现,该边框与禁用的ComboBox 边框颜色不匹配。
I am using the VisualStyleRendererclass. MSDN suggests using the VisualStyleElement.TextBox
element for the TextBoxpart of the ComboBoxcontrol but a standard disabled TextBoxand a standard disabled ComboBoxdraw slightly differently (one has a light grey border, the other a light blue border).
我正在使用VisualStyleRenderer类。MSDN 建议将该VisualStyleElement.TextBox
元素用于ComboBox控件的TextBox部分,但标准禁用TextBox和标准禁用ComboBox 的绘制略有不同(一个具有浅灰色边框,另一个具有浅蓝色边框)。
How can I get correct rendering of the control in a disabled state?
如何在禁用状态下正确呈现控件?
回答by Nick
Are any of the ControlPaintmethods useful for this? That's what I usually use for custom-rendered controls.
任何ControlPaint方法对此有用吗?这就是我通常用于自定义呈现控件的内容。
回答by Patrik Svensson
I'm not 100% sure if this is what you are looking for but you should check out the VisualStyleRendererin the System.Windows.Forms.VisualStyles-namespace.
我不能 100% 确定这是否是您要查找的内容,但您应该查看System.Windows.Forms.VisualStyles 命名空间中的VisualStyleRenderer。
- VisualStyleRenderer class(MSDN)
- How to: Render a Visual Style Element(MSDN)
- VisualStyleElement.ComboBox.DropDownButton.Disabled(MSDN)
- VisualStyleRenderer 类(MSDN)
- 如何:呈现视觉样式元素(MSDN)
- VisualStyleElement.ComboBox.DropDownButton.Disabled(MSDN)
Since VisualStyleRenderer won't work if the user don't have visual styles enabled (he/she might be running 'classic mode' or an operative system prior to Windows XP) you should always have a fallback to the ControlPaint class.
由于如果用户没有启用视觉样式(他/她可能正在运行“经典模式”或 Windows XP 之前的操作系统),VisualStyleRenderer 将无法工作,因此您应该始终回退到 ControlPaint 类。
// Create the renderer.
if (VisualStyleInformation.IsSupportedByOS
&& VisualStyleInformation.IsEnabledByUser)
{
renderer = new VisualStyleRenderer(
VisualStyleElement.ComboBox.DropDownButton.Disabled);
}
and then do like this when drawing:
然后在绘图时这样做:
if(renderer != null)
{
// Use visual style renderer.
}
else
{
// Use ControlPaint renderer.
}
Hope it helps!
希望能帮助到你!