WPF Cursor casting problem

2019-07-09 05:59发布

问题:

I have a System.Windows.Forms.Cursor with me and wanted to assign it to a WPF's image.Cursor property which happens to be of System.Windows.Input.Cursor type. The constraint here is, the former Cursor type is returned by our Framework and i can in no way modify it. Is there any way of casting the former to latter?

回答1:

This did the trick for me:

SafeFileHandle panHandle = new SafeFileHandle(System.Windows.Forms.Cursors.PanNorth.Handle, false);
this.Cursor = System.Windows.Interop.CursorInteropHelper.Create(panHandle);

Documentation for SafeFileHandle warns against using false for the second arg but I got SEHExceptions no matter what if I used true (even if I used Cursors.PanNorth.CopyHandle())