By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,441 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,441 IT Pros & Developers. It's quick & easy.

Load Color Cursor from Embedded Resource

P: n/a
Hello all,

I believe I have a tough one today. I'm trying to load a color cursor from
an embedded resource in C# and am having quite a bit of trouble doing it.
Let me tell what I have found out already.

Firstly, the normal "new Cursor(GetType(), "Cursor.cur")" will not work with
cursors in color.

However I did find an example online that works when given a filename and
I've included the code for anyone who needs it. It uses a dllimport to use a
function called LoadCursorFromFileW. How can I adapt this or use a similar
approach to use a Cursor that is an embedded resource in my C# project.
public class CursorFactory {
[DllImport("user32.dll", EntryPoint = "LoadCursorFromFileW",
CharSet = CharSet.Unicode)]
private static extern IntPtr LoadCursorFromFile(String str);

public static Cursor Create(string filename){
IntPtr hCursor;
Cursor result = null;
try {
hCursor = LoadCursorFromFile(filename);
if (!IntPtr.Zero.Equals(hCursor)) {
result = new Cursor(hCursor);
} else {
throw new
ApplicationException("Could not create cursor from file "
+ filename);
}
} catch(Exception ex) {
//log exception
}

return result;
}

Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Sorry to bump, but I did write in on a friday. Does anyone have any ideas?
I've begun to look at the LoadCursor function in user32 but so far I've not
made any progress.

Andrew

"Andrew Roesch" <no****@nospam.com> wrote in message
news:OX****************@TK2MSFTNGP11.phx.gbl...
Hello all,

I believe I have a tough one today. I'm trying to load a color cursor from
an embedded resource in C# and am having quite a bit of trouble doing it.
Let me tell what I have found out already.

Firstly, the normal "new Cursor(GetType(), "Cursor.cur")" will not work with cursors in color.

However I did find an example online that works when given a filename and
I've included the code for anyone who needs it. It uses a dllimport to use a function called LoadCursorFromFileW. How can I adapt this or use a similar
approach to use a Cursor that is an embedded resource in my C# project.
public class CursorFactory {
[DllImport("user32.dll", EntryPoint = "LoadCursorFromFileW",
CharSet = CharSet.Unicode)]
private static extern IntPtr LoadCursorFromFile(String str);

public static Cursor Create(string filename){
IntPtr hCursor;
Cursor result = null;
try {
hCursor = LoadCursorFromFile(filename);
if (!IntPtr.Zero.Equals(hCursor)) {
result = new Cursor(hCursor);
} else {
throw new
ApplicationException("Could not create cursor from file "
+ filename);
}
} catch(Exception ex) {
//log exception
}

return result;
}

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.