Esempio n. 1
0
        public static Win32Bitmap Create(IntPtr hdc, int width, int height)
        {
            IntPtr bitmapPtr = IntPtr.Zero;
            IntPtr bitmapHdc = IntPtr.Zero;

            try
            {
                bitmapPtr = Gdi32API.CreateCompatibleBitmapChecked(hdc, width, height);
                bitmapHdc = Gdi32API.CreateCompatibleDCChecked(hdc);
                Gdi32API.SelectObjectChecked(bitmapHdc, bitmapPtr);

                var bitmap = new Win32Bitmap(bitmapPtr, bitmapHdc, width, height);
                bitmapPtr = IntPtr.Zero;
                bitmapHdc = IntPtr.Zero;
                return(bitmap);
            }
            finally
            {
                if (bitmapHdc != IntPtr.Zero)
                {
                    Gdi32API.DeleteDC(bitmapHdc);
                }

                if (bitmapPtr != IntPtr.Zero)
                {
                    Gdi32API.DeleteObject(bitmapPtr);
                }
            }
        }
Esempio n. 2
0
 internal Win32Canvas CreateCanvas(Win32Bitmap bitmap, Point offset)
 {
     return(bitmap.CreateCanvas(offset, objectCache));
 }
Esempio n. 3
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));
        }