使用 ControlPaint.DrawBorder 为所有边框着色

Colouring all the borders with ControlPaint.DrawBorder

我正在尝试通过 Paint 事件在我的 Winform 控件上进行一些绘画,因为为什么不呢。我已经连接好了,因为 StackOverlfow 告诉我它会起作用:

private void PaintLines(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
                    Color.Gray, 1, ButtonBorderStyle.Solid,
                    Color.Gray, 1, ButtonBorderStyle.Solid,
                    Color.Gray, 1, ButtonBorderStyle.Solid,
                    Color.Gray, 1, ButtonBorderStyle.Solid);
}

问题是只适用于顶部和左侧边框,不适用于右侧或底部。这是 Designer.cs 因为我怀疑控件的设置有问题。

this.lblOffset.AutoSize = true;
this.lblOffset.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblOffset.Location = new System.Drawing.Point(3, 25);
this.lblOffset.Name = "lblOffset";
this.lblOffset.Size = new System.Drawing.Size(114, 25);
this.lblOffset.TabIndex = 1;
this.lblOffset.Text = "Offset (V)";
this.lblOffset.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lblOffset.Paint += new System.Windows.Forms.PaintEventHandler(PaintLines);

所以问题是如何绘制我的四个边框?

您使用的是不正确的 ClientRectangle - 整个表格之一。因此,该矩形的 WidthHeight 值不适合 Label 的客户端矩形。

改用LabelClientRectangle

ControlPaint.DrawBorder(e.Graphics, lblOffset.ClientRectangle,
    Color.Gray, 1, ButtonBorderStyle.Solid,
    Color.Gray, 1, ButtonBorderStyle.Solid,
    Color.Gray, 1, ButtonBorderStyle.Solid,
    Color.Gray, 1, ButtonBorderStyle.Solid);