原文: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. 原生JSON解析

    原生JSON解析 JSONObject:JSON数据封装对象JSONArray:JSON数据封装数组 布局: <?xml version="1.0" encoding=&qu ...

  2. React Native 二维码扫描组件

    学rn得朋友们,你们知道rn开源项目吗?来吧看这里:http://www.marno.cn/(rn开源项目) React Native学习之路(9) - 注册登录验证的实现 + (用Fetch实现po ...

  3. cuda中用cublas库做矩阵乘法

    这里矩阵C=A*B,原始文档给的公式是C=alpha*A*B+beta*C,所以这里alpha=1,beta=0. 主要使用cublasSgemm这个函数,这个函数的第二个参数有三种类型,这里CUBL ...

  4. LeetCode题解之Convert BST to Greater Tree

    1.题目描述 2.问题分析 使用一个vector将所有节点的值以升序排列.然后比较,求和,加上. 3.代码 TreeNode* convertBST(TreeNode* root) { if (roo ...

  5. jqGrid删除单条和多条数据

    $("#Delete").click(function () { //var id = $("#showGrid").jqGrid('getGridParam' ...

  6. Huawei华为交换机 consolep密码和vty密码配置telnet,ssh

    以登录用户界面的认证方式为密码认证,密码为Huawei@123为例,配置如下. <HUAWEI> system-view [HUAWEI] user- [HUAWEI-ui-console ...

  7. 常用的 接口访问方法 post 和get

    public string GetFunction(string serviceAddress) { HttpWebRequest request = (HttpWebRequest)WebReque ...

  8. 阿里云centos7.2 lamp配置

    安装apache 1.安装yum -y install httpd 2.设置apache服务开机启动systemctl enable httpd.service 3.开启apache服务systemc ...

  9. 记录一次mysql使用load into命令导入csv格式数据的过程

    今天从qwiklab实验获取一组数据,大概有5万条,在qwiklab实验室使用的是pgsql数据库,但是今天想把他插入本地的mysql数据库中. 1.首先是查看一下数据内容: 数据中有的是空值,有的是 ...

  10. laravel 实现思路以及各组件原理

    laravel 内核是个IOC容器,IOC是把本来自己实例化的对象. 通过在容器里注册,通过容器来进行实例化. laravel队列用的是redis的列表来实现.