コード例 #1
0
ファイル: CursorData.cs プロジェクト: Edison6351/ShareX
        public void UpdateCursorData()
        {
            CursorInfo cursorInfo = new CursorInfo();
            cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);

            if (NativeMethods.GetCursorInfo(out cursorInfo))
            {
                IsVisible = cursorInfo.flags == NativeMethods.CURSOR_SHOWING;

                if (IsVisible)
                {
                    IconHandle = NativeMethods.CopyIcon(cursorInfo.hCursor);
                    IconInfo iconInfo;

                    if (NativeMethods.GetIconInfo(IconHandle, out iconInfo))
                    {
                        Point cursorPosition = CaptureHelpers.GetZeroBasedMousePosition();
                        Position = new Point(cursorPosition.X - iconInfo.xHotspot, cursorPosition.Y - iconInfo.yHotspot);

                        if (iconInfo.hbmMask != IntPtr.Zero)
                        {
                            NativeMethods.DeleteObject(iconInfo.hbmMask);
                        }

                        if (iconInfo.hbmColor != IntPtr.Zero)
                        {
                            NativeMethods.DeleteObject(iconInfo.hbmColor);
                        }
                    }
                }
            }
        }
コード例 #2
0
 public static extern bool GetCursorInfo(out CursorInfo pci);
コード例 #3
0
 public static extern bool GetCursorInfo(out CursorInfo pci);
コード例 #4
0
        public static MyCursor CaptureCursor()
        {
            CursorInfo cursorInfo = new CursorInfo();
            cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);

            if (GetCursorInfo(out cursorInfo) && cursorInfo.flags == CURSOR_SHOWING)
            {
                cursorInfo.ptScreenPos = CaptureHelpers.GetZeroBasedMousePosition();

                IntPtr hicon = CopyIcon(cursorInfo.hCursor);
                if (hicon != IntPtr.Zero)
                {
                    IconInfo iconInfo;
                    if (GetIconInfo(hicon, out iconInfo))
                    {
                        Point position = new Point(cursorInfo.ptScreenPos.X - iconInfo.xHotspot, cursorInfo.ptScreenPos.Y - iconInfo.yHotspot);

                        using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask))
                        {
                            Bitmap resultBitmap = null;

                            if (IsCursorMonochrome(maskBitmap))
                            {
                                resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width);

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

                                IntPtr maskHdc = NativeMethods.CreateCompatibleDC(desktopHdc);
                                IntPtr oldPtr = NativeMethods.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.
                                    NativeMethods.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, CopyPixelOperation.SourceCopy);
                                    NativeMethods.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, CopyPixelOperation.SourceInvert);

                                    resultGraphics.ReleaseHdc(resultHdc);
                                }

                                IntPtr newPtr = NativeMethods.SelectObject(maskHdc, oldPtr);
                                NativeMethods.DeleteDC(newPtr);
                                NativeMethods.DeleteDC(maskHdc);
                                desktopGraphics.ReleaseHdc(desktopHdc);

                                // Remove the white background from the BitBlt calls,
                                // resulting in a black cursor over a transparent background.
                                resultBitmap.MakeTransparent(Color.White);
                            }
                            else
                            {
                                resultBitmap = Icon.FromHandle(hicon).ToBitmap();
                            }

                            return new MyCursor(new Cursor(cursorInfo.hCursor), position, resultBitmap);
                        }
                    }
                }
            }

            return new MyCursor();
        }