我想绘制一个矩形形状和文本。我的用户控件中有一个边框粗细和边框画笔属性。但是我得到一个没有边框的矩形。
我的代码:
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);
}