WPF 创建自定义鼠标光标指针
2021-04-13 17:25
标签:override UI class orb lease code ret stat private WPF Cursor类中的两个构造函数: 以上的构造函数所使用的的资源均是 ani 或 cur的文件,并不支持图片的方式生成。 提供的扩展方法: 转自:http://www.cnblogs.com/zhouyinhui/archive/2010/05/28/1746502.html WPF 创建自定义鼠标光标指针 标签:override UI class orb lease code ret stat private 原文地址:https://www.cnblogs.com/runningRain/p/8973986.htmlpublic Cursor(Stream cursorStream)
public Cursor(string cursorFile)
/// This class allow you create a Cursor form a Bitmap
///
internal class BitmapCursor : SafeHandle
{
public override bool IsInvalid
{
get
{
return handle == (IntPtr)(-1);
}
}
public static Cursor CreateBmpCursor(Bitmap cursorBitmap)
{
var c = new BitmapCursor(cursorBitmap);
return CursorInteropHelper.Create(c);
}
protected BitmapCursor(Bitmap cursorBitmap)
: base((IntPtr)(-1), true)
{
handle = cursorBitmap.GetHicon();
}
protected override bool ReleaseHandle()
{
bool result = DestroyIcon(handle);
handle = (IntPtr)(-1);
return result;
}
[DllImport("user32")]
private static extern bool DestroyIcon(IntPtr hIcon);
}
public static Bitmap BitmapSourceToBitmap(this BitmapSource source)
{
using (var stream = new MemoryStream())
{
var e = new BmpBitmapEncoder();
e.Frames.Add(BitmapFrame.Create(source));
e.Save(stream);
var bmp = new Bitmap(stream);
return bmp;
}
}