Esempio n. 1
0
        /*************************************************************************
        * method for retrieving the current bitmap of the global cursor
        **************************************************************************/
        public static cursorInTime CaptureCursor()
        {
            Bitmap bmp;
            IntPtr hicon;
            Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
            Win32Stuff.ICONINFO icInfo;
            ci.cbSize = Marshal.SizeOf(ci);
            try
            {
                if (Win32Stuff.GetCursorInfo(out ci))
                {
                    if (ci.flags == Win32Stuff.CURSOR_SHOWING)
                    {
                        hicon = Win32Stuff.CopyIcon(ci.hCursor);
                        Win32Stuff.GetIconInfo(hicon, out icInfo);
                        while (icInfo.hbmMask == IntPtr.Zero)
                        {
                            Win32Stuff.GetIconInfo(hicon, out icInfo);
                            System.Diagnostics.Debug.Print("Retrying cursor capture.");
                        }
                        int x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
                        int y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);
                        using (Bitmap maskBitmap = Bitmap.FromHbitmap(icInfo.hbmMask))
                        {
                            // Is this a monochrome cursor?
                            // This portion taken from http://stackoverflow.com/questions/918990/c-sharp-capturing-the-mouse-cursor-image
                            if (maskBitmap.Height == maskBitmap.Width * 2)
                            {
                                // Reverting to simple saving and no cleanup because of an object leak
                                Rectangle sourceRectangle = new Rectangle(0, 0, maskBitmap.Width, maskBitmap.Height / 2);

                                Bitmap secondBitmap = maskBitmap.Clone(sourceRectangle, PixelFormat.DontCare);

                                bmp = secondBitmap;
                            }
                            else
                            {
                                Icon ic = Icon.FromHandle(hicon);
                                bmp = ic.ToBitmap();
                                Win32Stuff.DestroyIcon(ic.Handle);
                            }
                        }
                        Win32Stuff.DeleteObject(hicon);
                        Win32Stuff.DeleteObject(icInfo.hbmColor);
                        Win32Stuff.DeleteObject(icInfo.hbmMask);
                        Win32Stuff.DeleteObject(ci.hCursor);
                        return new cursorInTime(x, y, bmp);
                    }
                }
            }
            catch (ExternalException e)
            {
                System.Diagnostics.Debug.Print(e.ToString());
            }
            return null;
        }
Esempio n. 2
0
        // Method for retrieving the current bitmap of the global cursor
        public static cursorInTime CaptureCursor()
        {
            Bitmap bmp;

            Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
            Win32Stuff.ICONINFO   icInfo;
            ci.cbSize = Marshal.SizeOf(ci);
            try
            {
                if (Win32Stuff.GetCursorInfo(out ci))
                {
                    if (ci.flags == Win32Stuff.CURSOR_SHOWING)
                    {
                        Win32Stuff.GetIconInfo(ci.hCursor, out icInfo);

                        // If fetch failed, something wrong with current cursor, give up
                        if (icInfo.hbmMask == IntPtr.Zero)
                        {
                            Win32Stuff.DeleteObject(icInfo.hbmColor);
                            Win32Stuff.DeleteObject(icInfo.hbmMask);
                            Win32Stuff.DeleteObject(ci.hCursor);
                            return(null);
                        }

                        int x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
                        int y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);

                        using (Bitmap maskBitmap = Bitmap.FromHbitmap(icInfo.hbmMask))
                        {
                            // Is this a monochrome cursor?
                            if (maskBitmap.Height == maskBitmap.Width * 2)
                            {
                                bmp = maskBitmap.Clone(new Rectangle(0, 0, maskBitmap.Width, maskBitmap.Height / 2), PixelFormat.DontCare);
                            }
                            else
                            {
                                Icon ic = Icon.FromHandle(ci.hCursor);
                                bmp = ic.ToBitmap();
                                Win32Stuff.DestroyIcon(ic.Handle);
                                ic.Dispose();
                            }
                            maskBitmap.Dispose();
                        }
                        Win32Stuff.DeleteObject(icInfo.hbmColor);
                        Win32Stuff.DeleteObject(icInfo.hbmMask);
                        Win32Stuff.DestroyIcon(ci.hCursor);
                        return(new cursorInTime(x, y, bmp));
                    }
                }
            }
            // GDI+ Had a bad day getting the cursor, API says ignore and try again
            catch (ExternalException e)
            {
                System.Diagnostics.Debug.Print(e.ToString());
                using (StreamWriter w = File.AppendText("Cursor-Bug-External-Exception-Log.txt"))
                {
                    w.Write("\r\nLog Entry : ");
                    w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                                DateTime.Now.ToLongDateString());
                    w.WriteLine("  :");
                    w.WriteLine("  :{0}", e.ToString());
                    w.WriteLine("-------------------------------");
                }
            }
            //Invalid cursor information! The cursor is likely blank.
            catch (ArgumentException e)
            {
                System.Diagnostics.Debug.Print(e.ToString());
                using (StreamWriter w = File.AppendText("Cursor-Bug-Argument-Exception-Log.txt"))
                {
                    w.Write("\r\nLog Entry : ");
                    w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                                DateTime.Now.ToLongDateString());
                    w.WriteLine("  :");
                    w.WriteLine("  :{0}", e.ToString());
                    w.WriteLine("-------------------------------");
                }
            }
            return(null);
        }