-
[2005] Custom Mouse Cursor
:confused: I have seen multiple programs where you can choose a fancier mouse cursor to use instead of the plain white one. For instance, the cursor with a flame around it. I am in need of some code to change the mouse cursor to a specific Icon file on my computer. I would appreciate any code that you know works for sure. :)
-
Re: [2005] Custom Mouse Cursor
-
Re: [2005] Custom Mouse Cursor
I've tried many things, including what was in that thread. I always get this error:
"Image format is not valid. The image file may be corrupted.
Parameter name: stream"
-
Re: [2005] Custom Mouse Cursor
Here's My Code:
VB Code:
Dim curPCPI As Cursor
Dim cursorfile As IO.Stream
cursorfile = Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("PCPI.cur")
curPCPI = New Cursor(cursorfile)
Me.Cursor = curPCPI
curPCPI = Nothing
I Actually get this error:
"Object reference not set to an instance of an object."
Directed at the line: "curPCPI = New Cursor(cursorfile)"
-
Re: [2005] Custom Mouse Cursor
is your cursor file in the app's resurse file? If yes then use this code:
VB Code:
Me.Cursor = New Cursor(Me.GetType(), "PCPI.cur")
-
Re: [2005] Custom Mouse Cursor
I am now getting the image corrupt error:
"Image format is not valid. The image file may be corrupted.
Parameter name: stream"
Although, there is nothing wrong with my image.
-
Re: [2005] Custom Mouse Cursor
Quote:
Originally Posted by StreaksAthlete
I am now getting the image corrupt error:
"Image format is not valid. The image file may be corrupted.
Parameter name: stream"
Although, there is nothing wrong with my image.
that error can acure when the the file can't be read. Are you sure that the iamge file is .cur (cursor) file? and it exists in the resurs file with the same name?
-
Re: [2005] Custom Mouse Cursor
-
Re: [2005] Custom Mouse Cursor
I just tried adding a cursor file to the Resources tab and then accessing it via My.Resources. It produced that same error. That's doesn't really help you fix the issue but as far as I can see that should have worked so it at least confirms that you are not insane... or at least no more than me. ;)
-
Re: [2005] Custom Mouse Cursor
I am not sure why it happens but I am sure there is some thing wrong with the cursor file. Not if the image was created with the jpg format and later the extension got changed to .cur it does not change the format of the image. It is still have jpg format. Try to use it with another cursor file to test the code. You can find cursor file in the “C:\WINDOWS\Cursors” directory.
-
Re: [2005] Custom Mouse Cursor
I tried it with an alternate Windows cursor, and I did not get any error, BUT, the cursor still didn't change, it stayed the same Default cursor.
EDIT:
Nevermind, I got the C:\Windows\Cursor to work, so the problem must be my image file. It was an Icon file, saved from an Icon editor, and I changed it to a .cur file, is that the problem?
-
Re: [2005] Custom Mouse Cursor
Ok try this. It works i tested it!
VB Code:
Dim ms As New IO.MemoryStream(My.Resources._3dsns)
Me.Cursor = New Cursor(ms)
ms.Close()
ms.Dispose()
-
Re: [2005] Custom Mouse Cursor
Error 1 Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(buffer() As Byte)': Value of type 'System.Drawing.Bitmap' cannot be converted to '1-dimensional array of Byte'.
'Public Sub New(capacity As Integer)': Value of type 'System.Drawing.Bitmap' cannot be converted to 'Integer'.
-
Re: [2005] Custom Mouse Cursor
can you post your project?
-
1 Attachment(s)
Re: [2005] Custom Mouse Cursor
I don't get any error. Here check this simple project.
-
1 Attachment(s)
Re: [2005] Custom Mouse Cursor
-
Re: [2005] Custom Mouse Cursor
I created a resource from a standard Windows cursor file. I then tried loading that cursor from the disc file via a FileStream, which worked, and from the resource via a MemoryStream, which didn't. Can anyone see any obvious issues with this:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using fs As New IO.FileStream("C:\WINDOWS\Cursors\arrow_i.cur", IO.FileMode.Open)
Me.Cursor = New Cursor(fs)
End Using
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Using ms As New IO.MemoryStream
Dim buffer As Byte() = My.Resources.arrow_i
ms.Write(buffer, 0, buffer.Length)
Me.Cursor = New Cursor(ms)
End Using
End Sub
-
Re: [2005] Custom Mouse Cursor
The problem is that my file is not a valid .cur file. I renamed an Icon (.ico) to .cur which is most likely the problem. But, how do I make it a valid Cursor file?
-
Re: [2005] Custom Mouse Cursor
Ah HA!: There was something obviously wrong:
VB Code:
Using ms As New IO.MemoryStream
Dim buffer As Byte() = My.Resources.arrow_i
ms.Write(buffer, 0, buffer.Length)
[U]ms.Seek(0, IO.SeekOrigin.Begin)[/U]
Me.Cursor = New Cursor(ms)
End Using
-
Re: [2005] Custom Mouse Cursor
Quote:
Originally Posted by StreaksAthlete
The problem is that my file is not a valid .cur file. I renamed an Icon (.ico) to .cur which is most likely the problem. But, how do I make it a valid Cursor file?
If you renamed myfile.mdb to myfile.doc would you expect Microsoft Word to open it successfully? The extension doesn't change the contents of a file, only how Windows tries to handle it. An ICO file with a CUR extension is still an icon file. Visual Studio allows you to create cursor and icon objects. You'll need to open the icon, copy its graphical content and paste that into a new cursor. You then save the cursor as a resource and you can access it. I would suggest using the My.Resources method I showed previously.
-
Re: [2005] Custom Mouse Cursor
The sarcasm isn't needed. I am thankfull for everybody's help! You guys should get paid for everyword. :thumb: :thumb: :thumb:
Now, is there a way to make this custom cursor a System-Wide change?
-
Re: [2005] Custom Mouse Cursor
It wasn't sarcasm. It was an example to illustrate why what you were trying to do couldn't possibly work. Is that a guilty conscience I smell? ;)
You can change the Windows.Forms.Cursor.Current property but every object will simply use its own cursor as soon as you move the mouse. If you want to affect the system-wide cursor settings then you'd have to edit those Windows settings. I would guess that those settings are stored in the registry. You can find them relatively easily by checking what the location of your current cursor is in the Mouse properties and then searching the registry for that value.
-
Re: [2005] Custom Mouse Cursor
Quote:
Originally Posted by jmcilhinney
Ah HA!: There was something obviously wrong:
VB Code:
Using ms As New IO.MemoryStream
Dim buffer As Byte() = My.Resources.arrow_i
ms.Write(buffer, 0, buffer.Length)
[U]ms.Seek(0, IO.SeekOrigin.Begin)[/U]
Me.Cursor = New Cursor(ms)
End Using
Ah so this is how to access randomly to the data! It seems the other stream objects dose not have that method. Good info! Sorry I can’t give any rep it doesn’t let me, but I like to. This is a good info thanks.
-
Re: [2005] Custom Mouse Cursor
Quote:
Originally Posted by VBDT
Ah so this is how to access randomly to the data! It seems the other stream objects dose not have that method. Good info! Sorry I can’t give any rep it doesn’t let me, but I like to. This is a good info thanks.
Some stream types can seek while others can't. Every stream has a CanSeek property to indicate whether it can or not. Those that can are usually those that are sitting on your own machine. Those that can't are usually those coming from elsewhere, like a NetworkStream. It doesn't make sense to randomly access a stream that you haven't received all of from a remote source.
Note that classes like a StreamReader are NOT streams and thus have no CanSeek property. A StreamReader is an object that reads text from a stream. Its BaseStream property is a reference to a stream. That stream has a CanSeek property. Note that a StreamReader can read any type of stream, including FileStream, MemoryStream, NetworkStream, etc. Whether you can reread the contents of a StreamReader depends on the type of its BaseStream, e.g.
VB Code:
If myStreamReader.BaseStream.CanSeek Then
'Reset the stream position to the beginning.
myStreamReader.BaseStream.Seek(0, IO.SeekOrigin.Begin)
End If
-
Re: [2005] Custom Mouse Cursor
Thanks again Jmc for the great info ;)