protected override void OnPaint(PaintEventArgs e) { if (!PInvoke.VisualStylesEnabled()) { base.OnPaint(e); return; } IntPtr hTheme = PInvoke.OpenThemeData(Handle, "Button"); if (hTheme == IntPtr.Zero) { base.OnPaint(e); return; } SizeF sz; // Text needs to be measured here because e.Graphics will soon be tied up with GetHdc() if (Text != null && Text.Length > 0) { sz = e.Graphics.MeasureString(Text, Font, DisplayRectangle.Width - 17, StringFormat.GenericDefault); } else { sz = new SizeF(); // have to set sz or else compiler will complain } // You'd think the height should be (Height - (Font.Height/2)), but using full Height gives a size consistent with FlatStyle.System XP GroupBox'es PInvoke.Rect rBounds = new PInvoke.Rect(0, Font.Height / 2, Width, Height); PInvoke.Rect rClip = new PInvoke.Rect(e.Graphics.VisibleClipBounds); IntPtr hDC = e.Graphics.GetHdc(); PInvoke.DrawThemeBackground(hTheme, hDC, PInvoke.BP_GROUPBOX, (Enabled ? PInvoke.GBS_NORMAL : PInvoke.GBS_DISABLED), ref rBounds, ref rClip); if (Text != null && Text.Length > 0) { RectangleF rText = new RectangleF(7, 0, sz.Width + 3.2F, sz.Height); PInvoke.Rect wrText = new PInvoke.Rect(rText); // Redraw the background over the border where text will be displayed if (BackColor == Color.Transparent) { PInvoke.DrawThemeParentBackground(Handle, hDC, ref wrText); e.Graphics.ReleaseHdc(hDC); } else { e.Graphics.ReleaseHdc(hDC); using (SolidBrush sb = new SolidBrush(BackColor)) e.Graphics.FillRectangle(sb, rText); } Color textColor; if (Enabled) { PInvoke.ColorRef rColor = new PInvoke.ColorRef(); PInvoke.GetThemeColor(hTheme, PInvoke.BP_GROUPBOX, PInvoke.GBS_NORMAL, PInvoke.TMT_TEXTCOLOR, ref rColor); textColor = rColor.ToColor(); } else // GetThemeColor (... GBS_DISABLED ...) always returns the default color - so use GrayText { textColor = SystemColors.GrayText; } rText.Inflate(-0.8F, 0); rText.Offset(0.4F, 0); using (SolidBrush sb = new SolidBrush(textColor)) e.Graphics.DrawString(Text, Font, sb, rText, StringFormat.GenericDefault); } else { e.Graphics.ReleaseHdc(hDC); } PInvoke.CloseThemeData(hTheme); }