protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { // Apparantly OnPaintBackground isn't used in CheckBox so we must do this here. if (BackColor == Color.Transparent && PInvoke.VisualStylesEnabled()) { IntPtr bghDC = e.Graphics.GetHdc(); PInvoke.Rect rBounds = new PInvoke.Rect(0, 0, Width, Height); PInvoke.DrawThemeParentBackground(Handle, bghDC, ref rBounds); e.Graphics.ReleaseHdc(bghDC); } else { using (SolidBrush sb = new SolidBrush(BackColor)) e.Graphics.FillRectangle(sb, new Rectangle(0, 0, Width, Height)); } if (!PInvoke.VisualStylesEnabled()) { base.OnPaint(e); return; } IntPtr hTheme = PInvoke.OpenThemeData(Handle, "Button"); if (hTheme == IntPtr.Zero) { base.OnPaint(e); return; } RectangleF leftF = new RectangleF(0, 0, 14, Height); RectangleF clipF = e.Graphics.VisibleClipBounds; PInvoke.Rect rLeft = new PInvoke.Rect(leftF); PInvoke.Rect rClip = /*rLeft;*/ new PInvoke.Rect(clipF); Rectangle rRight = new Rectangle(15, 0, Width - 15, Height); IntPtr hDC = e.Graphics.GetHdc(); PInvoke.DrawThemeBackground(hTheme, hDC, PInvoke.BP_CHECKBOX, checkBoxState(), ref rLeft, ref rClip); PInvoke.CloseThemeData(hTheme); e.Graphics.ReleaseHdc(hDC); if (Text != null && Text.Length > 0) { using (StringFormat sf = stringFormatOf(TextAlign)) using (SolidBrush sb = new SolidBrush(Enabled ? ForeColor : SystemColors.GrayText)) { e.Graphics.DrawString(Text, Font, sb, rRight, sf); if (Focused && !pressed) { SizeF sz = e.Graphics.MeasureString(Text, Font, rRight.Width, sf); Rectangle rText = new Rectangle(15, 0, (int)Math.Ceiling(sz.Width), (int)Math.Ceiling(sz.Height)); if (UseFocusRectangle) { ControlPaint.DrawFocusRectangle(e.Graphics, rText); } } } } }
protected override void OnPaintBackground(PaintEventArgs e) { if (!PInvoke.VisualStylesEnabled() || BackColor != Color.Transparent) { base.OnPaintBackground(e); return; } IntPtr hDC = e.Graphics.GetHdc(); PInvoke.Rect rBounds = new PInvoke.Rect(0, 0, Width, Height); PInvoke.DrawThemeParentBackground(Handle, hDC, ref rBounds); e.Graphics.ReleaseHdc(hDC); }
protected override void OnPaintBackground(PaintEventArgs e) { if (PInvoke.VisualStylesEnabled() && BackColor == Color.Transparent) { PInvoke.Rect rFull = new PInvoke.Rect(0, 0, Width, Height); IntPtr hDC = e.Graphics.GetHdc(); PInvoke.DrawThemeParentBackground(Handle, hDC, ref rFull); e.Graphics.ReleaseHdc(hDC); } else { Color useBackColor = BackColor; // If BackColor == Transparent, we must figure out the next Parent BackColor that isn't transparent and paint it ourself, since // ControlStyles.UserPaint and ControlStyles.AllPaintingInWmPaint are set. if (BackColor == Color.Transparent) { try { for (Control c = Parent; true; c = c.Parent) { if (c.BackColor != Color.Transparent) { useBackColor = c.BackColor; break; } } } catch { useBackColor = SystemColors.Control; } } using (SolidBrush sb = new SolidBrush(useBackColor)) e.Graphics.FillRectangle(sb, new Rectangle(0, 0, Width, Height)); } }
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); }