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;
}