Click here to Skip to main content
15,350,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Hopefully the solution is very easy, but I've been searching online for the past few days for how to use/reference a custom cursor in an application without it being an external file (That is, while it's a resource of the application, itself), but I keep coming up empty.

There are most likely several things I'm over looking while attempting to do this as I'm still rather new to C++, so even a push in the right direction would be greatly appreciated.

The following code is how I've manged to do this so far, but the cursor is external :(

XML
//Included extra as I do plan to build on this more.

#include <stdio.h>
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>

main()
{

// Change System Cursor.
    HCURSOR hcur;
    hcur = ::LoadCursorFromFile("test.cur");
    ::SetSystemCursor(hcur,OCR_NORMAL);

// Wait 10 seconds

Sleep(10000);
}



After I modified the code (as below), I've been adding the resource file to the compiled exe
The version information adds correctly, and when viewing the exe's resources, the cursor has been added, but nothing I have tried so far has been able to use the cursor when it's included.

XML
//Included extra as I do plan to build on this more.

#include <stdio.h>
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>

int main()
{

//  Change System Cursor.

   HINSTANCE hInst;            // handle to current instance
   hInst = GetModuleHandle(NULL);

   HCURSOR hCurs;     // cursor handles
   hCurs = LoadCursor(hInst, MAKEINTRESOURCE(2));

/* 2 is the number I've set for the cursor in the .rc file
   ------------------------------
   1 ICON "icon1.ico"
   2 CURSOR "test.cur"
   ------------------------------
*/


    ::SetSystemCursor(hCurs,OCR_NORMAL);

//Wait for 10 seconds.

Sleep(10000);
}


I've tried #include 'ing the .rc file instead of adding it later, as well as replacing 2 CURSOR with MAINCUR CURSOR and then #define 'ing MAINCUR in the application and various other things that I can't remember off the top of my head.

I'm using Code::Blocks on Windows XP 32bit.

Much thanks in advance to anyone who can offer hints, help or a solution,
Gremz.
Posted
Updated 11-Feb-22 15:17pm

You should check the result of the LoadCursor() call, and if it does not return a handle use GetLastError()[^] to discover why.
   
Comments
Gremz 7-Jul-11 5:58am
   
Hey Richard,
thank you for the fast reply.

After hCurs = LoadCursor(hInst, MAKEINTRESOURCE(2));

I added the following to see if hCurs was being populated.

if(hCurs==NULL){
printf("hCurs is NULL");
}
else{
printf("hCurs is Populated!");
}



I'm not sure why or even how, but the code is now working correctly (with & without the addition).

I honestly have no idea why it wasn't working prior, but I'd like to thank you for reminding me to check the little things :)

Kind regards,
Gremz
Richard MacCutchan 7-Jul-11 8:18am
   
Interesting; perhaps some part of your project needed to be recompiled ...
Mārtiņš Andžäns 11-Feb-22 13:00pm
   
To set cursor you simple need set cursor in WM_SETCURSOR event in Windows Procedure.
To set cursor you simple need set cursor in WM_SETCURSOR event in Windows Procedure.


C++
LRESULT WindowProcedure(HWND hMainWindow, UINT msg, WPARAM wParam, LPARAM lParam) {

case WM_SETCURSOR:

{
    HCURSOR hCursor = LoadCursorFromFile(L"Cursor.cur");
    SetCursor(hCursor);
}

}
   
v4
Comments
Tony Hill 12-Feb-22 5:46am
   
The OP was not trying to load a cursor from a file but from an embedded resource.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900