我正在使用ButtonRenderer
在自定义单元格中绘制按钮。我想按钮有一个非标准的BackColor。这由普通按钮支持,但按钮单元格或ButtonRenderer
中没有任何内容支持它。如何绘制带有非标准BackColor的按钮?该方法必须考虑用户的主题 - 我不能只绘制我自己的按钮。如何绘制带有非标准BackColor的按钮?
0
A
回答
2
ButtonRenderer使用VisualStyleRenderer.DrawBackground()绘制按钮背景。该方法非常了解用户选择的主题,按钮的背景将使用主题指定的颜色。使用非标准的BackColor会违反用户选择的主题。你不能两面都有。
Button类实际上并不使用ButtonRenderer,它使用从System.Windows.Forms.ButtonInternal命名空间的内部ButtonBaseAdapter类派生的三个渲染器之一。这些渲染器是内部的,你不能在你自己的代码中使用它们。用Reflector或Reference Source看看它们的含义。专注于PaintButtonBackground方法。
-1
使用提供的ControlPaint和TextRenderer类绘制自己的按钮。这相当简单。我把这些代码快速地放在一起给你看。您可以通过设置边框样式等
private ButtonState state = ButtonState.Normal;
public ButtonCell(): base()
{
this.Size = new Size(100, 40);
this.Location = new Point(50, 50);
this.Font = SystemFonts.IconTitleFont;
this.Text = "Click here";
}
private void DrawFocus()
{
Graphics g = Graphics.FromHwnd(this.Handle);
Rectangle r = Rectangle.Inflate(this.ClientRectangle, -4, -4);
ControlPaint.DrawFocusRectangle(g, r);
g.Dispose();
}
private void DrawFocus(Graphics g)
{
Rectangle r = Rectangle.Inflate(this.ClientRectangle, -4, -4);
ControlPaint.DrawFocusRectangle(g, r);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (state == ButtonState.Pushed)
ControlPaint.DrawBorder3D(e.Graphics, e.ClipRectangle, Border3DStyle.Sunken);
else
ControlPaint.DrawBorder3D(e.Graphics, e.ClipRectangle, Border3DStyle.Raised);
TextRenderer.DrawText(e.Graphics, Text, this.Font, e.ClipRectangle, this.ForeColor,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
protected override void OnGotFocus(EventArgs e)
{
DrawFocus();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
Invalidate();
base.OnLostFocus(e);
}
protected override void OnMouseEnter(EventArgs e)
{
DrawFocus();
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
Invalidate();
base.OnMouseLeave(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
state = ButtonState.Pushed;
Invalidate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
state = ButtonState.Normal;
Invalidate();
base.OnMouseUp(e);
}
相关问题
- 1. 非标准后退按钮
- 2. 如何复制标准的Salesforce按钮?
- 3. 简化非标准按钮的绑定
- 4. 绘制矩阵的标准错误(带有复制数据)
- 5. 用ggplot2绘制非标准年(水年)
- 6. 如何用ggplot2绘制非标准化的数据?
- 7. 带有非标准字符的SHA1
- 8. 带有JNA的非标准类型
- 9. 如何重置为默认按钮BackColor?
- 10. Qt:如何实现QDialogButtonBox与QSignalMapper的非标准按钮?
- 11. 如何在带按钮的画布上绘制正方形
- 12. 按钮BackColor不会改变
- 13. 更改按钮BackColor属性
- 14. 准备一个非标准内部发光的Android按钮?
- 15. 自定义按钮或链接到带有标准控制器的Visualforce页面
- 16. 按住鼠标按钮时如何绘制?
- 17. 如何缩放按钮内部的可绘制按钮,使按钮大小不受可绘制的控制?
- 18. 如何绘制上的启动按钮
- 19. 如何垂直对齐带有背景可绘制按钮的文本?
- 20. 如何制作带有非圆角的小本机浏览器按钮?
- 21. 是否有标准的按钮尺寸?
- 22. Winform上的Datagridview不会绘制backcolor
- 23. 如何隐藏Acumatica标准按钮
- 24. ChartJS - 绘制带有月份标签的图表,按天数据
- 25. 如何为anko中的按钮设置图标(可绘制)?
- 26. 如何在windows-CE中绘制按钮?
- 27. 如何绘制按钮故事板?
- 28. 熊猫:如何绘制带有标签的数据框的barchar?
- 29. 带按钮的控制单选按钮
- 30. 更改中继器按钮onClick的backColor
这是ButtonStandardAdapter.PaintThemedButtonBackground发现竟然是至关重要的一个美化按钮的外观 - 它调用ButtonRender.DrawButton,然后收缩了4PX矩形,无论的主题。 – Simon 2010-02-19 13:49:56