Example #1
0
        private void WithTransparentCanvas(Rectangle rect, byte alpha, bool copySource, Action <Win32Canvas> action)
        {
            if (rect.Width <= 0 || rect.Height <= 0 || alpha == 0)
            {
                return;
            }

            IntPtr memoryHdc = Gdi32API.CreateCompatibleDCChecked(hdc);

            try
            {
                IntPtr memoryBitmap = Gdi32API.CreateCompatibleBitmapChecked(hdc, rect.Width, rect.Height);
                try
                {
                    var originalBitmap = Gdi32API.SelectObjectChecked(memoryHdc, memoryBitmap);
                    try
                    {
                        if (copySource)
                        {
                            Gdi32API.BitBlt(memoryHdc, 0, 0, rect.Width, rect.Height, hdc, rect.X, rect.Y, GDI32RasterOperation.SRCCOPY);
                        }

                        using (Win32Canvas memoryCanvas = new Win32Canvas(memoryHdc, offset, objectCache))
                        {
                            action(memoryCanvas);
                        }

                        Gdi32API.GdiAlphaBlend
                        (
                            hdc,
                            rect.X, rect.Y, rect.Width, rect.Height,
                            memoryHdc,
                            0, 0, rect.Width, rect.Height,
                            BLENDFUNCTION.ConstantAlpha(alpha)
                        );
                    }
                    finally
                    {
                        Gdi32API.SelectObjectChecked(memoryHdc, originalBitmap);
                    }
                }
                finally
                {
                    Gdi32API.DeleteObject(memoryBitmap);
                }
            }
            finally
            {
                Gdi32API.DeleteDC(memoryHdc);
            }
        }
Example #2
0
        private IntPtr WindowProc(IntPtr hwnd, Win32MessageType uMsg, IntPtr wParam, IntPtr lParam)
        {
            if (HandleWindowEvent(hwnd, uMsg, wParam, lParam))
            {
                return(IntPtr.Zero);
            }

            if (uMsg == Win32MessageType.WM_SETCURSOR)
            {
                uint lParam32 = (uint)lParam.ToInt64();
                var  hitTest  = (Win32HitTestResult)(lParam32 & 0xFFFF);
                if (hitTest == Win32HitTestResult.HTCLIENT)
                {
                    IntPtr cursor = Win32API.LoadImageW(
                        IntPtr.Zero,
                        Win32SystemResources.IDC_ARROW,
                        Win32ImageType.IMAGE_CURSOR,
                        0, 0,
                        Win32LoadImageFlags.LR_DEFAULTSIZE | Win32LoadImageFlags.LR_SHARED
                        );
                    if (cursor != IntPtr.Zero)
                    {
                        Win32API.SetCursor(cursor);
                        return(IntPtr.Zero);
                    }
                }
            }

            if (uMsg == Win32MessageType.WM_DESTROY)
            {
                windows.Remove(hwnd);
                Win32API.PostQuitMessage(0);
                return(IntPtr.Zero);
            }

            if (uMsg == Win32MessageType.WM_MOUSEMOVE)
            {
                if (windows.TryGetValue(hwnd, out var window))
                {
                    uint wParam32 = (uint)wParam.ToInt64();
                    uint lParam32 = (uint)lParam.ToInt64();
                    int  x        = (short)(lParam32 & 0xFFFF);
                    int  y        = (short)((lParam32 >> 16) & 0xFFFF);
                    window.StartupInfo.OnMouseMove(new Point(x, y));
                }

                return(IntPtr.Zero);
            }

            if (uMsg == Win32MessageType.WM_CHAR)
            {
                if (windows.TryGetValue(hwnd, out var window))
                {
                    char c = (char)wParam.ToInt64();

                    if (!char.IsControl(c))
                    {
                        window.StartupInfo.OnTextInput(c.ToString());
                    }
                }

                return(IntPtr.Zero);
            }

            if (uMsg == Win32MessageType.WM_PAINT)
            {
                PAINTSTRUCT ps  = new PAINTSTRUCT();
                IntPtr      hdc = Win32API.BeginPaint(hwnd, ref ps);
                try
                {
                    if (windows.TryGetValue(hwnd, out var window))
                    {
                        Rectangle area = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.Width, ps.rcPaint.Height);
                        using (Win32Bitmap bitmap = Win32Bitmap.Create(hdc, area.Width, area.Height))
                        {
                            using (Win32Canvas canvas = graphics.CreateCanvas(bitmap, new Point(-area.X, -area.Y)))
                            {
                                window.StartupInfo.OnPaint(canvas, area);
                            }

                            bitmap.CopyTo(hdc, area.X, area.Y);
                        }
                    }
                }
                finally
                {
                    Win32API.EndPaint(hwnd, ref ps);
                }

                return(IntPtr.Zero);
            }

            if (uMsg == Win32MessageType.WM_SIZE)
            {
                if (windows.TryGetValue(hwnd, out var window))
                {
                    ulong lParam32 = (uint)lParam.ToInt64();
                    int   width    = (short)(lParam32 & 0xFFFF);
                    int   height   = (short)((lParam32 >> 16) & 0xFFFF);
                    window.StartupInfo.OnResize(new Size(width, height));
                }

                return(IntPtr.Zero);
            }

            return(Win32API.DefWindowProcW(hwnd, uMsg, wParam, lParam));
        }