From 0804effa0304a584e460ad8622f802ec8aabe371 Mon Sep 17 00:00:00 2001 From: David Hall Date: Thu, 10 Jan 2019 08:03:39 -0700 Subject: [PATCH] Replaced DrawWrapper with DrawViaDIB method --- .../Extensions/VisualStylesRendererExtensionGDI.cs | 113 ++++++++------------- 1 file changed, 42 insertions(+), 71 deletions(-) diff --git a/WIndows.Forms/Extensions/VisualStylesRendererExtensionGDI.cs b/WIndows.Forms/Extensions/VisualStylesRendererExtensionGDI.cs index afdc7ad8..0897878a 100644 --- a/WIndows.Forms/Extensions/VisualStylesRendererExtensionGDI.cs +++ b/WIndows.Forms/Extensions/VisualStylesRendererExtensionGDI.cs @@ -12,8 +12,8 @@ using static Vanara.PInvoke.UxTheme; namespace Vanara.Extensions { /// - /// Extension methods for for glass effects and extended method functionality. Also provides GetFont2 and GetMargins2 - /// methods that corrects base library's non-functioning methods. + /// Extension methods for for glass effects and extended method functionality. Also provides GetFont2 + /// and GetMargins2 methods that corrects base library's non-functioning methods. /// public static partial class VisualStylesRendererExtension { @@ -22,7 +22,8 @@ namespace Vanara.Extensions private delegate void DrawWrapperMethod(HDC hdc); /// - /// Draws the background image of the current visual style element within the specified bounding rectangle and optionally clipped to the specified clipping rectangle. + /// Draws the background image of the current visual style element within the specified bounding rectangle and optionally clipped to + /// the specified clipping rectangle. /// /// The instance. /// The used to draw the background image. @@ -51,7 +52,8 @@ namespace Vanara.Extensions } /// - /// Draws the background image of the current visual style element onto a glass background within the specified bounding rectangle and optionally clipped to the specified clipping rectangle. + /// Draws the background image of the current visual style element onto a glass background within the specified bounding rectangle + /// and optionally clipped to the specified clipping rectangle. /// /// The instance. /// The used to draw the background image. @@ -61,8 +63,7 @@ namespace Vanara.Extensions public static void DrawGlassBackground(this VisualStyleRenderer rnd, IDeviceContext dc, Rectangle bounds, Rectangle? clipRectangle = null, bool rightToLeft = false) { var ht = new SafeHTHEME(rnd.Handle, false); - DrawWrapper(dc, bounds, - memoryHdc => + dc.DrawViaDIB(bounds, (memoryHdc, b) => { var rBounds = new RECT(bounds); //var opts = new DrawThemeBackgroundOptions(clipRectangle); @@ -78,9 +79,7 @@ namespace Vanara.Extensions ); } - /// - /// Draws the image from the specified within the specified bounds on a glass background. - /// + /// Draws the image from the specified within the specified bounds on a glass background. /// The instance. /// The used to draw the image. /// A in which the image is drawn. @@ -89,28 +88,23 @@ namespace Vanara.Extensions public static void DrawGlassImage(this VisualStyleRenderer rnd, IDeviceContext g, Rectangle bounds, ImageList imageList, int imageIndex) { var ht = new SafeHTHEME(rnd.Handle, false); - DrawWrapper(g, bounds, - memoryHdc => - { - DrawThemeIcon(ht, memoryHdc, rnd.Part, rnd.State, bounds, imageList.Handle, imageIndex); - } - ); + g.DrawViaDIB(bounds, (memoryHdc, b) => + DrawThemeIcon(ht, memoryHdc, rnd.Part, rnd.State, bounds, imageList.Handle, imageIndex)); } - /// - /// Draws the specified image within the specified bounds on a glass background. - /// - /// The instance. - /// The used to draw the image. - /// A in which the image is drawn. - /// An that contains the to draw. - /// if set to true draws the image in a disabled state using the method. + /// Draws the specified image within the specified bounds on a glass background. + /// The instance. + /// The used to draw the image. + /// A in which the image is drawn. + /// An that contains the to draw. + /// + /// if set to true draws the image in a disabled state using the method. + /// public static void DrawGlassImage(this VisualStyleRenderer rnd, IDeviceContext g, Rectangle bounds, Image image, bool disabled = false) { - DrawWrapper(g, bounds, - memoryHdc => + g.DrawViaDIB(bounds, (memoryHdc, b) => { - using (var mg = Graphics.FromHdc((IntPtr)memoryHdc)) + using (var mg = Graphics.FromHdc(memoryHdc.DangerousGetHandle())) { if (disabled) ControlPaint.DrawImageDisabled(mg, image, bounds.X, bounds.Y, Color.Transparent); @@ -124,25 +118,24 @@ namespace Vanara.Extensions /// /// Draws glowing text in the specified bounding rectangle with the option of overriding text color and applying other text formatting. /// - /// The instance. - /// The used to draw the text. - /// A in which the text is drawn. + /// The instance. + /// The used to draw the text. + /// A in which the text is drawn. /// The text to draw. /// Optional font override. /// Optionally, the color to draw text in overriding the default color for the theme. - /// A bitwise combination of the values. + /// A bitwise combination of the values. /// The size of the glow. public static void DrawGlowingText(this VisualStyleRenderer rnd, IDeviceContext dc, Rectangle bounds, string text, Font font, Color? color, TextFormatFlags flags = TextFormatFlags.Default, int glowSize = 10) { var ht = new SafeHTHEME(rnd.Handle, false); - DrawWrapper(dc, bounds, - memoryHdc => + dc.DrawViaDIB(bounds, (memoryHdc, b) => { // Create and select font using (new GdiObjectContext(memoryHdc, new SafeHFONT(font?.ToHfont() ?? IntPtr.Zero))) { // Draw glowing text - var dttOpts = new DTTOPTS(null) {GlowSize = glowSize, AntiAliasedAlpha = true}; + var dttOpts = new DTTOPTS(null) { GlowSize = glowSize, AntiAliasedAlpha = true }; if (color != null) dttOpts.TextColor = color.Value; var textBounds = new RECT(4, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top); DrawThemeTextEx(ht, memoryHdc, rnd.Part, rnd.State, text, text.Length, FromTFF(flags), ref textBounds, dttOpts); @@ -151,12 +144,10 @@ namespace Vanara.Extensions ); } - /// - /// Draws text in the specified bounding rectangle with the option of applying other text formatting. - /// - /// The instance. - /// The used to draw the text. - /// A in which the text is drawn. + /// Draws text in the specified bounding rectangle with the option of applying other text formatting. + /// The instance. + /// The used to draw the text. + /// A in which the text is drawn. /// The text to draw. /// A bitwise combination of the values. /// The . @@ -169,10 +160,9 @@ namespace Vanara.Extensions bounds = rc; } - private static DrawTextFlags FromTFF(TextFormatFlags tff) => (DrawTextFlags)(int)tff; - /// - /// Gets the background image of the current visual style element within the specified background color. If is set, the resulting image will contain each of the state images side by side. + /// Gets the background image of the current visual style element within the specified background color. If + /// is set, the resulting image will contain each of the state images side by side. /// /// The instance. /// The background color. This color cannot have an alpha channel. @@ -234,45 +224,26 @@ namespace Vanara.Extensions } } - private static Size MaxSize(Size sz1, Size sz2) => new Size(Math.Max(sz1.Width, sz2.Width), Math.Max(sz1.Height, sz2.Height)); - - /// - /// Returns the value of the specified font property for the current visual style element. - /// - /// The instance. - /// The used to draw the text. + /// Returns the value of the specified font property for the current visual style element. + /// The instance. + /// The used to draw the text. /// A value to return if the system has no font defined for this instance. - /// A that contains the value of the property specified by the prop parameter for the current visual style element. + /// + /// A that contains the value of the property specified by the prop parameter for the current visual style element. + /// public static Font GetFont2(this VisualStyleRenderer rnd, IDeviceContext dc = null, Font defaultValue = null) { using (var hdc = new SafeHDC(dc)) { - return 0 != GetThemeFont(new SafeHTHEME(rnd.Handle, false), hdc, rnd.Part, rnd.State, 210, out LOGFONT f) + return 0 != GetThemeFont(new SafeHTHEME(rnd.Handle, false), hdc, rnd.Part, rnd.State, 210, out var f) ? defaultValue : Font.FromLogFont(f); } } - private static void DrawWrapper(IDeviceContext dc, Rectangle bounds, DrawWrapperMethod func) - { - using (var sdc = new SafeHDC(dc)) - { - // Create a memory DC so we can work off screen - using (var memoryHdc = sdc.GetCompatibleDCHandle()) - { - // Create a device-independent bitmap and select it into our DC - var info = new BITMAPINFO(bounds.Width, -bounds.Height); - using (memoryHdc.SelectObject(CreateDIBSection(sdc, ref info, 0, out var pBits, IntPtr.Zero, 0))) - { - // Call method - func(memoryHdc); - - // Copy to foreground - BitBlt(sdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, RasterOperationMode.SRCCOPY); - } - } - } - } + private static DrawTextFlags FromTFF(TextFormatFlags tff) => (DrawTextFlags)(int)tff; private static long GetHashCode(this VisualStyleRenderer r) => (long)r.Class.GetHashCode() << 32 | ((uint)r.Part << 16 | (ushort)r.State); + + private static Size MaxSize(Size sz1, Size sz2) => new Size(Math.Max(sz1.Width, sz2.Width), Math.Max(sz1.Height, sz2.Height)); } } \ No newline at end of file