Skip to main content
 首页 » 编程设计

c#之面板的两个边框

2024年02月05日10hnrainll

我已完成以下操作,但问题是整个面板双边框未正确填充。//Inner Border 的右下方线条未绘制。
目标控件是Panel

protected override void OnPaint(PaintEventArgs pe) 
    { 
        using (SolidBrush brush = new SolidBrush(BackColor)) 
            pe.Graphics.FillRectangle(brush, this.Bounds); 
 
        // Inner Border 
        pe.Graphics.DrawRectangle(new Pen(Color.FromArgb(_InnerBorderColor.R, _InnerBorderColor.B, _InnerBorderColor.G), 1.0f), 1, 1, ClientSize.Width - 1, ClientSize.Height - 1); 
        using (SolidBrush brush = new SolidBrush(BackColor)) 
            pe.Graphics.FillRectangle(brush, this.Bounds); 
 
        // Main Border 
        pe.Graphics.DrawRectangle(new Pen(Color.FromArgb(_BorderColor.R, _BorderColor.B, _BorderColor.G), 1.0f), 0, 0, ClientSize.Width - 1, ClientSize.Height - 1); 
    } 

这是我得到的

这就是我需要的

已编辑

请您参考如下方法:

你应该使用这个坐标:

e.Graphics.DrawRectangle(pen1,  
    this.ClientRectangle.Left, this.ClientRectangle.Top, 
    this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1); 
e.Graphics.DrawRectangle(pen2,  
    this.ClientRectangle.Left + 1, this.ClientRectangle.Top + 1, 
    this.ClientRectangle.Width - 3, this.ClientRectangle.Height - 3); 

注意:

  • 对于外矩形,你应该从原来的宽度和高度减去-1才能可见
  • 要绘制内部矩形,您应该从 1,1 开始绘制,因此宽度和高度将比外部宽度和高度小 2 磅,并且由于外部宽度和高度为原始宽度和高度 -1,因此您需要减去 - 3 来自原始宽度和高度。