C#/vb.net WinForm修改Panel边框颜色

文章来源:365jz.com     点击数:185    更新时间:2017-11-13 17:01   参与评论
一、Winform Panel边框方法:每边能设置不同的颜色、宽度和样式

c#代码

private void panel1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics,
                                this.panel1.ClientRectangle,
                                Color.LightSeaGreen,//7f9db9
                                1,
                                ButtonBorderStyle.Solid,
                                Color.LightSeaGreen,
                                1,
                                ButtonBorderStyle.Solid,
                                Color.LightSeaGreen,
                                1,
                                ButtonBorderStyle.Solid,
                                Color.LightSeaGreen,
                                1,
                                ButtonBorderStyle.Solid);
}



vb.net代码:

Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
        ControlPaint.DrawBorder(e.Graphics, Me.panel1.ClientRectangle, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid)
End Sub


方法2:自定义panel控件
框架自带的Panel是默认不设置边框样式的,只能设置边框类型。

你需要自己在Paint事件中实现边框绘制。当然,自己继承一下panel改一改也是可以的。
下面的控件继承自Panel.

    public class PanelWithCustomBorder : Panel
    {
        public Color BorderColor;
        public int BorderWidth = 1;
        public ButtonBorderStyle BorderLineStyle =  ButtonBorderStyle.Solid;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (BorderColor != null)
                ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
                    BorderColor,BorderWidth,BorderLineStyle,
                    BorderColor, BorderWidth, BorderLineStyle,
                    BorderColor, BorderWidth, BorderLineStyle,
                    BorderColor, BorderWidth, BorderLineStyle);
        }
    }

使用BorderColor属性指定边框颜色,不指定则为默认;
使用BorderWidth指定边框宽度;

使用BorderLineStyle指定边框线条的样式。

你也可以在标准panel的Paint事件中使用ControlPaint完成一样的工作。

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛


发表评论 (185人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换图片
最新评论
------分隔线----------------------------