// Draw an XP-style close button using DrawThemeBackground() protected void drawThemeCloseButton(Graphics fx) { IntPtr hTheme = PInvoke.OpenThemeData(Handle, "Window"); if (hTheme == IntPtr.Zero) { drawLegacyCloseButton(fx); return; } int stateID; if (closePressed) { stateID = PInvoke.CBS_PUSHED; } else if (closeHover) { stateID = PInvoke.CBS_HOT; } else { stateID = PInvoke.CBS_NORMAL; } PInvoke.Rect reClose = new PInvoke.Rect(rClose); PInvoke.Rect reClip = reClose; //new PInvoke.Rect (e.Graphics.VisibleClipBounds); IntPtr hDC = fx.GetHdc(); PInvoke.DrawThemeBackground(hTheme, hDC, PInvoke.WP_CLOSEBUTTON, stateID, ref reClose, ref reClip); fx.ReleaseHdc(hDC); PInvoke.CloseThemeData(hTheme); }
protected override void OnPaintBackground(PaintEventArgs e) { if (!PInvoke.VisualStylesEnabled()) { using (SolidBrush sb = new SolidBrush(BackColor)) e.Graphics.FillRectangle(sb, DisplayRectangle); return; } IntPtr hTheme = PInvoke.OpenThemeData(Handle, "Tab"); if (hTheme == IntPtr.Zero) { using (SolidBrush sb = new SolidBrush(BackColor)) e.Graphics.FillRectangle(sb, DisplayRectangle); return; } PInvoke.Rect rDisplayRect = new PInvoke.Rect(DisplayRectangle); PInvoke.Rect rClipArea = new PInvoke.Rect(e.Graphics.VisibleClipBounds); IntPtr hDC = e.Graphics.GetHdc(); PInvoke.DrawThemeBackground(hTheme, hDC, PInvoke.TABP_BODY, 0, ref rDisplayRect, ref rClipArea); e.Graphics.ReleaseHdc(hDC); PInvoke.CloseThemeData(hTheme); }
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 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); }