原文:WPF编程,自定义鼠标形状的一种方法。

版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/87275432

参考:https://www.cnblogs.com/TianFang/p/5186497.html

1、增加引用

System.Drawing.dll

2、代码

public class CursorHelper
{
static class NativeMethods
{
public struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
} [DllImport("user32.dll")]
public static extern SafeIconHandle CreateIconIndirect(ref IconInfo icon); [DllImport("user32.dll")]
public static extern bool DestroyIcon(IntPtr hIcon); [DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
} [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public SafeIconHandle()
: base(true)
{
} protected override bool ReleaseHandle()
{
return NativeMethods.DestroyIcon(handle);
}
} static Cursor InternalCreateCursor(System.Drawing.Bitmap bitmap, int xHotSpot, int yHotSpot)
{
var iconInfo = new NativeMethods.IconInfo
{
xHotspot = xHotSpot,
yHotspot = yHotSpot,
fIcon = false
}; NativeMethods.GetIconInfo(bitmap.GetHicon(), ref iconInfo); var cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);
return CursorInteropHelper.Create(cursorHandle);
} public static Cursor CreateCursor(UIElement element, int xHotSpot = 0, int yHotSpot = 0)
{
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(new Point(), element.DesiredSize)); var renderTargetBitmap = new RenderTargetBitmap(
(int)element.DesiredSize.Width, (int)element.DesiredSize.Height,
96, 96, PixelFormats.Pbgra32); renderTargetBitmap.Render(element); var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); using (var memoryStream = new MemoryStream())
{
encoder.Save(memoryStream);
using (var bitmap = new System.Drawing.Bitmap(memoryStream))
{
return InternalCreateCursor(bitmap, xHotSpot, yHotSpot);
}
}
}
}

3、调用

this.Cursor = CursorHelper.CreateCursor(elips);//elips是自定义控件的名字

 

WPF编程,自定义鼠标形状的一种方法。的更多相关文章

  1. WPF编程,使用WindowChrome实现自定义窗口功能的一种方法。

    原文:WPF编程,使用WindowChrome实现自定义窗口功能的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/arti ...

  2. c#自定义鼠标形状

    更改鼠标指针,需要使用到 Windows API: 1. 添加命名空间的引用: using System.Runtime.InteropServices; using System.Reflectio ...

  3. Python并发编程之创建多线程的几种方法(二)

    大家好,并发编程 进入第二篇. 今天的内容会比较基础,主要是为了让新手也能无障碍地阅读,所以还是要再巩固下基础.学完了基础,你们也就能很顺畅地跟着我的思路理解以后的文章. 本文目录 学会使用函数创建多 ...

  4. unity 改变鼠标样式的两种方法

    1.第一个直接改变鼠标样式 public var cursorTexture:Texture2D; private var changeFlag = false; function Update(){ ...

  5. Ajax设置自定义请求头的两种方法

    用自定义请求头token为例 方法一 $.ajax({ type: "post", url:"http://127.0.0.1:4564/bsky-app/templat ...

  6. C/C++:Windows编程—调用DLL程序的2种方法(转载)

    文章为转载,原文出处https://blog.csdn.net/qq_29542611/article/details/86618902 前言先简单介绍下DLL.DLL:Dynamic Link Li ...

  7. 【WPF】自定义鼠标样式

    /// <summary> /// This class allow you create a Cursor form a Bitmap /// </summary> inte ...

  8. 自定义的tabBarController的几种方法

    本文转载自:http://blog.sina.com.cn/s/blog_79c5bdc30100t88i.html 我自己实现的一种可以很方便的实现更换TabBarController图片的方法,代 ...

  9. python中自定义超时异常的几种方法

    最近在项目中调用第三方接口时候,经常会出现请求超时的情况,或者参数的问题导致调用异代码异常.针对超时异常,查询了python 相关文档,没有并发现完善的包来根据用户自定义的时间来抛出超时异常的模块.所 ...

随机推荐

  1. Ink——一款使用React风格开发命令行界面应用(CLI App)的nodejs工具

    Github: https://github.com/vadimdemedes/ink Ink introduction: React for CLIs. Build and test your CL ...

  2. JavaScript函数节流(throttle)与函数去抖(debounce)

    对于浏览器窗口大小改变的时候,来动态改变页面元素的大小,可以采用window的resize事件,实现代码: <script type="text/javascript"> ...

  3. Docker相关概念

    一.概念 ①云计算:是一种资源的服务模式,该模式可以实现随时随地,便捷按需地从可配置计算资源共享池中获取所需的资源(如网络.服务器.存储.应用及服务),资源能够快速供应并释放,大大减少了资源管理工作的 ...

  4. SqlServer为字段创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名) 创建索引实例: 聚簇索引 create clustered index index_name on table_name (c ...

  5. systemd-analyze – 在Linux中查找系统启动性能统计信息

    您是否在使用 systemd 系统和服务管理器,并且您的 Linux 系统需要较长时间才能启动,或者您希望查看系统启动性能的报告? 如果是的话,你已经登陆了正确的地方. 在本文中,我们将向您展示如何使 ...

  6. ulimit linux文件配置

    文件描述符在形式上是一个非负整数.实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表.当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符.在程序设计中,一 ...

  7. orcl regexp_like 的用法

    oracle10g以上支持正则表达式的函数主要有下面四个:1.REGEXP_LIKE :与LIKE的功能相似2.REGEXP_INSTR :与INSTR的功能相似3.REGEXP_SUBSTR :与S ...

  8. SDN期末作业博客

    一.项目描述 A P4-based Network Load Balancing Application 基于P4的网络负载均衡项目 二.个人分工 The Controller : Acquire t ...

  9. Sublime2 DocBlocker插件在自动补全注释时输出自定义作者和当前时间等信息

    Sublime在进行前端开发时非常棒,当然也少不了众多的插件支持,DocBlocker是在Sublime平台上开发一款自动补全代码插件,支持JavaScript (including ES6), PH ...

  10. 旋转的地球css3

    css3果然博大精深: 1.代码里面还有用box-shadow制作椭圆形阴影的效果,厉害了!之前找了好久都没找到,今天给找到了 html: <section class="stage& ...