Exemplo n.º 1
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);
                    }
                }
            }
        }
Exemplo n.º 2
0
 public static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
Exemplo n.º 3
0
        private void RefreshCtrl()
        {
            if (m_bIsRefreshing)
            {
                return;
            }

            int width  = host.ClientRectangle.Width;
            int height = host.ClientRectangle.Height;

            m_bIsRefreshing = true;


            IntPtr hDC = WinAPI.GetDC(host.Handle);

            if (hDC == IntPtr.Zero)
            {
                m_bIsRefreshing = false;
                Debug.Assert(false, "GetDC failed.");
                return;
            }

            IntPtr hdcMemory = WinAPI.CreateCompatibleDC(hDC);

            int        nBytesPerLine   = ((width * 32 + 31) & (~31)) >> 3;
            BITMAPINFO stBmpInfoHeader = new BITMAPINFO();

            stBmpInfoHeader.bmiHeader.biSize        = Marshal.SizeOf(stBmpInfoHeader);
            stBmpInfoHeader.bmiHeader.biWidth       = width;
            stBmpInfoHeader.bmiHeader.biHeight      = height;
            stBmpInfoHeader.bmiHeader.biPlanes      = 1;
            stBmpInfoHeader.bmiHeader.biBitCount    = 32;
            stBmpInfoHeader.bmiHeader.biCompression = 0;
            stBmpInfoHeader.bmiHeader.biClrUsed     = 0;
            stBmpInfoHeader.bmiHeader.biSizeImage   = nBytesPerLine * height;

            IntPtr pvBits  = IntPtr.Zero;
            IntPtr hbmpMem = WinAPI.CreateDIBSection(hDC
                                                     , ref stBmpInfoHeader
                                                     , Constants.DIB_RGB_COLORS
                                                     , out pvBits
                                                     , IntPtr.Zero
                                                     , 0
                                                     );

            Debug.Assert(hbmpMem != IntPtr.Zero, "CreateDIBSection failed.");

            if (hbmpMem != null)
            {
                IntPtr hbmpOld = WinAPI.SelectObject(hdcMemory, hbmpMem);

                Graphics graphic = Graphics.FromHdcInternal(hdcMemory);

                graphic.FillRectangle(new SolidBrush(BackColor), graphic.ClipBounds);

                foreach (Control ctrl in host.Controls)
                {
                    Bitmap    bmp  = new Bitmap(ctrl.Width, ctrl.Height);
                    Rectangle rect = new Rectangle(0, 0, ctrl.Width, ctrl.Height);
                    ctrl.DrawToBitmap(bmp, rect);
                    graphic.DrawImage(bmp, ctrl.Location);
                }


                POINT         ptSrc    = new POINT(0, 0);
                POINT         ptWinPos = new POINT(host.Left, host.Top);
                SIZE          szWin    = new SIZE(width, height);
                BLENDFUNCTION stBlend  = new BLENDFUNCTION(Constants.AC_SRC_OVER, 0, 0xFF, Constants.AC_SRC_ALPHA);

                WinAPI.UpdateLayeredWindow(host.Handle
                                           , hDC
                                           , ref ptWinPos
                                           , ref szWin
                                           , hdcMemory
                                           , ref ptSrc
                                           , 0
                                           , ref stBlend
                                           , Constants.ULW_ALPHA
                                           );

                graphic.Dispose();
                WinAPI.SelectObject(hbmpMem, hbmpOld);
                WinAPI.DeleteObject(hbmpMem);
            }

            WinAPI.DeleteDC(hdcMemory);
            WinAPI.DeleteDC(hDC);

            m_bIsRefreshing = false;
        }