Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6. MyGroupBox mgb1 = new MyGroupBox();
  7. mgb1.Name = "GroupBox1";
  8. mgb1.BackColor = Color.LightBlue;
  9. mgb1.ForeColor = Color.LightBlue;
  10. mgb1.Text = "test";
  11. mgb1.Location = new System.Drawing.Point(53, 35);
  12. mgb1.Size = new System.Drawing.Size(200, 100);
  13.  
  14.  
  15. }
  16.  
  17. public class MyGroupBox : GroupBox
  18. {
  19. private Color _borderColor = Color.Black;
  20.  
  21. public Color BorderColor
  22. {
  23. get { return this._borderColor; }
  24. set { this._borderColor = value; }
  25. }
  26.  
  27. protected override void OnPaint(PaintEventArgs e)
  28. {
  29. //get the text size in groupbox
  30. Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
  31.  
  32. Rectangle borderRect = e.ClipRectangle;
  33. borderRect.Y = (borderRect.Y + (tSize.Height / 2));
  34. borderRect.Height = (borderRect.Height - (tSize.Height / 2));
  35. ControlPaint.DrawBorder(e.Graphics, borderRect, this._borderColor, ButtonBorderStyle.Solid);
  36.  
  37. Rectangle textRect = e.ClipRectangle;
  38. textRect.X = (textRect.X + 6);
  39. textRect.Width = tSize.Width;
  40. textRect.Height = tSize.Height;
  41. e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
  42. e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement