【问题标题】:VisualStyles color inconsistancyVisualStyles 颜色不一致
【发布时间】:2018-10-16 18:30:54
【问题描述】:

我只是在搞乱 .NET 使用 Winforms 提供的视觉样式,我遇到了问题。我只是执行这段代码,从自定义面板的 OnPaint 方法调用,没什么特别的。

private void DrawBox(PaintEventArgs e)
{
        ComboBoxRenderer.DrawDropDownButton(e.Graphics, e.ClipRectangle, ComboBoxState.Normal);}

我的问题是按钮的背景颜色是灰色,而真正的 ComboBox 的背景颜色是白色(无论如何在我的计算机上)。

这里有两个问题,为什么颜色会改变,以及如何使用与真正的 ComboBox 相同的背景颜色来渲染按钮?

谢谢 丹尼

【问题讨论】:

  • 您是否还在搞乱启用/禁用VisualStyles 设置和SetWindowTheme() 和Co.?
  • 组合框样式受 DropdownStyle 属性的影响很大,比较 DropDown 和 DropDownList。考虑到 DropDownList 是“真正的”样式,您不喜欢 ComboBoxRender。 Lars 解决了您的问题,请关闭您的问题。

标签: c# .net winforms


【解决方案1】:

ClipRectangle 不是你想要的。设置您要绘制的实际矩形。话虽如此,是的,使用 VisualStyles 会让你发疯。这会让你接近:

VisualStyleRenderer vsr = new VisualStyleRenderer("EDIT", 1, 1);
vsr.DrawBackground(e.Graphics, controlRectangle);
vsr.SetParameters("COMBOBOX", 7, 1);
vsr.DrawBackground(e.Graphics, arrowRectangle);

【讨论】:

  • 可能更好地使用参数的内部定义:VisualStyleElement ButtonElm = VisualStyleElement.ComboBox.DropDownButton.Normal;VisualStyleElement TextElm = VisualStyleElement.TextBox.TextEdit.Normal;([Element].Part 和 [Element].State)。 `ComboBoxRenderer Class 用于 ComboBox 部件区域的(基本 - 不考虑 DPI 事物)定义。
  • 极度技术性,7 是左侧按钮的部件号,6 是右侧按钮的部件号。您必须注意表单的 RightToLeft 属性。很难想象它实际上很重要:)
  • @LarsTech,我想要的是搞乱视觉样式,而不是真正关心实际的矩形,所以我的例子确实是我想要的,因为我不关心控件的大小。也就是说,除了第 2 行中的错字之外,您的代码运行良好,有一对多的大括号。谢谢
  • @Jimi, VisualStyleElement.ComboBox,DropDown.Normal 用灰色背景绘制按钮,这首先激发了这些问题。
  • @dannyhut 你错过了那条评论的重点。可能是因为您没有打开提供的链接。您正在绘制一个 Panel 并使用它的 e.Graphics 对象来绘制 Combo(部分)部分。这只是一幅画,不是对照。它没有句柄,可以将其传递给SetWindowTheme() 以应用当前主题。 (...)。
猜你喜欢
  • 2011-10-31
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
相关资源
最近更新 更多