C# 钢笔颜色和宽度的默认值
我想画一个矩形形状和文本。我的用户控件中有边框厚度和边框笔刷属性。但是我得到的是一个没有边框的矩形。C# 钢笔颜色和宽度的默认值,c#,winforms,border,rectangles,graphic,C#,Winforms,Border,Rectangles,Graphic,我想画一个矩形形状和文本。我的用户控件中有边框厚度和边框笔刷属性。但是我得到的是一个没有边框的矩形。 我的代码: float borderThickness; [Description(""), Category("Appearance"), DefaultValue(1.0)] public float BorderThickness { get { return borderThickness; } set { borderThickness
我的代码:
float borderThickness;
[Description(""), Category("Appearance"), DefaultValue(1.0)]
public float BorderThickness
{
get {
return borderThickness;
}
set {
borderThickness = value;
Invalidate();
}
}
//it is hex value.
Color borderBrush;
[Description(""), Category("Appearance"), DefaultValue("FF0000")]
public Color BorderBrush {
get {
return borderBrush;
}
set {
borderBrush = value;
Invalidate();
}
}
void DrawRect(Color br, float x, float y, float w, float h)
{
g.DrawRectangle(br, x, y, w, h);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen selPen = new Pen(this.BorderBrush,this.BorderThickness);
this.Text = Number;
DrawRect(selPen,this.ClientRectangle);
}
画画:
float borderThickness;
[Description(""), Category("Appearance"), DefaultValue(1.0)]
public float BorderThickness
{
get {
return borderThickness;
}
set {
borderThickness = value;
Invalidate();
}
}
//it is hex value.
Color borderBrush;
[Description(""), Category("Appearance"), DefaultValue("FF0000")]
public Color BorderBrush {
get {
return borderBrush;
}
set {
borderBrush = value;
Invalidate();
}
}
void DrawRect(Color br, float x, float y, float w, float h)
{
g.DrawRectangle(br, x, y, w, h);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen selPen = new Pen(this.BorderBrush,this.BorderThickness);
this.Text = Number;
DrawRect(selPen,this.ClientRectangle);
}
乍一看,您是在混合字段初始值设定项和
DefaultValueAttribute
。后者不会将1.0
值设置为您的属性,它仅由PropertyGrid
用于在属性值不等于它时设置粗体文本。除非您在某个地方设置它们,否则您的属性具有默认值(例如,BorderThickness=0
)。尝试明确设置初始值,例如float borderThickness=1f
.DrawRect(selPen,this.ClientRectangle);DrawRectangle将过度绘制最后一个像素(右下角(),因为它是从像素中心绘制的),因此您需要将矩形缩小1像素。谢谢!我删除了DefaultValue并添加了float borderThickness=1f;颜色边框笔刷=Color.Red。现在可以工作了。@sinatr乍一看,您是在混合字段初始值设定项和DefaultValueAttribute
。后者不会将1.0
值设置为您的属性,它仅由PropertyGrid
用于在属性值不等于它时设置粗体文本。除非您在某个地方设置它们,否则您的属性具有默认值(例如,BorderThickness=0
)。尝试明确设置初始值,例如float borderThickness=1f
.DrawRect(selPen,this.ClientRectangle);DrawRectangle将过度绘制最后一个像素(右下角(),因为它是从像素中心绘制的),因此您需要将矩形缩小1像素。谢谢!我删除了DefaultValue并添加了float borderThickness=1f;颜色边框笔刷=Color.Red。“现在开始工作了。”@Sinatr