原文: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. ubantu 16.4 Hadoop 完全分布式搭建

    一个虚拟机 1.以  NAT网卡模式   装载虚拟机 2.最好将几个用到的虚拟机修改主机名,静态IP     /etc/network/interface,这里 是 s101 s102  s103 三 ...

  2. 判断exe是64位还是32位

    右击exe属性,查看兼容模式. 如果有windwos vista之前的版本则为32位的,如下图: 如果没有windwos vista之前的版本则为64位的,如下图:

  3. 记一款bug管理系统(bugdone.cn)的开发过程(1) -- 为什么要开发一款bug开发系统

    对于从事软件研发行业的同学来说bug管理系统肯定不陌生.本人03年左右开始正式成为一名码农,工作期间接触过若干bug管理系统,如JIRA等,不过都是自行部署在公司内网的. 几年过去了,现在已经是互联网 ...

  4. myeclipse 10配置jboss 7.1.1无法启动Error: Could not create the Java Virtual Machine

    myeclipse 10中配置jboss 7.1.1,多写了个server name,结果死活启动不了.后来,发现了这个细节. 错误显示: 错误源头: 删掉Server name后,可以完美启动 小问 ...

  5. Asp连接Oracle (包含绿色版12.2客户端和ODBC驱动安装)

    我能操作的终端电脑是一台linux系统可以上互联网 ,服务器在部署在独立的私网上,不方便上互联网.服务器是2008R2.安装vs不是很方便.其所linux下作开发不是不可以,java php mono ...

  6. ETL技巧应用(高级应用介绍:准备区运用、 时间戳的运用、日志表的运用、使用调度)

    1.1    准备区运用 a.在构建数据仓库时,数据源位于一服务器上,数据仓库在另一服务器端,数据源Server端访问频繁,并且数据量大,需要不断更新, b.建立准备区数据库: >将数据抽取到准 ...

  7. 【2017下集美大学软工1412班_助教博客】团队作业4——Alpha冲刺日志公示

    作业要求 团队作业4--第一次项目冲刺(Alpha版本) 团队评分结果和评分标准 检查项 总分 会议内容 代码签入 心得体会或其他记录 燃尽图 会议照片 评论区反馈 组别 分值 10 2 2 2 1 ...

  8. 如何在SAE搭建属于自己的黑盒xss安全测试平台

    Author:雪碧 http://weibo.com/520613815 此篇文章技术含量不高,大牛不喜勿喷,Thx!写这篇文章主要是为了各位小伙伴在SAE搭建XSSING平台的时候少走点弯路(同志们 ...

  9. Articulate Presenter文字乱码的排除

    Articulate Presenter乱码的问题如何设置? 字体乱码的设置: 1.首先如果ppt中有中文内容,肯定需要将Articulate Presenter的Character Set设置为No ...

  10. PHP判断用户是否手机访问

    自定义的函数如下: $agent = check_wap(); if( $agent ) { header('Location: http://www.nowamagic.net'); exit; } ...