Introduction

I've had to build an application that displays contact information in an expandable Tree control. My users can grab the Contact from that Tree and drag it to several locations on various forms. Instead of having the same-ole-same-ole *yawn* dragdrop cursor, I thought it would be kind of cool to mimick the Firefox browser's drag cursor...

Those of you that use the Firefox Internet Browser have almost certainly at one time or another selected a chunk of text and dragged it to another location on the screen... If you have not, try it and check out the Drag Cursor: It's an exact duplicate of the text you selected and goes from slightly faded in the middle to completely transparent at the edges. That's exactly what the xCursor is: A semi-transparent cursor that displays text based on property settings.

So, like all developers with just enough knowledge to be dangerous, I started digging around on the internet to see if anyone's already got this bad-boy out there. Nope. Then I started to see if anyone had anything similar I could learn from. Nope. So finally I started to look to see if *anyone* had *anything* that utilized a custom cursor and bingo! A whopping 5 articles on CodeProject.com: C++, C++, C++, C# and C# - and here's me: a VB.NET developer... Surely I can take one of the C# articles and convert it? C# and VB.NET are (more or less) identical right? Normally, I would agree with that last statement but for this - and from here forward, I will have to say NO!

What was the problem you might be asking yourselves right about now? I'll tell ya: The WinAPI calls that generate custom cursors are expecting not the graphics object - but a POINTER to the graphics object and guess what the Rocket Scientists at Microsoft forgot to give VB.NET developers?? That's right: pointers.

Knowing what I do about VB.NET, I was certain there was a way to pull this off and sure enough, there is: It's called Marshalling... So with my newly discovered Marshalling information and a nice article by Alexander Bekrenev, I was able to pull it off. Thanks Alex!

Background

The xCursor and demo application were written in VB.NET (VS 2005) and for those of you out there who insist C# is the only way to go... I also have a C# version available upon request.

Using the Cursor

Just add a reference to xCursor.dll into your project and you're all set.

xCursor has the following seven properties:

CursorText: This is the text to be displayed as a cursor. Multi lined cursors are supported but you have to create the line breaks in the text.

Font: The font to use for the xCursor

Fade: If True, the text will fade to transparent along the edge *NOTE* Textcolor will be IGNORED if Fade=True

TextColor: The color of the text used in the cursor

TextTransparency: 0-255 where 0=100% transparent and 255= solid.*NOTE* because of how the cursor is created, all transparencies will have a blueish tint. I am searching for way to prevent this - any thoughts or help would be greatly appreciated!

GoodDrop: This is used to tell the cursor if its currently over a good or no-drop location - or to not draw an icon at all.

Shrinkage: If you are dragging a large chunk of text, you might want to reduce the size of the font used in the cursor: This property tells the cursor how much to reduce the size by.

The key is the GiveFeedback method. This method allows you to evaluate whether or not your cursor is over a valid drop location and even gives you a chance to make sure that the location is valid for the specific item being dragged.

You may also notice that I am trapping the DragOver of the DRAG SOURCE and not just the Drag Destination. That's because I want to give feedback to the cursor that I'm over a "No Drop" location so I'll get my custom cursor and not the defaults, windows no-drop cursor.

Interesting...?

There are a pair of GIF images added to the cursor as resources. You could conceivably add your own good/no drop image properties and pass them to the cursor, or just replace the ones I made with your own.

Acknowledgements

I'd like to thank Alexander Bekrenev for his article showing how to create a custom cursor. Thanks again, Alex.

thanx for your article. I tried parts of the c# version and run over a memory leak you are causing when calling CreateIconIndirect(...)
You should take care to free the pointer which is returned by this function as this is not handled by the .NET Framework.
Do this with:

DestroyIcon(pointer_created_by_CreateIconIndirect);

Furthermore, you have to delete the GDI+ Objects which were created by GetHbitmap() which are used in the IconInfo struct. In your case:

DeleteObject(IconInfoInstance.hbmMask); //has to be called to free GDI+ memory
DeleteObject(IconInfoInstance.hbmColor); //has to be called to free GDI+ memory

The DestroyIcon and the DeleteObject functions have to be imported like that: