Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Custom Cursors from .RES

Status
Not open for further replies.

onrdbandit

Programmer
Mar 12, 2003
145
US
Howdy,

I am trying to give my app cursors like Adobe Acrobat Reader. (i.e. Open hand that closes on click) I found the help files of little use, because the code I copied directly from them did not work. There is little explaination there as well. If anyone could post a few tips, I would be very thankful.....
 
Maybe the best way to illustrate how to do this is with an example:

I have a compiled resorce file new.res that contains a cursor namned NEWCURSOR. I've added this .res file to my project.

My main form is called Form1 & my source is Unit1.cpp (original, I know). In my Unit1.h I declare a new cursor constant:

Code:
private:	// User declarations
   const TCursor crNewCursor;

Now, I need to assign the cursor to the global Screen Cursor array:

I do this in Unit1.cpp in Form1's constructor like this:
Code:
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner),
        crNewCursor(TCursor(1))

{
}

The array index (in this instance 1) is a positive index into the cursor array. The system cursors use -22 to 0 (I believe), so all positive indexes are available to us.

Now, I can load & use the cursor like this:

Code:
   Screen->Cursors[crNewCursor]  = LoadCursor(HInstance,"NEWCURSOR");
   Screen->Cursor = crNewCursor;

Hope this helps.
 
Thanks for the help, BUT
I still cant get it to work....

The LoadCursor function does not return anything.
Code:
Screen->Cursors[crNewCursor] = LoadCursor(HInstance,"HAND");
Screen->Cursor = crNewCursor;

In my Image Editor tree view I see:(Project1.res)

-Contents
|
-Images
| |
| -MAINICON
|
-Cursors
|
-HAND


Why would the program not load the cursor?
NOTE: I modified the MAINICON image, but no changes are reflected in the application even after deleting all .obj files and project rebuild.

Thanks
onrdbandit
 
Hmmm....

I don't have time right now to dig much deeper, but just off the top of my head, have you tried saving the cursor to a cursor file & then loading it from the cursor file instead of from the .res?

Code:
Screen->Cursors[crNewCursor] = LoadCursorFromFile("filename");

LoadCursor() should return either the cursor handle or NULL if it fails. It's a Win32API call so you can call GetLastError() & FormatMessage() to find out what the error is.

I haven't had any trouble with using custom cursors, but I haven't used any animated cursors either, maybe something to do with it being an animated cursor?

Maybe someone else knows? If no luck I can play with it more tomorrow.

Good Luck.
 
Thanks a bunch....

The LoadCursorFromFile function was exactly what I was looking for...

Thanks again
onrdbandit
 
Howdy,

I have been working on the cursor issue for a while now, and I have hit another brick wall....

This is how I load the cursors:
Code:
HCURSOR hand;
 HCURSOR grab;

 hand = LoadCursorFromFile("handflat.cur");
 grab = LoadCursorFromFile("handgrab.cur");

 if (hand == NULL)
 ShowMessage("Hand Cursor not loaded.");
 else
 {
  Screen->Cursors[1] = hand;
 }

 if (grab == NULL)
 ShowMessage("Grab Cursor not loaded.");
 else
 {
  Screen->Cursors[2] = grab;
 }

this is a minor adaptation of some code I found on the borland website.

I can sucessfully load each cursor, and can set them almost like I want, but I cannont switch between the two in my OnMouseDown event. I set the exact same code somewhere else, and the cursor is set then. I have no trouble setting the cursor in the OnMouseUp event. I cannot understand what exactly is going on...
 
Since you are able to set the cursor elsewhere, I believe that the problem is that the cursor will not change until another event is fired. So, if you are setting the cursor in your MouseDown event for a particular component (like a button) like:

Code:
  Button1->Cursor = crNew;

and then setting it again on your MouseUp event:

Code:
  Button1->Cursor = crOld;

the cursor change won't show until after the MouseUp event when the button is re-drawn in the up position, which means you'll never see the change because the cursor is set back to the old cursor.

If this seems to be what's happening, try setting the Screen cursor instead and see if that works:

in MouseDown event:

Code:
  Screen->Cursor = crNew;

then in MouseUp event:

Code:
  Screen->Cursor = crDefault;

Hope this helps to isolate the problem. If not, maybe post some additional code showing your MouseDown & MouseUp events.

Good Luck.
 
Hi guys,

I have seen a while a go, that it might be useful to fire
Application->ProcessMessages();
sometimes, when it comes to changing a cursor.

btw.
when I edited an avi into my .res file (with M$VC, sorry did not find any other tool to do so), I had to recompile the myproject.cpp (complete project) file, which is normally not done even when all objects are deleted, in order to have them shown and accessible in the sourcecode.

regards,

Michael


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top