// JP opened flex table

Click to See Complete Forum and Search --> : LoadImage, SetCursor (.ani) problem


Estrayk
January 23rd, 2008, 05:51 PM
Hey, I want to ask how to load a .ani cursor from resources and display it on my dialog.
I can load a normal cursor (.cur) and display it without problems, however when I try with
the animated cursor it just disappears.

With this I can load a normal cursor

HCURSOR cur = LoadCursor(MyInstance, MAKEINTRESOURCE(IDR_CUR1));
SetClassLong(hwnd, GCL_HCURSOR, (LONG)cur);


Trying to do that with a .ani file just makes the cursor invisible.
I have tried to load it with LoadImage() instead, and that does not work either

HCURSOR cur = (HCURSOR)LoadImage(MyInstance, MAKEINTRESOURCE(IDR_CUR1), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE);


Finally I got it working by using:

HCURSOR cur = LoadCursorFromFile("arrow.ani");


But that is not how I want it, I do not wish to have anything besides a .exe for my final product, how can I do this? I have searched the forum several times and I cant find a answer. msdn points me to use LoadImage, but I cant get it to work. Please help

srelu
January 23rd, 2008, 08:04 PM
LoadCursorFromFile is the only way to use an ani cursor. You must load the cursor from a file.

A workaround could be to keep the ani file embedded in your exe, when you need it create a temporary file, load the cursor from it, then delete that file.

To embed the ani file in your exe, add it to the project as a custom resource. To retrieve it, use FindResource, LoadResource, LockResource. The last function returns a pointer to the bytes of the resource. Now you can write it to a temporary file.

Estrayk
January 25th, 2008, 12:27 PM
Thanks for the quick and nice answer, It worked very good. I made small class to make it even easier. Here it is if someone else are looking at this post and want quick and easy way to do it.

.h

class RCMinder
{
public:
RCMinder(void) { mLoaded = false; }
~RCMinder(void) {}

inline char *GetData(void) { return mRCData; }
inline unsigned long GetSize(void) { return mSz; }

/**
returns true on success
returns false if no resource is loaded
*/
bool Write(char *, char *);

/**
returns 0 on success
returns -1 on FindResource error
returns -2 on LoadResource error
returns -3 on LockResource error
*/
int Load(int, LPCTSTR);

private:
bool mLoaded;
HRSRC mRC;
HGLOBAL mRCHandle;
char *mRCData;
char mPathAndName[64];
unsigned long mSz;
};

.cpp


int RCMinder::Load(int rc_name, LPCTSTR rc_type)
{
if ((mRC = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(rc_name), rc_type)) == NULL)
return -1;

if ((mRCHandle = LoadResource(GetModuleHandle(NULL), mRC)) == NULL)
return -2;

if ((mRCData = (char *)LockResource(mRCHandle)) == NULL)
return -3;

mSz = SizeofResource(GetModuleHandle(NULL), mRC);
mLoaded = true;

return 0;
}

bool RCMinder::Write(char *path, char *file_name)
{
if (!mLoaded) return false;

sprintf(mPathAndName, "%s%s", path, file_name);

std::ofstream f(mPathAndName, std::ios::binary);
f.write((const char *) mRCData, mSz);
f.close();

return true;
}


Usage is simple, in this example i will wirte a .ani cursor and load it.

HCURSOR Cursor;
RCMinder *RC = new RCMinder();

if ((RC->Load(IDR_ANICURSOR1, RT_ANICURSOR)) != 0) // http://msdn2.microsoft.com/en-us/library/ms648009(VS.85).aspx resource types
ErrorBox("Error");
if (!RC->Write("./", "cur.ani"))
ErrorBox("Error");

Cursor = LoadCursorFromFile("cur.ani");

Cheers
EDIT: made a few changes, works better now

//JP added flex table