Ejemplo n.º 1
0
 public static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO pbmi, uint iUsage, int ppvBits, IntPtr hSection, uint dwOffset);
Ejemplo n.º 2
0
        // Draws theme text on glass.
        public static void DrawTextOnGlass(Graphics graphics, String text, Font font, Rectangle bounds, int glowSize)
        {
            if (IsGlassEnabled)
            {
                IntPtr destdc = IntPtr.Zero;
                try
                {
                    destdc = graphics.GetHdc();
                    IntPtr Memdc = CreateCompatibleDC(destdc);   // Set up a memory DC where we'll draw the text.
                    IntPtr bitmap;
                    IntPtr bitmapOld = IntPtr.Zero;
                    IntPtr logfnotOld;

                    //int uFormat = DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX;   //text format

                    BITMAPINFO dib = new BITMAPINFO();
                    dib.bmiHeader.biHeight      = -bounds.Height;    // negative because DrawThemeTextEx() uses a top-down DIB
                    dib.bmiHeader.biWidth       = bounds.Width;
                    dib.bmiHeader.biPlanes      = 1;
                    dib.bmiHeader.biSize        = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
                    dib.bmiHeader.biBitCount    = 32;
                    dib.bmiHeader.biCompression = BI_RGB;
                    if (!(SaveDC(Memdc) == 0))
                    {
                        bitmap = CreateDIBSection(Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0);   // Create a 32-bit bmp for use in offscreen drawing when glass is on
                        if (!(bitmap == IntPtr.Zero))
                        {
                            bitmapOld = SelectObject(Memdc, bitmap);
                            IntPtr hFont = font.ToHfont();
                            logfnotOld = SelectObject(Memdc, hFont);
                            try
                            {
                                VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active);

                                DTTOPTS dttOpts = new DTTOPTS();
                                dttOpts.dwSize    = (uint)Marshal.SizeOf(typeof(DTTOPTS));
                                dttOpts.dwFlags   = DTT_COMPOSITED | DTT_GLOWSIZE;
                                dttOpts.iGlowSize = glowSize;

                                RECT rc2   = new RECT(0, 0, bounds.Width, bounds.Height);
                                int  dtter = DrawThemeTextEx(renderer.Handle, Memdc, 0, 0, text, -1, uFormat, ref rc2, ref dttOpts);
                                bool bbr   = BitBlt(destdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Memdc, 0, 0, SRCCOPY);
                                if (!bbr)
                                {
                                    //throw new Exception("???");
                                }
                            }
                            catch (Exception)
                            {
                                //Log.Print(e.ToString());
                                //throw new Exception("???");
                            }

                            //Remember to clean up
                            SelectObject(Memdc, bitmapOld);
                            SelectObject(Memdc, logfnotOld);
                            DeleteObject(bitmap);
                            DeleteObject(hFont);

                            ReleaseDC(Memdc, -1);
                            DeleteDC(Memdc);
                        }
                        else
                        {
                            //throw new Exception("???");
                        }
                    }
                    else
                    {
                        //throw new Exception("???");
                    }
                }
                finally
                {
                    if (destdc != IntPtr.Zero)
                    {
                        graphics.ReleaseHdc(destdc);
                    }
                }
            }
        }