Example #1
0
        public static void PaintShadowTextOnGlass(Graphics g, string text, Font font, Rectangle rect, ThemeTextFormat tf, Color textColor, Color shadowColor, bool copySourceBackground)
        {
            Point transformPoint = new Point();
            using (Matrix trans = g.Transform)
            {
                Point[] pts = new Point[1];
                pts[0] = transformPoint;
                trans.TransformPoints(pts);
                transformPoint = pts[0];
            }

            IntPtr hdc = g.GetHdc();
            const int SRCCOPY = 0x00CC0020;
            try
            {
                IntPtr memdc = WinApi.CreateCompatibleDC(hdc);
                try
                {
                    WinApi.BITMAPINFO info = new WinApi.BITMAPINFO();
                    info.biWidth = rect.Width;
                    info.biHeight = -rect.Height;
                    info.biPlanes = 1;
                    info.biBitCount = 32;
                    info.biSize = Marshal.SizeOf(info);
                    IntPtr dib = WinApi.CreateDIBSection(hdc, info, 0, 0, IntPtr.Zero, 0);
                    WinApi.SelectObject(memdc, dib);

                    IntPtr fontHandle = font.ToHfont();
                    WinApi.SelectObject(memdc, fontHandle);

                    Themes.RECT bounds = new Themes.RECT(new Rectangle(0, 0, rect.Width, rect.Height));
                    System.Windows.Forms.VisualStyles.VisualStyleRenderer themeRenderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
                    Themes.DTTOPTS dttOpts = new Themes.DTTOPTS();
                    dttOpts.crText = new Themes.COLORREF(shadowColor);
                    dttOpts.dwFlags = (int)Themes.DTT_VALIDBITS.DTT_COMPOSITED |
                        (int)Themes.DTT_VALIDBITS.DTT_TEXTCOLOR;
                    dttOpts.dwSize = Marshal.SizeOf(dttOpts);

                    if (copySourceBackground)
                        WinApi.BitBlt(memdc, 0, 0, rect.Width, rect.Height, hdc, rect.Left + transformPoint.X, rect.Top + transformPoint.Y, SRCCOPY);
                    // Shadow
                    bounds.Offset(1, 2);
                    Themes.DrawThemeTextEx(themeRenderer.Handle, memdc, 0, 0, text, -1, (int)tf, ref bounds, ref dttOpts);
                    bounds.Offset(-1, -1);
                    dttOpts.crText = new Themes.COLORREF(textColor);
                    Themes.DrawThemeTextEx(themeRenderer.Handle, memdc, 0, 0, text, -1, (int)tf, ref bounds, ref dttOpts);
                    //Themes.DrawThemeTextEx(themeRenderer.Handle, memdc, 0, 0, text, -1, (int)tf, ref bounds, ref dttOpts);

                    WinApi.BitBlt(hdc, rect.Left + transformPoint.X, rect.Top + transformPoint.Y, rect.Width, rect.Height, memdc, 0, 0, SRCCOPY);

                    WinApi.DeleteObject(fontHandle);
                    WinApi.DeleteObject(dib);
                }
                finally
                {
                    WinApi.DeleteDC(memdc);
                }
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
        }
Example #2
0
 protected virtual Themes.RECT GetThemeTextBounds(Rectangle rect)
 {
     Themes.RECT bounds = new Themes.RECT(new Rectangle(0, 0, rect.Width, rect.Height));
     return bounds;
 }
Example #3
0
        public static void PaintTextOnGlass(Graphics g, string text, Font font, Rectangle rect, ThemeTextFormat tf, Color textColor, bool copySourceBackground, bool renderGlow, int glowSize)
        {
            IntPtr hdc = g.GetHdc();
            const int SRCCOPY = 0x00CC0020;
            try
            {
                IntPtr memdc = WinApi.CreateCompatibleDC(hdc);
                try
                {
                    WinApi.BITMAPINFO info = new WinApi.BITMAPINFO();
                    info.biWidth = rect.Width;
                    info.biHeight = -rect.Height;
                    info.biPlanes = 1;
                    info.biBitCount = 32;
                    info.biSize = Marshal.SizeOf(info);
                    IntPtr dib = WinApi.CreateDIBSection(hdc, info, 0, 0, IntPtr.Zero, 0);
                    WinApi.SelectObject(memdc, dib);

                    IntPtr fontHandle = font.ToHfont();
                    WinApi.SelectObject(memdc, fontHandle);

                    Themes.RECT bounds = new Themes.RECT(new Rectangle(0, 0, rect.Width, rect.Height));
                    System.Windows.Forms.VisualStyles.VisualStyleRenderer themeRenderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
                    Themes.DTTOPTS dttOpts = new Themes.DTTOPTS();
                    dttOpts.iGlowSize = glowSize;
                    dttOpts.crText = new Themes.COLORREF(textColor);
                    dttOpts.dwFlags = (int)Themes.DTT_VALIDBITS.DTT_COMPOSITED |
                        (renderGlow ? (int)Themes.DTT_VALIDBITS.DTT_GLOWSIZE : 0) |
                        (int)Themes.DTT_VALIDBITS.DTT_TEXTCOLOR;
                    dttOpts.dwSize = Marshal.SizeOf(dttOpts);

                    if (copySourceBackground)
                        WinApi.BitBlt(memdc, 0, 0, rect.Width, rect.Height, hdc, rect.Left, rect.Top, SRCCOPY);

                    Themes.DrawThemeTextEx(themeRenderer.Handle, memdc, 0, 0, text, -1, (int)tf, ref bounds, ref dttOpts);

                    WinApi.BitBlt(hdc, rect.Left, rect.Top, rect.Width, rect.Height, memdc, 0, 0, SRCCOPY);

                    WinApi.DeleteObject(fontHandle);
                    WinApi.DeleteObject(dib);
                }
                finally
                {
                    WinApi.DeleteDC(memdc);
                }
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
        }