User32.dll cursor (CopyImage?) problem

Vai

Vai

Soldato
Joined
18 Oct 2002
Posts
3,347
I hope someone can help.
This code is almost working, but when executed the Cursor is a static image rather than an animated Cursor.

I believe the problem lies in CopyImage not copying the Cursor as an animated image, because when you comment out:
hCursor = CopyImage(hCursor, IMAGE_CURSOR, 0, 0, 0);
So that the original Cursor is used it works fine, however as the Cursor is not copied the WAIT cursor is overwritten by the ARROW cursor during RevertCursor().

The code is in C#, but I expect this is something which would show up for any language using User32.dll
Code:
internal static class MouseCursor
{
    [DllImport("User32.dll")]
    private static extern IntPtr LoadImage(IntPtr hInst, int lpszName, int uType, int cxDesired, int cyDesired, int fuLoad);
    [DllImport("User32.dll")]
    private static extern IntPtr CopyImage(IntPtr hImage, int uType, int cxDesired, int cyDesired, int fuFlags);
    [DllImport("User32.dll")]
    private static extern bool SetSystemCursor(IntPtr hCur, int id);

    private const int IDC_APPSTARTING = 32650;
    private const int IDC_WAIT = 32514;
    private const int IDC_ARROW = 32512;
    private const int IMAGE_CURSOR = 0x2;
    private const int LR_SHARED = 0x8000;

    private static IntPtr hOldCursor;

    internal static void SetCursor()
    {
        hOldCursor = LoadImage(new IntPtr(0), IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
        hOldCursor = CopyImage(hOldCursor, IMAGE_CURSOR, 0, 0, 0);
        IntPtr hCursor = LoadImage(new IntPtr(0), IDC_WAIT, IMAGE_CURSOR, 0, 0, LR_SHARED);
        hCursor = CopyImage(hCursor, IMAGE_CURSOR, 0, 0, 0);
        SetSystemCursor(hCursor, IDC_ARROW);
    }
    internal static void RevertCursor()
    {
        SetSystemCursor(hOldCursor, IDC_ARROW);
    }
}
 
Back
Top Bottom