0

We have an HwndHost UIElement in our WPF application which is used to display Direct3d graphics, and the only way I have found to set a cursor for the HwndHost UIElment is to call the Win32 API SetCursor(). All of our cursors are resources in managed assemblies, and I would prefer to not change that, but I have not been able to find a way to load one of these cursors via any Win32 APIs like LoadImage().

Does anybody know how to get a handle(hCursor) to a cursor which is a resource in a managed assembly?

Or, is there another way to set a cursor on an HwndHost displaying Direct3D graphics?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Hank
  • 193
  • 1
  • 10

1 Answers1

1

The technique I've used in this situation before is:

DllImport("user32.dll", EntryPoint = "LoadCursorFromFileW", CharSet = CharSet.Unicode)
public static extern IntPtr LoadCursorFromFile(String str);

...save your cursor into a temporary file...

IntPtr hCursor = DllImport.LoadCursorFromFile(sFilename);

...use hCursor in the SetCursor...
  • make sure your cursor .cur files are added to your project as "Embedded Resource"
  • determine/calculate the resource path to the cursor in your managed resources
  • access the Stream to that cursor by using GetManifestResourceStream
  • save that binary data into a temporary file
    (e.g. use FileStream with FileMode.Create and if you want FileAttributes.Temporary)
  • use LoadCursorFromFile to load the data from the file and create a cursor handle which you can then use in SetCursor.
  • clean up the temporary file

http://support.microsoft.com/kb/319292

Colin Smith
  • 12,375
  • 4
  • 39
  • 47