c# winform Label 边框 背景 颜色

  private void label1_Paint(object sender, PaintEventArgs e)
        {
            Label label1 = (Label)sender;
            DrawBorder(label1, e.Graphics, Color.Transparent, label1.Width, label1.Height);

        }

        private Color labelBorderColor = Color.Black;
        private SolidBrush SegBrush; //   功控填充颜色所用brush 
                                     /// <summary>
                                     /// //绘制边框
                                     /// </summary>
                                     /// <param name="g"></param>
                                     /// <param name="color">lable背景颜色</param>
                                     /// <param name="color">边框颜色</param>
                                     /// <param name="x">label宽度</param>
                                     /// <param name="y">label高度</param>
        private void DrawBorder(Label label1,System.Drawing.Graphics g, Color color, int x, int y)
        {


            SegBrush = new SolidBrush(color);
            Pen pen = new Pen(SegBrush, 1);
            //e.Graphics.FillRectangle(SegBrush, RcTime);

            label1.BorderStyle = BorderStyle.None;
            label1.BackColor = color;
          
            pen.Color = Color.White;

            Rectangle myRectangle = new Rectangle(0, 0, x, y);
            ControlPaint.DrawBorder(g, myRectangle, labelBorderColor, ButtonBorderStyle.Solid);//画个边框
                                                                                          // g.DrawRectangle(pen, myRectangle);
                                                                                          //g.DrawEllipse(pen, myRectangle);
        }

  

原文地址:https://www.cnblogs.com/Betty-IT/p/13157770.html