开发者

Using a Windows.Forms.Cursor for a WPF Cursor?

开发者 https://www.devze.com 2023-03-26 15:39 出处:网络
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.

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?


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())


I avoided SEHExceptions by this:

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

精彩评论

暂无评论...
验证码 换一张
取 消