GroupBox 重繪圓角邊框和文字


        private void GroupBox_Paint(object sender, PaintEventArgs e)
        {
            if (sender != null && sender is GroupBox)
            {
                GroupBox gbx = sender as GroupBox;
                e.Graphics.Clear(gbx.BackColor);
                Color color = Color.Black;
                Pen p = new Pen(color, 1);
                int w = gbx.Width;
                int h = gbx.Height;
                Brush b = null;
                if (gbx.Parent != null)
                    b = new SolidBrush(gbx.Parent.BackColor);
                else
                    b = new SolidBrush(this.BackColor);
                //繪制直線
                e.Graphics.DrawLine(p, 3, h - 1, w - 4, h - 1);                     //bottom
                e.Graphics.DrawLine(p, 0, h - 4, 0, 12);                            //left
                e.Graphics.DrawLine(p, w - 1, h - 4, w - 1, 12);                    //right
                e.Graphics.FillRectangle(b, 0, 0, w, 8);
                e.Graphics.DrawLine(p, 3, 8, 10, 8);                                //lefg top
                e.Graphics.DrawLine(p,                                              //right top
                    e.Graphics.MeasureString(gbx.Text,
                    gbx.Font).Width + 8, 8, w - 4, 8);
                //繪制文字
                e.Graphics.DrawString(gbx.Text, gbx.Font, Brushes.Blue, 10, 0);     //title
                //繪制弧線
                e.Graphics.DrawArc(p, new Rectangle(0, 8, 10, 10), 180, 90);        //left top
                e.Graphics.DrawArc(p, new Rectangle(w - 11, 8, 10, 10), 270, 90);   //right top
                e.Graphics.DrawArc(p, new Rectangle(0, h - 11, 10, 10), 90, 90);    //left bottom
                e.Graphics.DrawArc(p, new Rectangle(w - 11, h - 11, 10, 10), 0, 90);//right bottom
            }
        }

 因為 GroupBox 自帶的灰色邊框太過難看,所以嘗試着對其重新繪制了一個新的邊框。最開始不帶圓角,感覺不太舒服,然后又添加了圓角,不過四個圓角的外側還是與GroupBox外的顏色不太一樣。試着用過一次路徑來做,但是GroupBox內側會有污點,不知道是怎么回事。

上面是沒有污點,但是四個圓角外有細微顏色不同的,下面是對四個圓角外側也做了着色,但是 GroupBox 內卻出現污點的。如果有哪位高人能夠幫忙解釋一下污點的問題和提出一些解決方法,本人感激不盡

        private void GroupBox_Paint(object sender, PaintEventArgs e)
        {
            if (sender != null && sender is GroupBox)
            {
                GroupBox gbx = sender as GroupBox;
                e.Graphics.Clear(gbx.BackColor);
                Color color = Color.Black;
                Pen p = new Pen(color, 1);
                int w = gbx.Width;
                int h = gbx.Height;
                Brush b = null;
                if (gbx.Parent != null)
                    b = new SolidBrush(gbx.Parent.BackColor);
                else
                    b = new SolidBrush(this.BackColor);
                e.Graphics.FillRectangle(Brushes.PapayaWhip, new Rectangle(0, 0, w, h));
                GraphicsPath gp = new GraphicsPath();
                gp.AddArc(new Rectangle(0, 8, 10, 10), 180, 90);                //left top
                gp.AddLine(0, h - 4, 0, 12);                                    //left
                gp.AddArc(new Rectangle(0, h - 11, 10, 10), 90, 90);            //left bottom
                gp.AddLine(3, h - 1, w - 4, h - 1);                             //bottom
                gp.AddArc(new Rectangle(w - 11, h - 11, 10, 10), 0, 90);        //right bottom
                gp.AddLine(w - 1, h - 4, w - 1, 12);                            //right
                gp.AddArc(new Rectangle(w - 11, 8, 10, 10), 270, 90);           //right top
                gp.AddLine(3, 8, w - 4, 8);                                     //top
                e.Graphics.FillRegion(Brushes.White, new Region(gp));
                //繪制直線
                e.Graphics.DrawLine(p, 3, h - 1, w - 4, h - 1);                     //bottom
                e.Graphics.DrawLine(p, 0, h - 4, 0, 12);                            //left
                e.Graphics.DrawLine(p, w - 1, h - 4, w - 1, 12);                    //right
                e.Graphics.FillRectangle(b, 0, 0, w, 8);
                e.Graphics.DrawLine(p, 3, 8, 10, 8);                                //lefg top
                e.Graphics.DrawLine(p,                                              //right top
                    e.Graphics.MeasureString(gbx.Text,
                    gbx.Font).Width + 8, 8, w - 4, 8);
                //繪制文字
                e.Graphics.DrawString(gbx.Text, gbx.Font, Brushes.Blue, 10, 0);     //title
                //繪制弧線
                e.Graphics.DrawArc(p, new Rectangle(0, 8, 10, 10), 180, 90);        //left top
                e.Graphics.DrawArc(p, new Rectangle(w - 11, 8, 10, 10), 270, 90);   //right top
                e.Graphics.DrawArc(p, new Rectangle(0, h - 11, 10, 10), 90, 90);    //left bottom
                e.Graphics.DrawArc(p, new Rectangle(w - 11, h - 11, 10, 10), 0, 90);//right bottom
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM