user iconThe Spud on September 10th 2008
13132 views

topic iconUsing Cursors in VB?

Cursor Problem

Is it possible to use Real World cursors in Visual Basic applications?
I get the following error message:
"Image format is not valid. The image file may be corrupted. Parameter name: stream"
Any help would be appreciated, Thanks

user iconVlasta on September 10th 2008 0

I did not try that myself, but it should be possible. What kind of cursor (static, animated, color depth) did you try to use and how?

user iconAnonymous on September 11th 2008 0

I just made a simple black and white static cursor (that I named "NewArrow") as a trial, and used the following code:

   Dim Cur As New Cursor("C:\Documents and Settings\Administrator\My Documents\My Pictures\NewArrow.cur")

   Windows.Forms.Cursor.Current = Cur

I also tried using cursors from the RealWorld Cursor Editor library without success. However, when I tried a custom made cursor (that was included with a program that I had downloaded) using the same VB code it worked.

user iconThe Spud on September 11th 2008 0

Sorry, Forgot to log on before posting previous message. Also seem to be having some kind of smiley problem with my brackets.

user iconVlasta on September 11th 2008 0

Did a couple of test with C# and yes, you are right. .NET cannot load any cursor except black and white (you need to select monochromatic format when creating the cursor in the editor) with its native methods. I thought the later versions of the .net framework were better, but it still sucks after 5 years of development...

Here is a workaround in C#, which should be easy to transform to VB code.

[System.Runtime.InteropServices.DllImport("user32.dll" )]
static extern IntPtr LoadCursorFromFile(string fileName);

IntPtr colorCursorHandle = LoadCursorFromFile("your-cursor.cur" );
Cursor.GetType().InvokeMember("handle", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetField, null, this.Cursor, new object[] { colorCursorHandle });
user iconThe Spud on September 13th 2008 0

Changed my cursor to monochromatic and it works fine. Haven't been able to figure out the workaround in VB yet, but I'll get there eventually.
Thanks for your help, much appreciated.

user icon