C# 更改NumericUpDown的边框颜色
我对C#很陌生,有一个问题。我已经能够通过将按钮的平面样式更改为“平面”来更改按钮等的边框颜色。使用C# 更改NumericUpDown的边框颜色,c#,.net,winforms,numericupdown,C#,.net,Winforms,Numericupdown,我对C#很陌生,有一个问题。我已经能够通过将按钮的平面样式更改为“平面”来更改按钮等的边框颜色。使用NumericUpDown,我无法更改平面样式。我希望仍然能够使用向上和向下箭头,所以仅仅使用其他东西来覆盖边缘将不起作用。下面是我在代码中所做工作的简化版本: using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Li
NumericUpDown
,我无法更改平面样式。我希望仍然能够使用向上和向下箭头,所以仅仅使用其他东西来覆盖边缘将不起作用。下面是我在代码中所做工作的简化版本:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bordertest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BackColor = Color.Black;
numericUpDown1.BackColor = Color.Red;
}
}
}
您可以从
NumericUpDown
派生,添加BorderColor
属性,替代OnPaint
并基于边框颜色绘制边框
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class MyNumericUpDown : NumericUpDown
{
private Color borderColor = Color.Blue;
[DefaultValue(typeof(Color), "0,0,255")]
public Color BorderColor
{
get { return borderColor; }
set
{
if (borderColor != value)
{
borderColor = value;
Invalidate();
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (BorderStyle != BorderStyle.None)
{
using (var pen = new Pen(BorderColor, 1))
e.Graphics.DrawRectangle(pen,
ClientRectangle.Left, ClientRectangle.Top,
ClientRectangle.Width - 1, ClientRectangle.Height - 1);
}
}
}
注意:就像旁注一样,此控件引发绘制事件,如果出于任何原因,有人希望在不继承的情况下实现相同的行为,他们可以处理绘制事件并绘制边框;然而,作为一种通用解决方案和可重用解决方案,派生控件更有意义
private void numericUpDown_Paint(object sender, PaintEventArgs e)
{
var c = (NumericUpDown)sender;
ControlPaint.DrawBorder(e.Graphics, c.ClientRectangle,
Color.Red, ButtonBorderStyle.Solid);
var r = new Rectangle(1, 1, c.Width - 2, c.Height - 2);
e.Graphics.SetClip(r);
}
FlatNumericUpDown
我已经创建了一个FlatNumericUpDown,它支持BorderColor
和ButtonHighlightColor
。您可以下载或克隆它:
- 存储库:
- 下载
一种自定义控件版本,它使用标准渲染功能替代绘制NumericUpDown(但也可以应用于许多其他控件)边框 这样就可以不费吹灰之力(实际上,没有:)绘制边框,并且有多种开箱即用的样式
边框样式
属性被阴影化,以返回扩展值,除默认的实线
样式外,扩展值还包括点和虚线
样式
一个公共的BorderColor
属性被添加到类中,它当然会出现在PropertyGrid中;它允许更改边框的颜色。它默认为
Color.DarkGray
,类似于SystemColors.ControlDark
如果希望精确,可以使用相同的属性样式来指定RGB值,而不是
“DarkGray”
将此类添加到项目中,构建项目,在工具箱中找到自定义控件,并像往常一样将其放到表单上(以防您不知道如何处理它:) ► 请注意,您可以对许多其他标准控件(包括文本框控件,它通常不会引发绘制事件)和您自己设计的所有自定义控件/组件使用相同的技术 这就是它看起来的样子:
查看此链接:作为旁注,此控件引发绘制事件,如果出于任何原因,有人希望在不继承的情况下实现相同的行为,他们可以处理绘制事件并绘制边框;但是,作为一种通用解决方案和可重用解决方案,派生控件更有意义。@RezeAghaei我尝试了您使用的第一种方法,VS 2019给了我错误“CS0119 BorderStyle”是一种类型,在给定上下文中无效”。不过,你在便条下给出的第二种方法确实有效。我想我应该更清楚我想要什么,因为薄边框的颜色确实改变了,这是我想要的一部分。我还想知道,我是否可以改变这些带有箭头的小按钮的颜色?这两种方法都经过测试,工作正常。可能你遗漏了什么。我还创建了一个FlatNumericUpDown存储库。在此基础上,您可以开始创建自己的FlatNumericUpDown。
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
[DesignerCategory("Code"), ToolboxItem(true)]
public class NumericUpDownEx : NumericUpDown
{
private const int WM_PAINT = 0x000F;
private ButtonBorderStyle m_BorderStyle = ButtonBorderStyle.Solid;
private Color m_BorderColor = Color.DarkGray;
public NumericUpDownEx() { }
public new ButtonBorderStyle BorderStyle {
get => m_BorderStyle;
set {
if (m_BorderStyle != value) {
m_BorderStyle = value;
Invalidate();
}
}
}
[DefaultValue(typeof(Color), "DarkGray")]
public Color BorderColor {
get => m_BorderColor;
set {
if (m_BorderColor != value) {
m_BorderColor = value;
Invalidate();
}
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg) {
case WM_PAINT:
if (IsHandleCreated) {
using (var g = Graphics.FromHwndInternal(this.Handle)) {
ControlPaint.DrawBorder(g, ClientRectangle, m_BorderColor, m_BorderStyle);
}
m.Result = IntPtr.Zero;
}
break;
}
}
}