views:

36

answers:

2

In VB.Net with Winforms how do I make an image follow the cursor even when it leaves the form? I want to do this during a drag and drop operation. BCL and/or GDI is better for my usecase than P/Invokes.

If you are familiar with PeaZip, it does something of this sort when dragging a file from it's interface.

A: 

For information on doing it with P/Invokes:

CodeProject: Custom Mouse Cursors (VB.NET)

Key ones from the article:

<DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
End Function

<DllImport("gdi32.dll")> _
Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
Charles Y.
A: 

The easiest way to do this seems to be just plain changing the mouse cursor to a composite image of the default mouse cursor and the image you want following it. Then after you are finished change it back to the default mouse cursor.

There are ways to do this with P/Invokes, but here is a theory on how to do it without them. In examples I've run across P/Invokes are primarily used to get a pointer to/for a cursor Structure that has been created and Marshalled into unmanaged memory. Then the Pointer is passed to the Constructor for the System.Windows.Forms.Cursor Class.

However, there is an overload in the Cursor Class that accepts a Memory Stream. If the same cursor file structure could be constructed in a Memory Stream, it could then be passed into the Constructor, which would give us the Cursor to change to. The 'cur' image file is very similar to an 'ico' file, and is fairly simple. It consists of a small header, an image directory, and finally the image data itself (Wikipedia entry on the ICO file format). This should not be too hard to construct and then write out to a Memory Stream.

Charles Y.