void CleanupWindowCapture()
        {
            if (hdc != IntPtr.Zero)
            {
                Win32Funcs.ReleaseDC(hwnd, hdc);
                hdc = IntPtr.Zero;
            }

            if (hDest != IntPtr.Zero)
            {
                Win32Funcs.DeleteDC(hDest);
                hDest = IntPtr.Zero;
            }

            if (curRenderingBitmap != IntPtr.Zero)
            {
                Win32Funcs.DeleteObject(curRenderingBitmap);
                curRenderingBitmap = IntPtr.Zero;
            }

            if (cursorHandle != IntPtr.Zero)
            {
                Win32Funcs.DeleteObject(cursorHandle);
                cursorHandle = IntPtr.Zero;
            }
        }
예제 #2
0
        public static void UpdateCursorInfo()
        {
            // TODO - make sure everything here is cleaned up properly?
            // I'm pretty sure it is but it is good to check
            //int x = 0, y = 0;
            //return CaptureCursor (ref x, ref y);

            Win32Types.CursorInfo ci = new Win32Types.CursorInfo();
            ci.cbSize = Marshal.SizeOf(typeof(Win32Types.CursorInfo));

            if (!Win32Funcs.GetCursorInfo(ref ci))
            {
                return;
            }

            // Todo: this will change if cursor icon changes (via http://stackoverflow.com/questions/358527/how-to-tell-if-mouse-pointer-icon-changed?rq=1),
            // So then we can make more expensive and accurate cursor things and only update as needed

            IntPtr cursorPointer = ci.hCursor;

            cursorHandle = cursorPointer;

            int iconWidth  = Win32Funcs.GetSystemMetrics(Win32Consts.SystemMetric.SM_CXICON) + 1;
            int iconHeight = Win32Funcs.GetSystemMetrics(Win32Consts.SystemMetric.SM_CYICON) + 1;


            iconDims = new System.Drawing.Point(iconWidth, iconHeight);


            Win32Types.IconInfo hotSpotInfo = new Win32Types.IconInfo();
            Win32Funcs.GetIconInfo(cursorPointer, out hotSpotInfo);



            //Win32funcs.DrawIcon(hdcBitmap, 1, 1, cursorPointer);


            hotspot = new System.Drawing.Point(hotSpotInfo.xHotspot + 1, hotSpotInfo.yHotspot + 1);


            if (hotSpotInfo.hbmColor != IntPtr.Zero)
            {
                Win32Funcs.DeleteObject(hotSpotInfo.hbmColor);
            }
            if (hotSpotInfo.hbmMask != IntPtr.Zero)
            {
                Win32Funcs.DeleteObject(hotSpotInfo.hbmMask);
            }


            Win32Types.PointL cursorPos;
            Win32Funcs.GetCursorPos(out cursorPos);
            int cursorX = cursorPos.x - hotspot.X;
            int cursorY = cursorPos.y - hotspot.Y;

            cursorRect = new Win32Types.RECT(cursorX, cursorY, cursorX + iconDims.X, cursorY + iconDims.Y);
        }
        void FreeWinCapture()
        {
            if (hdc != IntPtr.Zero)
            {
                Win32Funcs.ReleaseDC(hwnd, hdc);
                hdc = IntPtr.Zero;
            }

            if (hDest != IntPtr.Zero)
            {
                Win32Funcs.DeleteDC(hDest);
                hDest = IntPtr.Zero;
            }

            if (curRenderingBitmap != IntPtr.Zero)
            {
                Win32Funcs.DeleteObject(curRenderingBitmap);
                curRenderingBitmap = IntPtr.Zero;
            }
        }
        public byte[] GetWindowBitmapUsingBitBlt(out int numBytesPerRow)
        {
            windowRect = new Win32Types.RECT();
            bool result;

            if (isDesktop)
            {
                Win32Types.MonitorInfo mi = new Win32Types.MonitorInfo();
                mi.cbSize = Marshal.SizeOf(mi);
                result    = Win32Funcs.GetMonitorInfo(hwnd, ref mi);
                if (result)
                {
                    windowRect = mi.rcMonitor;
                }
            }
            else
            {
                // Get the size of the window
                result = Win32Funcs.GetWindowRect(hwnd, out windowRect);
            }

            if (onlyCaptureMouse)
            {
                windowRect = cursorRect;
                result     = true;
            }

            if (!result)
            {
                // Failed getting rect
                numBytesPerRow = 0;

                return(null);
            }

            // If they resized the window we need to reinit the memory

            if (windowWidth != windowRect.Width || windowHeight != windowRect.Height)
            {
                CleanupWindowCapture();
                SetupWindowCapture();
                windowWidth  = windowRect.Width;
                windowHeight = windowRect.Height;
            }
            oldhDest = Win32Funcs.SelectObject(hDest, curRenderingBitmap);
            Win32Types.BITMAP bitmap = new Win32Types.BITMAP();
            Win32Funcs.GetObjectBitmap(curRenderingBitmap, Marshal.SizeOf(bitmap), ref bitmap);

            if (onlyCaptureMouse)
            {
                if (isDesktop)
                {
                    Win32Funcs.BitBlt(hDest, 0, 0, cursorRect.Width, cursorRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                else
                {
                    Win32Funcs.BitBlt(hDest, cursorRect.Left, cursorRect.Height, cursorRect.Width, cursorRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                Win32Funcs.DrawIconEx(hDest, 1, 1, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL);
            }
            else
            {
                // Copy from the screen device context to the bitmap device context
                var testing = false;
                if (isDesktop)
                {
                    Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                else
                {
                    if (testing)
                    {
                        Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY);
                    }
                    else
                    {
                        if (needsGDIp)
                        {
                            Bitmap   map      = new Bitmap(windowRect.Width, windowRect.Height);
                            Graphics graphics = Graphics.FromHdc(hDest);
                            try
                            {
                                graphics.CopyFromScreen(new System.Drawing.Point(windowRect.Left, windowRect.Top), System.Drawing.Point.Empty, windowRect.Size, CopyPixelOperation.SourceCopy);
                            }
                            finally
                            {
                                map.Dispose();
                                graphics.Dispose();
                            }
                        }
                    }
                }
                Win32Funcs.DrawIconEx(hDest, cursorRect.Left, cursorRect.Top, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL);
            }

            numBytesPerRow = bitmap.bmWidthBytes;

            if (bitmapBytes == null || bitmapBytes.Length != bitmap.bmHeight * bitmap.bmWidthBytes)
            {
                bitmapBytes = new byte[bitmap.bmHeight * bitmap.bmWidthBytes];
            }

            if (bitmap.bmBits != IntPtr.Zero)
            {
                Marshal.Copy(bitmap.bmBits, bitmapBytes, 0, bitmapBytes.Length);
            }

            if (bitmapBytes == null || bitmapBytes.Length == 0)
            {
                return(null);
            }
            Win32Funcs.DeleteObject(bitmap.bmBits);
            Win32Funcs.SelectObject(hDest, oldhDest);
            return(bitmapBytes);
        }