c#控件样式详解(一)button控件

c#Windows窗体中,每个控件都有自己的属性,可以在属性中修改简单的样式,如前景色、背景图片等,但是如果想要窗体更美观,就需要通过控件的paint函数进行绘制。

每个按钮都在一个矩形内部,矩形内部左上角坐标为(0,0),以最常见的按钮为例,绘制按钮所在矩形:

        public static void button1_Paint(object sender, PaintEventArgs e)
        {
            simplebutton.ForeColor = Color.DodgerBlue;//按钮字体颜色

            e.Graphics.DrawLine(Pens.Red, 0, 0, 0, button1.Height);//左边线
            e.Graphics.DrawLine(Pens.Red, 0, 0, button1.Width, 0);//上边线
            e.Graphics.DrawLine(Pens.Red, button1.Width, 0, button1.Width, button1.Height);//右边线
            e.Graphics.DrawLine(Pens.Red, 0, button1.Height, button1.Width, button1.Height);//下边线
        }

效果如图所示:

          

由于矩形下边线和右边线超出了控件显示范围,可以通过修改坐标值来改变显示效果,修改代码:

        public static void button1_Paint(object sender, PaintEventArgs e)
        {
            simplebutton.ForeColor = Color.DodgerBlue;//按钮字体颜色

            e.Graphics.DrawLine(Pens.Red, 0, 0, 0, button1.Height);//左边线
            e.Graphics.DrawLine(Pens.Red, 0, 0, button1.Width, 0);//上边线
            e.Graphics.DrawLine(Pens.Red, button1.Width - 1, 0, button1.Width - 1, button1.Height - 1);//右边线
            e.Graphics.DrawLine(Pens.Red, 0, button1.Height - 1, button1.Width - 1, button1.Height - 1);//下边线
        }

修改后的效果:

          

 

 

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值