Beispiel #1
0
 public static extern int DrawThemeTextEx(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, string text, int iCharCount, int dwFlags, ref RECT pRect, ref DTTOPTS pOptions);
Beispiel #2
0
        public static void DrawTextOnGlass(IntPtr hwnd, String text, Font font, Rectangle ctlrct, int iglowSize)
        {
            if (IsCompositionEnabled())
            {
                RECT rc  = new RECT();
                RECT rc2 = new RECT();

                rc.left   = ctlrct.Left;
                rc.right  = ctlrct.Right;  // + 2 * iglowSize;  //make it larger to contain the glow effect
                rc.top    = ctlrct.Top;
                rc.bottom = ctlrct.Bottom; // + 2 * iglowSize;

                //Just the same rect with rc,but (0,0) at the lefttop
                rc2.left   = 0;
                rc2.top    = 0;
                rc2.right  = rc.right - rc.left;
                rc2.bottom = rc.bottom - rc.top;

                IntPtr destdc = WinAPI.GetDC(hwnd);                //hwnd must be the handle of form,not control
                IntPtr Memdc  = WinAPI.CreateCompatibleDC(destdc); // Set up a memory DC where we'll draw the text.
                IntPtr bitmap;
                IntPtr bitmapOld = IntPtr.Zero;
                IntPtr logfnotOld;

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

                IntPtr ptPixels = IntPtr.Zero;

                BITMAPINFO dib = new BITMAPINFO();
                dib.bmiHeader.biHeight      = -(rc.bottom - rc.top);    // negative because DrawThemeTextEx() uses a top-down DIB
                dib.bmiHeader.biWidth       = rc.right - rc.left;
                dib.bmiHeader.biPlanes      = 1;
                dib.bmiHeader.biSize        = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
                dib.bmiHeader.biBitCount    = 32;
                dib.bmiHeader.biCompression = Constants.BI_RGB;
                dib.bmiColors.rgbBlue       = 255;
                if (!(WinAPI.SaveDC(Memdc) == 0))
                {
                    bitmap = WinAPI.CreateDIBSection(Memdc, ref dib, Constants.DIB_RGB_COLORS, out ptPixels, IntPtr.Zero, 0);   // Create a 32-bit bmp for use in offscreen drawing when glass is on
                    if (!(bitmap == IntPtr.Zero))
                    {
                        bitmapOld = WinAPI.SelectObject(Memdc, bitmap);
                        IntPtr hFont = font.ToHfont();
                        logfnotOld = WinAPI.SelectObject(Memdc, hFont);
                        try
                        {
                            System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);

                            DTTOPTS dttOpts = new DTTOPTS();

                            dttOpts.dwSize = (uint)Marshal.SizeOf(typeof(DTTOPTS));

                            dttOpts.dwFlags = Constants.DTT_COMPOSITED | Constants.DTT_GLOWSIZE;


                            dttOpts.iGlowSize = iglowSize;

                            for (int i = (-(dib.bmiHeader.biWidth * dib.bmiHeader.biHeight) - 1); i >= 0; i--)
                            {
                                Marshal.WriteInt32(
                                    (IntPtr)((long)ptPixels + Marshal.SizeOf(typeof(int)) * i),
                                    GetAeroBackgroundColor().ToArgb()
                                    );
                            }

                            WinAPI.DrawThemeTextEx(renderer.Handle, Memdc, 1, 1, text, -1, uFormat, ref rc2, ref dttOpts);

                            WinAPI.BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, 0, 0, Constants.SRCCOPY);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Trace.WriteLine(e.Message);
                        }


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

                        WinAPI.ReleaseDC(Memdc, -1);
                        WinAPI.DeleteDC(Memdc);
                    }
                }
            }
        }