Esempio n. 1
0
        public static Bitmap CaptureCursor(ref int x, ref int y)
        {
            Bitmap bmp;
            IntPtr hicon;

            Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
            Win32Stuff.ICONINFO   icInfo;
            ci.cbSize = Marshal.SizeOf(ci);
            if (Win32Stuff.GetCursorInfo(out ci))
            {
                if (ci.flags == Win32Stuff.CURSOR_SHOWING)
                {
                    hicon = Win32Stuff.CopyIcon(ci.hCursor);
                    if (Win32Stuff.GetIconInfo(hicon, out icInfo))
                    {
                        x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
                        y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);

                        Icon ic = Icon.FromHandle(hicon);
                        bmp = ic.ToBitmap();
                        return(bmp);
                    }
                }
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// The capture cursor.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="Bitmap"/>.
        /// </returns>
        public static Bitmap CaptureCursor(ref int x, ref int y)
        {
            Win32Stuff.CURSORINFO cursorInfo = new Win32Stuff.CURSORINFO();
            cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
            if (!Win32Stuff.GetCursorInfo(out cursorInfo))
            {
                return(null);
            }

            if (cursorInfo.flags != Win32Stuff.CURSOR_SHOWING)
            {
                return(null);
            }

            IntPtr hicon = Win32Stuff.CopyIcon(cursorInfo.hCursor);

            if (hicon == IntPtr.Zero)
            {
                return(null);
            }

            Win32Stuff.ICONINFO iconInfo;
            if (!Win32Stuff.GetIconInfo(hicon, out iconInfo))
            {
                return(null);
            }

            x = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot);
            y = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot);

            using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask))
            {
                // Is this a monochrome cursor?
                if (maskBitmap.Height == maskBitmap.Width * 2)
                {
                    Bitmap resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width);

                    using (Graphics desktopGraphics = Graphics.FromHwnd(Win32Stuff.GetDesktopWindow()))
                    {
                        IntPtr desktopHdc = desktopGraphics.GetHdc();

                        IntPtr maskHdc = GDIStuff.CreateCompatibleDC(desktopHdc);
                        IntPtr oldPtr  = GDIStuff.SelectObject(maskHdc, maskBitmap.GetHbitmap());

                        using (Graphics resultGraphics = Graphics.FromImage(resultBitmap))
                        {
                            IntPtr resultHdc = resultGraphics.GetHdc();

                            // These two operation will result in a black cursor over a white background.
                            // Later in the code, a call to MakeTransparent() will get rid of the white background.
                            GDIStuff.BitBlt(
                                resultHdc,
                                0,
                                0,
                                32,
                                32,
                                maskHdc,
                                0,
                                32,
                                (int)GDIStuff.TernaryRasterOperations.SRCCOPY);
                            GDIStuff.BitBlt(
                                resultHdc,
                                0,
                                0,
                                32,
                                32,
                                maskHdc,
                                0,
                                0,
                                (int)GDIStuff.TernaryRasterOperations.SRCINVERT);

                            resultGraphics.ReleaseHdc(resultHdc);
                            GDIStuff.DeleteDC(resultHdc);
                            GDIStuff.DeleteObject(resultHdc);
                        }

                        IntPtr newPtr = GDIStuff.SelectObject(maskHdc, oldPtr);
                        GDIStuff.DeleteObject(oldPtr);
                        GDIStuff.DeleteObject(newPtr);
                        GDIStuff.DeleteDC(maskHdc);
                        desktopGraphics.ReleaseHdc(desktopHdc);
                        GDIStuff.DeleteDC(desktopHdc);
                    }

                    // Remove the white background from the BitBlt calls,
                    // resulting in a black cursor over a transparent background.
                    resultBitmap.MakeTransparent(Color.White);
                    return(resultBitmap);
                }
            }

            //// Delete the mask, if present.
            // if (iconInfo.hbmMask != IntPtr.Zero)
            // {
            // DeleteObject(iconInfo.hbmMask);
            // }

            //// Delete the color bitmap, if present.
            // if (iconInfo.hbmColor != IntPtr.Zero)
            // {
            // DeleteObject(iconInfo.hbmColor);
            // }
            using (Icon icon = Icon.FromHandle(hicon))
            {
                return(icon.ToBitmap());
            }
        }