Custom Cursor not working?

This is supposed to be a simple issue but.. I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include "resource.h"

HINSTANCE hInst;
HCURSOR hCurs2 = LoadCursor(hInst, MAKEINTRESOURCE(IDI_CURSOR1));

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
        {
            SetClassLong(hwndDlg, GCL_HCURSOR,(LONG) hCurs2);
            return TRUE;
        }

        case WM_CLOSE:
        {
            EndDialog(hwndDlg, 0);
            return TRUE;
        }

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {

            }
        break;
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc);
}


A simple code to learn how to use custom cursors for my application. The problem is: it doesn't work. And after quite some time of browsing through different forums and documentation, I cannot figure out why. What strikes me as weird though is that if I try to use a windows premade cursor, i.e:

HCURSOR hCurs2 = LoadCursor(hInst, IDC_HELP);

... it works just fine!.. but when I tried to load it from my resource file, it just shows the "loading" cursor for a while then goes back to the default arrow. So I thought it might be because my .cur file is broken, but I tried various ones (both created by me and downloaded from the internet) without any results. Any idea why?

Here's the resource (.rc) file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"




//
// Dialog resources
//
LANGUAGE 0, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 359, 337
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
    PUSHBUTTON      "Cancel", IDCANCEL, 129, 24, 50, 14, 0, WS_EX_LEFT
    DEFPUSHBUTTON   "OK", IDOK, 129, 7, 50, 14, 0, WS_EX_LEFT
}



//
// Cursor resources
//
LANGUAGE 0, SUBLANG_NEUTRAL
IDI_CURSOR1        CURSOR         "Move.cur"


... and here's the header file:

1
2
3
4
5
6
7
8
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDD_DIALOG1                             100
#define IDI_CURSOR1                             40002
#define IDOK                                    40000
#define IDCANCEL                                40001 


Thanks in advance for the responses :)
try moving:
HCURSOR hCurs2 = LoadCursor(hInst, MAKEINTRESOURCE(IDI_CURSOR1));
to the beginning of WinMain and delete:
HINSTANCE hInst;
This didn't help.

My code now looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include "resource.h"

HINSTANCE hInst;
HCURSOR hCurs2;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
        {
            SetClassLong(hwndDlg, GCL_HCURSOR,(LONG) hCurs2);
            return TRUE;
        }

        case WM_CLOSE:
        {
            EndDialog(hwndDlg, 0);
            return TRUE;
        }

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {

            }
        break;
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    //hInst = hInstance;
    HCURSOR hCurs2 = LoadCursor(hInst, MAKEINTRESOURCE(IDI_CURSOR1));
    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc);
}


and I still have the same problem.. bump?
I'm hardly a WinAPI expert, but it looks like you are loading a cursor but never actually doing anything with it. Have you tried calling SetCursour() with the handle you acquire from LoadCursor()?
try changing hInst (line 41) to hInstance.
Last edited on
I'm hardly a WinAPI expert, but it looks like you are loading a cursor but never actually doing anything with it. Have you tried calling SetCursour() with the handle you acquire from LoadCursor()?


I'm setting the cursor here:

SetClassLong(hwndDlg, GCL_HCURSOR,(LONG) hCurs2);

And I know this should work because, as mentioned previously, it works just fine with default (preset) cursors, like:

HCURSOR hCurs2 = LoadCursor(hInst, IDC_HELP);

Also

try changing hInst (line 41) to hInstance.


thanks for the response.. but that didn't help :(

I have tried even more cursors (both self-created and downloaded), thinking the files might be corrupt but no.. whatever .cur file I try, nothing works..

I'm thinking my computer could be at fault.. I'll try running the code on a different machine and see the results..
maybe try MAKEINTRESOURCE(40002) as param 2 of LoadCursor
From your first post:

1
2
HINSTANCE hInst;
HCURSOR hCurs2 = LoadCursor(hInst, MAKEINTRESOURCE(IDI_CURSOR1));

File scope, LoadCursor is called before hInst has been initialized in WinMain. Looks like you may have realized that in the last piece of code you posted, but..

1
2
3
4
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;
    HCURSOR hCurs2 = LoadCursor(hInst, MAKEINTRESOURCE(IDI_CURSOR1));

This is creating and assigning to a local variable of the same name, remove the type specifier to assign to the global declared at the top of your code. HCURSOR hCurs2 = ...
This is creating and assigning to a local variable of the same name, remove the type specifier to assign to the global declared at the top of your code. HCURSOR hCurs2 = ...


Argh, I feel so silly for not realizing this before :D

Thanks a lot, it works now :)
Topic archived. No new replies allowed.