コード例 #1
0
        public static void DrawTextOnGlass(Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags, int rflags)
        {
            IntPtr primaryHdc = graphics.GetHdc();

            IntPtr memoryHdc = NativeMethods.CreateCompatibleDC(new HandleRef(null, primaryHdc));

            NativeMethods.BITMAPINFO info = new NativeMethods.BITMAPINFO();
            info.bmiHeader_biSize        = Marshal.SizeOf(info);
            info.bmiHeader_biWidth       = bounds.Width;
            info.bmiHeader_biHeight      = -bounds.Height;
            info.bmiHeader_biPlanes      = 1;
            info.bmiHeader_biBitCount    = 32;
            info.bmiHeader_biCompression = 0;

            IntPtr ppbBits = IntPtr.Zero;

            IntPtr dib = NativeMethods.CreateDIBSection(primaryHdc, info, (uint)0, out ppbBits, IntPtr.Zero, (uint)0);

            NativeMethods.SelectObject(new HandleRef(null, memoryHdc), new HandleRef(null, dib));


            IntPtr fontHandle = font.ToHfont();

            NativeMethods.SelectObject(new HandleRef(null, memoryHdc), new HandleRef(null, fontHandle));


            System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
            DWMAPI.DTTOPTS dttOpts = new DWMAPI.DTTOPTS();
            dttOpts.dwSize    = Marshal.SizeOf(typeof(DWMAPI.DTTOPTS));
            dttOpts.dwFlags   = DWMAPI.DTT_COMPOSITED | rflags | DWMAPI.DTT_TEXTCOLOR;
            dttOpts.crText    = ColorTranslator.ToWin32(color);
            dttOpts.iGlowSize = 10;
            NativeMethods.RECT textBounds = new NativeMethods.RECT(0, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
            UXTheme.DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, -1, (int)flags, ref textBounds, ref dttOpts);

            const int SRCCOPY = 0x00CC0020;

            NativeMethods.BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY);


            NativeMethods.DeleteObject(new HandleRef(null, fontHandle));
            NativeMethods.DeleteObject(new HandleRef(null, dib));
            NativeMethods.DeleteDC(new HandleRef(null, memoryHdc));

            graphics.ReleaseHdc(primaryHdc);
        }
コード例 #2
0
        public static void DrawTextOnGlass(
            Graphics graphics,
            string text,
            Font font,
            Rectangle bounds,
            Color color,
            TextFormatFlags flags,
            int rflags)
        {
            IntPtr hdc          = graphics.GetHdc();
            IntPtr compatibleDc = NativeMethods.CreateCompatibleDC(new HandleRef((object)null, hdc));

            NativeMethods.BITMAPINFO pbmi = new NativeMethods.BITMAPINFO();
            pbmi.bmiHeader_biSize        = Marshal.SizeOf((object)pbmi);
            pbmi.bmiHeader_biWidth       = bounds.Width;
            pbmi.bmiHeader_biHeight      = -bounds.Height;
            pbmi.bmiHeader_biPlanes      = (short)1;
            pbmi.bmiHeader_biBitCount    = (short)32;
            pbmi.bmiHeader_biCompression = 0;
            IntPtr ppvBits    = IntPtr.Zero;
            IntPtr dibSection = NativeMethods.CreateDIBSection(hdc, pbmi, 0U, out ppvBits, IntPtr.Zero, 0U);

            NativeMethods.SelectObject(new HandleRef((object)null, compatibleDc), new HandleRef((object)null, dibSection));
            IntPtr hfont = font.ToHfont();

            NativeMethods.SelectObject(new HandleRef((object)null, compatibleDc), new HandleRef((object)null, hfont));
            VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active);

            DWMAPI.DTTOPTS pOptions = new DWMAPI.DTTOPTS();
            pOptions.dwSize    = Marshal.SizeOf(typeof(DWMAPI.DTTOPTS));
            pOptions.dwFlags   = 8192 | rflags | 1;
            pOptions.crText    = ColorTranslator.ToWin32(color);
            pOptions.iGlowSize = 10;
            NativeMethods.RECT pRect = new NativeMethods.RECT(0, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
            UXTheme.DrawThemeTextEx(visualStyleRenderer.Handle, compatibleDc, 0, 0, text, -1, (int)flags, ref pRect, ref pOptions);
            NativeMethods.BitBlt(hdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, compatibleDc, 0, 0, 13369376);
            NativeMethods.DeleteObject(new HandleRef((object)null, hfont));
            NativeMethods.DeleteObject(new HandleRef((object)null, dibSection));
            NativeMethods.DeleteDC(new HandleRef((object)null, compatibleDc));
            graphics.ReleaseHdc(hdc);
        }