Hello all, I'm not sure if this post landed in the right forum, I'm sorry if it didn't.
I'm developing a smallish Managed DirectX (2.0) application and I would like to change the standard mouse cursor image to my own image. I've created a small 32x32 image that I would like to have instead of the normal windows cursor. I guess this would change the cursor in other windows applications aswell during the time my application is running but that is ok. Is this possible or do I need to hide the standard cursor and render my own cursor ingame with Direct3D
If I look at properties for the form of my application there is a Cursor dropdown, but this only allows me to change between different images of the standard windows cursor. Like "normal arrow", "hourglass", "hand" and so on, not my own custom image.
Any help appreciated.
// cybbe

Setting a custom cursor image [MDX2]
Maria Shneerson
1) Add your cursor to the project (I'm using VS .Net 2003) and set it to an embedded resource.
2) Create a private Cursor myCursor = null; in your class to hold the cursor. I do this rather than load the cursor each time I need to change it (I have three cursors that I use based on the situation).
3) Initialize the cursor in your one-time start up code. myCursor = new Cursor(GetType(), "name of cursor.cur"); I do not think you have to worry about losing the device on this. Maybe some one else can clarify.
4) Set the cursor such as: MainControl.m_TheEngine.Cursor = myCursor;
This will only affect your application, other applications will still have their cursor.
This is my example main loop, where I set the cusor one time to a hand. In this case you do not need your own custom cursor. Just throwing this out for free!
static void Main()
{
m_TheEngine = new MainControl();
Debug.Assert(m_TheEngine != null);
if (m_TheEngine != null)
{
m_TheEngine.Cursor = Cursors.Hand;
m_TheEngine.InitializeEngine();
m_TheEngine.Show();
Application.Run(m_TheEngine);
m_TheEngine.Cursor = Cursors.Default;
m_TheEngine.CleanUp();
m_TheEngine.Dispose();
m_TheEngine = null;
}
}
alleh
Ok, nice to know that it is possible but I can't get it to work. In my form I do this:
public class MyForm : Form
{
private Cursor myCursor = null;
public Constructor()
{
// This line crashes the program, I guess it can't find "cursor"
// myCursor = new Cursor(GetType(), "cursor");
this.Cursor = Cursor.Hand;
}
...
}
So the Cursors.Hand works (it changes it to the hand) but when I try to load my own cursor it crashes, I don't even assign it to this.Cursor, it's the loading (new Cursor()) that crashes. So the question is, how do I add my cursor to the project correctly I doubleclicked the MyForm.resx and took "Add existing file" and choosed my cursor.png (don't know how to do a .cur) and it loaded it as "cursor" and I see the picture of the cursor.
So the question is, what should I write to load it "cursor.png" or "data/cursor.png" doesn't work. I've tried all kinds of paths...
Any ideas
csharpman
Two things...
1) Make sure you are including the cursor in your resources. If you are using Visual Studio .Net, select the cursor and make sure to change it to "Embedded Resource" so the cursor is compiled in the EXE otherwise you have to ship it.
2) You are telling it only the name "cursor" when you need to add the extension "cursor.cur"
7cutlass1
xied75
Simple steps to take.
1) Build your vertex structure and create a textured quad.
2) Get the coordinates of your mouse relative to the window.. top left should be (0, 0) and bottom right should be (width of your window, height of your window)
3) Render the textured quad with your "cursor" as the texture. Using the coordinates and assigning it toDevice.Transform.World
I hope this helps.
Take care.
D.Y.L.
Ok, got it to work (kinda) but I still have problems, let me explain. I've created a cursor file with _colors_ and saved it. I've tried different formats and all but whatever I do my loaded cursor is black and white only. No colors, not even grayscale... I've tried it both as an embedded and loading it as a file.
However, I found this thread: http://www.mcse.ms/message590138.html where the same problem is specified and "solved". The solution is to import LoadCursorFromFile from native and use that, like this:
cursor = new Cursor(GetType(), "MyCursorColored.CUR");
IntPtr colorCursorHandle = Native.LoadCursorFromFile("MyCursorColored.CUR");
cursor.GetType().InvokeMember("handle", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
System.Reflection.BindingFlags.SetField, null, cursor, new object[] { colorCursorHandle });
this.Cursor = cursor;
That way it works but first of all, is it really this bad But I can live with that I guess...
One last thing though. The native LoadCursorFromFile loads it from file (of course) but I tried to import LoadCursor(string resourceName) and use that one, but it doesn't recognize my embedded resource and it creashes with "object not set to reference". Do anyone have an idea on how to make Native.LoadCursor() work with the embedded resource Kinda ugly to send an .CUR file with releases :S
EDIT:
After some testing I found an exception that was thrown when I try to load the cursor from the embedded resource (Native.LoadCursor()). The exception is:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
But a big thanks to you Mikel to help me get this up and running.