Error loading custom cursor

This is a discussion on Error loading custom cursor within the Windows Programming forums, part of the Platform Specific Boards category; When I try to load a custom cursor into my program using this code Code: wndclass.hCursor = LoadCursor(NULL, IDC_CROSSHAIR); and ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Error loading custom cursor

    When I try to load a custom cursor into my program using this code
    Code:
      wndclass.hCursor        = LoadCursor(NULL, IDC_CROSSHAIR);
    and it gives me this error

    Code:
    error: invalid conversion from 'int' to 'const CHAR*'
    if it helps any, here is my resource.h file

    Code:
    //incon range: 1000 - 1999
    #define IDI_DUCKHUNT 1000
    #define IDI_DUCKHUNT_SM 1001
    
    //Bitmap range: 2000 - 2999
    #define IDB_DUCK 2000
    
    //Cursor range: 4000 - 4999
    #define IDC_CROSSHAIR 4000
    and here is my DuckHunt.rc file (I'm making a bad Duck Hunt clone)
    Code:
    #include "Resource.h"
    
    //Icon
    IDI_DUCKHUNT ICON "Res\\DuckHunt.ico"
    IDI_DUCKHUNT_SM ICON "Res\\DuckHunt_sm.ico"
    
    //Bitmap
    IDB_DUCK BITMAP "Res\\Duck.bmp"
    
    //Curspr
    IDC_CROSSHAIR CURSOR "Res\\CrossHair.cur"

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Well, the error pretty much tells you everything!
    Devoted my life to programming...

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by bijan311 View Post
    When I try to load a custom cursor into my program using this code
    Code:
      wndclass.hCursor        = LoadCursor(NULL, IDC_CROSSHAIR);
    and it gives me this error

    Code:
    error: invalid conversion from 'int' to 'const CHAR*'
    if it helps any, here is my resource.h file

    Code:
    //incon range: 1000 - 1999
    #define IDI_DUCKHUNT 1000
    #define IDI_DUCKHUNT_SM 1001
    
    //Bitmap range: 2000 - 2999
    #define IDB_DUCK 2000
    
    //Cursor range: 4000 - 4999
    #define IDC_CROSSHAIR 4000
    and here is my DuckHunt.rc file (I'm making a bad Duck Hunt clone)
    Code:
    #include "Resource.h"
    
    //Icon
    IDI_DUCKHUNT ICON "Res\\DuckHunt.ico"
    IDI_DUCKHUNT_SM ICON "Res\\DuckHunt_sm.ico"
    
    //Bitmap
    IDB_DUCK BITMAP "Res\\Duck.bmp"
    
    //Curspr
    IDC_CROSSHAIR CURSOR "Res\\CrossHair.cur"
    MAKEINTRESOURCE.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    So to get it to work I have to change IDC_CROSSHAIR to a const CHAR?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by bijan311 View Post
    So to get it to work I have to change IDC_CROSSHAIR to a const CHAR?
    Yeah, but through MAKEINTRESOURCE only!!
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Thanks, it woked

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    After finishing, the program, and after I compiled it, it just show the loading arrow, then it uses the normal arrow, what's wrong?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In your resource file name your cursor something like MYCURSOR ... not a number and lose the
    whole IDC_CROSSHAIR....

    In LoadCursor use the cursor's name

    LoadCursor (PgmInstance,"MYCURSOR");


    If you have the instance handle set to null it will try to load one of Window's stock cursors and error off without finding it. The instance handle you need is passed into your WindMain at startup...

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by CommonTater View Post
    In your resource file name your cursor something like MYCURSOR ... not a number and lose the
    whole IDC_CROSSHAIR....

    In LoadCursor use the cursor's name

    LoadCursor (PgmInstance,"MYCURSOR");


    If you have the instance handle set to null it will try to load one of Window's stock cursors and error off without finding it. The instance handle you need is passed into your WindMain at startup...
    Assuming that the OP has stored the icon in the resource section of the executable, MAKEINTRESOURCE should map the identifier to the icon. Textual cursor names open files.

    Quote Originally Posted by bijan311 View Post
    After finishing, the program, and after I compiled it, it just show the loading arrow, then it uses the normal arrow, what's wrong?
    What is the return value of LoadIcon? And how are you invoking it?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sebastiani View Post
    Assuming that the OP has stored the icon in the resource section of the executable, MAKEINTRESOURCE should map the identifier to the icon. Textual cursor names open files.
    Ummm... no....

    The numeric references most C programmers use in windows resources are simply names.

    For example if my .rc file says...
    Code:
    LANGUAGE LANG_ENGLISH,SUBLANG_ENGLISH_US
    
    AMAINICON ICON "RMBrowse.ico"
    I can load the icon with LoadIcon(Instance,"AMAINICON");

    The file "RMBrowse.ico" is the disk file and it's included in the resources at compile time.
    "AMAINICON" is the internal name of the resource.

    As long as the Instance handle is not NULL, windows looks for the string name in the resource for that Instance... When it is NULL it looks in it's own resource DLLs...

    Icons, Cursors, Bitmaps, Dialogs and Menus have names, not numbers.
    Strings are numbered.

    Even if you had...

    4101 CURSOR "MyCursor.cur"

    You could still load it with LoadCursor(Instance,"4101"); because it's a name not a number.

    Look at the prototype for MAKEINTRESOURCE...

    #define MAKEINTRESOURCEA(i) (LPSTR)((ULONG_PTR)((WORD)(i)))

    It just converts your numeric Identifier to a string...

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by CommonTater View Post
    Ummm... no....

    The numeric references most C programmers use in windows resources are simply names.

    For example if my .rc file says...
    Code:
    LANGUAGE LANG_ENGLISH,SUBLANG_ENGLISH_US
    
    AMAINICON ICON "RMBrowse.ico"
    I can load the icon with LoadIcon(Instance,"AMAINICON");

    The file "RMBrowse.ico" is the disk file and it's included in the resources at compile time.
    "AMAINICON" is the internal name of the resource.

    As long as the Instance handle is not NULL, windows looks for the string name in the resource for that Instance... When it is NULL it looks in it's own resource DLLs...

    Icons, Cursors, Bitmaps, Dialogs and Menus have names, not numbers.
    Strings are numbered.

    Even if you had...

    4101 CURSOR "MyCursor.cur"

    You could still load it with LoadCursor(Instance,"4101"); because it's a name not a number.

    Look at the prototype for MAKEINTRESOURCE...

    #define MAKEINTRESOURCEA(i) (LPSTR)((ULONG_PTR)((WORD)(i)))

    It just converts your numeric Identifier to a string...
    Ah, I wasn't aware of that usage. Either should work fine, then.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    You weren't aware of it because it's not right. You either have a numeric ID and use MAKEINTRESOURCE (or a string starting with a #) or you have a name and use a string. It's trivially provable that you can't use both, just run the following. If strings and IDs were interchangeable you'd expect the LoadCursors to fail or succeed in union, the fact one fails and one succeeds says they aren't.

    Code:
    #include <windows.h>
    #include <iostream>
    int main()
    {
        HMODULE hMod = LoadLibraryEx(TEXT("shell32.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE);
        HCURSOR hCur = LoadCursor(hMod, TEXT("1001"));
        if(!hCur)
        {
            DWORD err = GetLastError();
            std::cout << "Failed to load hCur with error " << err << '\n';
        }
        else
        {
            DestroyCursor(hCur);
            std::cout << "Loaded hCur successfully\n";
        }
        HCURSOR hCur2 = LoadCursor(hMod, MAKEINTRESOURCE(1001));
        if(!hCur2)
        {
            DWORD err = GetLastError();
            std::cout << "Failed to load hCur2 with error " << err << '\n';
        }
        else
        {
            DestroyCursor(hCur2);
            std::cout << "Loaded hCur2 successfully\n";
        }
        FreeLibrary(hMod);
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adeyblue View Post
    You weren't aware of it because it's not right. You either have a numeric ID and use MAKEINTRESOURCE (or a string starting with a #) or you have a name and use a string. It's trivially provable that you can't use both, just run the following. If strings and IDs were interchangeable you'd expect the LoadCursors to fail or succeed in union, the fact one fails and one succeeds says they aren't.
    Look at my example again...

    I used ... 4101 CURSOR "MyCursor.cur"

    If it was #4101 CURSOR "MyCursor.cur"

    you would be correct.

    I don't believe in all the time I've been programming windows I've ever used MAKEINTRESOURCE. I just give stuff names... it's so much easer.

    MAINMENU MENU ... LoadMenu(MyProg, "MAINMENU");

    SETTINGS DIALOG... DialogBox(...., "SETTINGS"....);

    It's almost too easy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Retrieving data from a file
    By kamitsuna in forum C++ Programming
    Replies: 45
    Last Post: 02-27-2010, 08:37 PM
  3. Custom Cursor
    By C-Compiler in forum Windows Programming
    Replies: 1
    Last Post: 12-21-2008, 09:24 PM
  4. Custom Animated Cursor
    By willc0de4food in forum Windows Programming
    Replies: 3
    Last Post: 05-13-2005, 10:05 PM
  5. Mouse in 800x600 24Bit Mode?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-11-2001, 12:38 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21