如何更改面板边框颜色

问题描述 投票:0回答:6

Panel
的属性中,我已将边框样式设置为
Fixed Single

当我运行我的应用程序时,它的颜色是灰色的。我不知道如何更改边框颜色。

我已经在面板的

Paint
事件处理程序中尝试过此操作:

private void HCp_Paint(object sender, PaintEventArgs e)
{
    Panel p = sender as Panel;
    ControlPaint.DrawBorder(e.Graphics, p.DisplayRectangle, Color.Yellow, ButtonBorderStyle.Inset);
}
        

这将显示如下边框:

Screenshot of actual result

但我想要一个像这样的固定单一边框:

Screenshot of desited result

如何将边框设为黄色?

c# .net winforms drawing
6个回答
26
投票

如果您不想按照 @Sinatr 的回答中的建议制作自定义面板,您可以自己绘制边框:

private void panel1_Paint(object sender, PaintEventArgs e)
{
     ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}

17
投票

您可以创建自己的

Panel
类并在客户区绘制边框:

[System.ComponentModel.DesignerCategory("Code")]
public class MyPanel : Panel
{
    public MyPanel() 
    {
        SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        using (SolidBrush brush = new SolidBrush(BackColor))
        e.Graphics.FillRectangle(brush, ClientRectangle);
        e.Graphics.DrawRectangle(Pens.Yellow, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
    }

}

10
投票

如果您不想麻烦地对面板进行子分类,您可以创建另一个每个尺寸大 2 像素的面板,将其设置为边框颜色,并将其直接放在需要边框的面板后面。 只需在 IDE 中点击几下...


4
投票

我发现这篇文章有用

我还将面板的填充设置为边框的厚度,以便面板内的控件不会与边框重叠并隐藏它。就我而言,我没有使用填充,所以这是一个很好的解决方案,但如果您还计划使用填充不仅仅是显示边框,事情可能会变得更加棘手......


1
投票

这也对我有用:

private void HCp_Paint(object sender, PaintEventArgs e)
{
    Panel p = sender as Panel;
    ControlPaint.DrawBorder(e.Graphics, p.DisplayRectangle, Color.Yellow, ButtonBorderStyle.Solid);
}

边框样式的问题是由于ButtomBorderStyle选项“Inset”造成的。通过选择“ButtonBorderStyle.Solid”,您将得到一条单线(也可以使用点线、虚线......)。

对于许多面板,我同意最好的解决方案是创建自己的类,继承自Panel并覆盖Paint方法...


0
投票

在创建自定义面板时采取解决方法后。当子控件的大小 > 面板的大小时,我被迫应用另一个调整来解决边框重叠问题。 在调整中,不是面板绘制其边框,而是由父控件绘制。

    Public Class SharpPanel : Inherits Panel
      Sub New()
        Padding = New Padding(2)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.ResizeRedraw, True)
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.ContainerControl, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.ContainerControl, True)
        Width = 100
        Height = 100
        TabStop = False
     End Sub
     Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim p As Control = Me.Parent
        Dim gr As Graphics = p.CreateGraphics
        Dim rec As Rectangle = Me.ClientRectangle
        If Me.VerticalScroll.Visible Then
            rec.Width = rec.Width + SystemInformation.VerticalScrollBarWidth
        End If
        If Me.HorizontalScroll.Visible Then
            rec.Height = rec.Height + SystemInformation.HorizontalScrollBarHeight
        End If
        rec.Location = Me.Location
        rec.Inflate(1, 1)
        gr.DrawRectangle(New Pen(Color.Pink), rec)
End sub
End Class
© www.soinside.com 2019 - 2024. All rights reserved.