public void Update(Graphics g, string text, Font font, Padding internalBounds, Rectangle bounds, Color color, TextFormatFlags formatFlags, IThemeTextOption[] options) { IntPtr compatHdc = this._TextHDC; if (bounds.Equals(_TextHDCBounds)) { //Same bounds, reuse HDC and Clear it IntPtr hClearBrush = Native.GDI.CreateSolidBrush(ColorTranslator.ToWin32(Color.Black)); Native.RECT cleanRect = new Native.RECT(bounds); Native.GDI.FillRect(compatHdc, ref cleanRect, hClearBrush); Native.GDI.DeleteObject(hClearBrush); } else { //Other bounds, create new HDC IntPtr outputHdc = g.GetHdc(); IntPtr DIB; compatHdc = CreateNewHDC(outputHdc, bounds, out DIB); //Store new data _TextHDC = compatHdc; _TextHDCBounds = bounds; //Clear up CleanUpHDC(DIB); g.ReleaseHdc(outputHdc); } DrawOnHDC(compatHdc, text, font, internalBounds, bounds, color, formatFlags, options); }
public static ThemedText Create(Graphics g, string text, Font font, Padding internalBounds, Rectangle bounds, Color color, TextFormatFlags formatFlags, IThemeTextOption[] options) { //Get HDC and create a compatible HDC IntPtr outputHdc = g.GetHdc(); IntPtr DIB; IntPtr compatHdc = CreateNewHDC(outputHdc, bounds, out DIB); //Draw DrawOnHDC(compatHdc, text, font, internalBounds, bounds, color, formatFlags, options); //Clean up CleanUpHDC(DIB); g.ReleaseHdc(outputHdc); //Return the wrapped HDC return new ThemedText(compatHdc, bounds); }
protected IThemeTextOption[] CreateOptions() { IThemeTextOption[] options = new IThemeTextOption[CountOptions()]; int curr = 0; if (_glowEnable) { options[curr] = new GlowOption(_glowSize); ++curr; } if (_shadowEnable) { options[curr] = new ShadowOption(_shadowColor, _shadowOffset, _shadowType); ++curr; } if (!_overlayEnable) { options[curr] = new OverlayOption(_overlayEnable); ++curr; } if (_borderEnable) { options[curr] = new BorderOption(_borderColor, _borderSize); ++curr; } return options; }
private static void DrawOnHDC(IntPtr compatHdc, string text, Font font, Padding internalBounds, Rectangle bounds, Color color, TextFormatFlags formatFlags, IThemeTextOption[] options) { //Create the Font to use IntPtr hFont = font.ToHfont(); Native.GDI.SelectObject(compatHdc, hFont); //Get theme renderer VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active); //Prepare options NativeMethods.DTTOPTS dttOpts = new NativeMethods.DTTOPTS(); dttOpts.dwSize = Marshal.SizeOf(dttOpts); dttOpts.dwFlags = NativeMethods.DTTOPSFlags.DTT_COMPOSITED | NativeMethods.DTTOPSFlags.DTT_TEXTCOLOR; dttOpts.crText = ColorTranslator.ToWin32(color); foreach (IThemeTextOption op in options) op.Apply(ref dttOpts); //Set full bounds with padding Native.RECT RECT = new Native.RECT(internalBounds.Left, internalBounds.Top, bounds.Width - internalBounds.Right, bounds.Height - internalBounds.Bottom); //Draw int ret = NativeMethods.DrawThemeTextEx(renderer.Handle, compatHdc, 0, 0, text, -1, (int)formatFlags, ref RECT, ref dttOpts); if (ret != 0) Marshal.ThrowExceptionForHR(ret); //Clean up Native.GDI.DeleteObject(hFont); }