Ejemplo n.º 1
0
        private static void DrawTextOnGlass(IntPtr hDC, string text, Font font, Rectangle rct, int iGlowSize, StringAlignment horizontalAlign)
        {
            RECT rect  = new RECT();
            RECT pRect = new RECT();

            rect.left    = rct.Left;
            rect.right   = rct.Right + (4 * iGlowSize);
            rect.top     = rct.Top;
            rect.bottom  = rct.Bottom + (2 * iGlowSize);
            pRect.left   = 2 * iGlowSize;
            pRect.top    = 2;
            pRect.right  = rect.Width - (3 * iGlowSize);
            pRect.bottom = rect.Height - 2;
            TextFormatFlags dwFlags = TextFormatFlags.ModifyString | TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine;

            switch (horizontalAlign)
            {
            case StringAlignment.Near:
                dwFlags |= TextFormatFlags.EndEllipsis;
                break;

            case StringAlignment.Center:
                dwFlags |= TextFormatFlags.PathEllipsis | TextFormatFlags.HorizontalCenter;
                break;

            case StringAlignment.Far:
                dwFlags |= TextFormatFlags.Right;
                break;
            }
            IntPtr ptr = PInvoke.CreateCompatibleDC(hDC);

            if (ptr != IntPtr.Zero)
            {
                IntPtr     ptr5;
                BITMAPINFO pbmi = new BITMAPINFO();
                pbmi.bmiHeader.biSize        = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
                pbmi.bmiHeader.biWidth       = rect.Width;
                pbmi.bmiHeader.biHeight      = -rect.Height;
                pbmi.bmiHeader.biPlanes      = 1;
                pbmi.bmiHeader.biBitCount    = 0x20;
                pbmi.bmiHeader.biCompression = 0;
                IntPtr hgdiobj = PInvoke.CreateDIBSection(ptr, ref pbmi, 0, out ptr5, IntPtr.Zero, 0);
                if (hgdiobj != IntPtr.Zero)
                {
                    IntPtr ptr3 = PInvoke.SelectObject(ptr, hgdiobj);
                    IntPtr ptr6 = font.ToHfont();
                    IntPtr ptr4 = PInvoke.SelectObject(ptr, ptr6);
                    VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active);
                    DTTOPTS             pOptions = new DTTOPTS();
                    pOptions.dwSize    = Marshal.SizeOf(typeof(DTTOPTS));
                    pOptions.dwFlags   = 0x2800;
                    pOptions.iGlowSize = iGlowSize;
                    PInvoke.DrawThemeTextEx(renderer.Handle, ptr, 0, 0, text, -1, dwFlags, ref pRect, ref pOptions);
                    PInvoke.BitBlt(hDC, rect.left, rect.top, rect.Width, rect.Height, ptr, 0, 0, 0xcc0020);
                    PInvoke.SelectObject(ptr, ptr3);
                    PInvoke.SelectObject(ptr, ptr4);
                    PInvoke.DeleteObject(hgdiobj);
                    PInvoke.DeleteObject(ptr6);
                }
                PInvoke.DeleteDC(ptr);
            }
        }
Ejemplo n.º 2
0
        private unsafe Bitmap GetCaptureBitmap(HWND hWnd)
        {
            Bitmap bitmap    = null;
            HWND   zero      = new HWND(0);
            HDC    desktopDC = new HDC(IntPtr.Zero);
            HDC    memoryDC  = new HDC(IntPtr.Zero);

            try
            {
                var result = PInvoke.GetClientRect(hWnd, out RECT rect);

                if (!result)
                {
                    throw new NullReferenceException($"指定したハンドルのウィンドウ({hWnd})を発見することができませんでした。");
                }

                POINT point     = new POINT();
                var   mapResult = PInvoke.MapWindowPoints(hWnd, zero, &point, 2);

                if (mapResult == 0)
                {
                    throw new NullReferenceException($"指定したハンドルのウィンドウ({hWnd})の座標空間の変換に失敗しました。");
                }

                rect.left   = point.x;
                rect.top    = point.y;
                rect.right  = rect.right + point.x;
                rect.bottom = rect.bottom + point.y;

                var tempRect = rect;

                desktopDC = PInvoke.GetWindowDC(zero); // デスクトップの HDC を取得

                var header = new BITMAPINFOHEADER()
                {
                    biSize        = (uint)Marshal.SizeOf(typeof(BITMAPINFOHEADER)),
                    biWidth       = tempRect.right - rect.left,
                    biHeight      = tempRect.bottom - rect.top,
                    biPlanes      = 1,
                    biCompression = 0, // BitmapCompressionMode.BI_RGB = 0
                    biBitCount    = 24,
                };

                var info = new BITMAPINFO
                {
                    bmiHeader = header,
                };

                void **bits = null;

                HBITMAP hBitmap = PInvoke.CreateDIBSection(desktopDC, &info, DIB_USAGE.DIB_RGB_COLORS, bits, new HANDLE(IntPtr.Zero), 0);

                memoryDC = PInvoke.CreateCompatibleDC(desktopDC);

                var phBitMap = PInvoke.SelectObject(memoryDC, new HGDIOBJ(hBitmap));

                PInvoke.BitBlt(memoryDC, 0, 0, header.biWidth, header.biHeight, desktopDC, rect.left, rect.top, ROP_CODE.SRCCOPY);
                PInvoke.SelectObject(memoryDC, phBitMap);

                bitmap = Bitmap.FromHbitmap(hBitmap, IntPtr.Zero);
            }
            finally
            {
                if (desktopDC.Value != IntPtr.Zero)
                {
                    PInvoke.ReleaseDC(zero, desktopDC);
                }

                if (memoryDC.Value != IntPtr.Zero)
                {
                    PInvoke.ReleaseDC(hWnd, memoryDC);
                }
            }

            return(bitmap);
        }