Synthetic Ascension
Synthetic Ascension

Reputation: 319

C# (WinForms) - Find Colour of Cursor

My Aim:

My Difficulty:

My Current Code:

    public static Color RetrieveColour(Point Coordinates)
    {
        //Use this 'image' to create a Graphics class.
        using (Graphics Destination = Graphics.FromImage(ScreenPixel))
        {
             //Creates a graphic from the specified handler to a window.
             //In this case, it uses a handler which has been initialised to zero.
             using (Graphics Source = Graphics.FromHwnd(IntPtr.Zero))
             {
                 //Handler from the source device context.
                 IntPtr HandlerSourceDC = Source.GetHdc();
                 //Handler to the destination device context.
                 IntPtr HandlerDC = Destination.GetHdc();
                 //BitBlt is responsible for doing the copying.
                 //Returns a non-zero value for upon success.
                 int retval = BitBlt(HandlerDC, 0, 0, 1, 1, HandlerSourceDC, Coordinates.X, Coordinates.Y, (int)CopyPixelOperation.SourceCopy);
                 //Release the handlers.
                 Destination.ReleaseHdc();
                 Source.ReleaseHdc();
             }
        }
        //Retrieve the colour of the pixel at the specified coordinates.
        return ScreenPixel.GetPixel(0, 0);
    }

I should probably add, it should work with non-Windows cursors. By this I mean custom cursors from other applications (if that has any bearing on future answers).

Upvotes: 1

Views: 623

Answers (1)

user4954523
user4954523

Reputation:

As far as I am currently aware, the only way that you'd be able to do this is if you were to create a custom driver that can act as an intermediary between the user.

I haven't read much of it, but you might want to look into this project http://www.codeproject.com/KB/cs/DesktopCaptureWithMouse.aspx?display=Print

Previously posted here: C# - Capturing the Mouse cursor image

Upvotes: 1

Related Questions