How to change Panel Border Color

2019-03-17 22:15发布

问题:

IDE : Visual Studio, C#.net,Type= Windows form application

Hi, In Panel Properties I have set the border style to Fixed Single".
when I am running my application its giving me Grey color. I don't know How to change the border color. I have tried in Paint event of panel

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

its giving me border like this:

http://i772.photobucket.com/albums/yy9/yogeshkmrsoni/giving_zps877730fc.png

and I want fixed single border like this:

http://i772.photobucket.com/albums/yy9/yogeshkmrsoni/want_zps081e3591.png

I am able to get FixedSingle Border but it is in Grey color which is default by system or IDE.

So pls suggest me how I make it in yellow color.

回答1:

You can create own Panel class and draw border in the client area:

[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);
    }

}


回答2:

I found this post useful:

https://vicky4147.wordpress.com/2007/03/04/how-to-draw-a-custom-border-around-a-form-or-control/

I also set the padding of the panel to the thickness of the border so that controls inside the panel don't overlap the border and hide it. In my case I wasn't using the padding otherwise so it was a good solution, but things might get more tricky if you also plan on using the padding for more than just showing the border...



回答3:

If you don't want to go to the trouble of sub-classing a panel, you can just create another panel 2 pixels bigger in each dimension, make it the border color and sit it directly behind the panel that needs a border. This is just a few clicks in the IDE ...



回答4:

just in case you dont want to make custom panel as Sinatra answered:

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


回答5:

After a workaround when creating my custom panel. I was forced to apply another tweak to solve the border overlapping when the size of the child control(s) > size of the panel. In the tweak, instead of the panel drawing its border, its drawn by the parent control.

    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