C# - C Sharp: Cách tạo màu và kích thước cho đường viền và chữ của GroupBox
Giải phóng thời gian, khai phóng năng lực
private void groupBox1_Paint(object sender, PaintEventArgs e) { GroupBox box = (GroupBox)sender; DrawGroupBox(box, e.Graphics, Color.Red, Color.Blue); } private void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor) { if (box != null) { Brush textBrush = new SolidBrush(textColor); //màu cho tiêu đề Brush borderBrush = new SolidBrush(borderColor); //màu cho đường viền Pen borderPen = new Pen(borderBrush, 5); //độ dày nét vẽ là 5 SizeF strSize = g.MeasureString(box.Text, box.Font); Rectangle rect = new Rectangle(box.ClientRectangle.X, box.ClientRectangle.Y + (int)(strSize.Height / 2), box.ClientRectangle.Width - 1, box.ClientRectangle.Height - (int)(strSize.Height / 2) - 1); // Xóa tiêu đề và đường viền g.Clear(this.BackColor); // Vẽ tiêu đề g.DrawString(box.Text, box.Font, textBrush, box.Padding.Left, 0); // Vẽ đường viền (border) //Đường viền trái g.DrawLine(borderPen, rect.Location, new Point(rect.X, rect.Y + rect.Height)); //Đường viền phải g.DrawLine(borderPen, new Point(rect.X + rect.Width, rect.Y), new Point(rect.X + rect.Width, rect.Y + rect.Height)); //Đường viền dưới g.DrawLine(borderPen, new Point(rect.X, rect.Y + rect.Height), new Point(rect.X + rect.Width, rect.Y + rect.Height)); //Đường viền trên bên trái text g.DrawLine(borderPen, new Point(rect.X, rect.Y), new Point(rect.X + box.Padding.Left, rect.Y)); //Đường viền trên bên phải text g.DrawLine(borderPen, new Point(rect.X + box.Padding.Left + (int)(strSize.Width), rect.Y), new Point(rect.X + rect.Width, rect.Y)); } }
Giải phóng thời gian, khai phóng năng lực