当前位置: 首页 > news >正文

在.net桌面程序中自定义鼠标光标

有的时候,一个自定义的鼠标光标能给你的程序增色不少。本文这里介绍一下如何在.net桌面程序中自定义鼠标光标。由于.net的桌面程序分为WinForm和WPF两种,这里分别介绍一下。

WinForm程序

对于WinForm程序,可以通过修改Control.Cursor属性来实现光标的修改,如果我们有光标文件的话,可以直接通过如下代码实现自定义光标:

    this.Cursor = new Cursor("myCursor.cur");

但这种方式不是本文介绍的重点,本文主要介绍如何自己绘制光标,这样则具有更多的可控性和灵活性。

创建一个自定义光标,首先需要定义需要一个光标结构 ICONINFO ,它的.net版本如下:

    public struct IconInfo
    {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }

然后通过GetIconInfo and CreateIconIndirect两个函数来合成光标。完整代码如下: 

 1     public class CursorHelper
 2     {
 3         static class NativeMethods
 4         {
 5             public struct IconInfo
 6             {
 7                 public bool fIcon;
 8                 public int xHotspot;
 9                 public int yHotspot;
10                 public IntPtr hbmMask;
11                 public IntPtr hbmColor;
12             }
13 
14             [DllImport("user32.dll")]
15             public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
16 
17 
18             [DllImport("user32.dll")]
19             [return: MarshalAs(UnmanagedType.Bool)]
20             public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
21         }
22 
23         public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
24         {
25             var icon = new NativeMethods.IconInfo
26             {
27                 xHotspot = xHotSpot,
28                 yHotspot = yHotSpot,
29                 fIcon = false
30             };
31 
32             NativeMethods.GetIconInfo(bmp.GetHicon(), ref icon);
33             return new Cursor(NativeMethods.CreateIconIndirect(ref icon));
34         }
35     }
View Code

测试代码为:

    using (Bitmap bitmap = new Bitmap(21, 26))
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.DrawRectangle(Pens.Red, 0, 0, 20, 25);
        this.Cursor = CursorHelper.CreateCursor(bitmap, 3, 3);
    }

 

WPF程序

至于WPF程序,和WinForm程序是非常类似的,一方面,它也可以通过光标文件来实现写入Cursor属性来自定义光标文件。

至于自己绘制光标,上面的代码基本上也是可以复用的,不过相对的要重新封装一下,完整代码如下: 

 1     public class CursorHelper
 2     {
 3         static class NativeMethods
 4         {
 5             public struct IconInfo
 6             {
 7                 public bool fIcon;
 8                 public int xHotspot;
 9                 public int yHotspot;
10                 public IntPtr hbmMask;
11                 public IntPtr hbmColor;
12             }
13 
14             [DllImport("user32.dll")]
15             public static extern SafeIconHandle CreateIconIndirect(ref IconInfo icon);
16 
17             [DllImport("user32.dll")]
18             public static extern bool DestroyIcon(IntPtr hIcon);
19 
20             [DllImport("user32.dll")]
21             [return: MarshalAs(UnmanagedType.Bool)]
22             public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
23         }
24 
25         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
26         class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid
27         {
28             public SafeIconHandle()
29                 : base(true)
30             {
31             }
32 
33             protected override bool ReleaseHandle()
34             {
35                 return NativeMethods.DestroyIcon(handle);
36             }
37         }
38 
39         static Cursor InternalCreateCursor(System.Drawing.Bitmap bitmap, int xHotSpot, int yHotSpot)
40         {
41             var iconInfo = new NativeMethods.IconInfo
42             {
43                 xHotspot = xHotSpot,
44                 yHotspot = yHotSpot,
45                 fIcon = false
46             };
47 
48             NativeMethods.GetIconInfo(bitmap.GetHicon(), ref iconInfo);
49 
50             var cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);
51             return CursorInteropHelper.Create(cursorHandle);
52         }
53 
54         public static Cursor CreateCursor(UIElement element, int xHotSpot = 0, int yHotSpot = 0)
55         {
56             element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
57             element.Arrange(new Rect(new Point(), element.DesiredSize));
58 
59             var renderTargetBitmap = new RenderTargetBitmap(
60                 (int)element.DesiredSize.Width, (int)element.DesiredSize.Height,
61                 96, 96, PixelFormats.Pbgra32);
62 
63             renderTargetBitmap.Render(element);
64 
65             var encoder = new PngBitmapEncoder();
66             encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
67 
68             using (var memoryStream = new MemoryStream())
69             {
70                 encoder.Save(memoryStream);
71                 using (var bitmap = new System.Drawing.Bitmap(memoryStream))
72                 {
73                     return InternalCreateCursor(bitmap, xHotSpot, yHotSpot);
74                 }
75             }
76         }
77     }
View Code

需要注意的是,由于使用的System.Drawing.BitMap,是需要引用System.Drawing.dll的

封装之后,是可以直接传入UIElement作为自绘制的光标的,得益于WPF的强大绘图功能,是可以非常容易的绘制漂亮的光标的。测试代码如下:

    this.Cursor= CursorHelper.CreateCursor(new UserControl1());

相关文章:

  • Beanstalkd中文协议解读
  • windows ping RPi 2B
  • BLE4.0相关
  • Steve Souders 高性能网站建设指南14条提升网站页面速度的最佳实践(1)
  • 生成日期序列
  • jQuery UI dialog插件出错信息:$(this).dialog is not a function
  • SPOJ QTREE4 lct
  • 2014 I/O归来:Google连接一切
  • kvm虚拟化技术中虚拟机vcpu与物理cpu绑定
  • YMP开发框架快速上手(一)
  • Apache CXF 框架结构和基本原理
  • PostgreSQL 9.5 使用 import foreign schema 语法一键创建外部表
  • 应用如何在后台执行
  • c语言随机数
  • 混合使用Azure LB和ILB访问相同web服务(3)
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • 【node学习】协程
  • C++11: atomic 头文件
  • centos安装java运行环境jdk+tomcat
  • Cumulo 的 ClojureScript 模块已经成型
  • Docker下部署自己的LNMP工作环境
  • GraphQL学习过程应该是这样的
  • HomeBrew常规使用教程
  • java8-模拟hadoop
  • Linux快速复制或删除大量小文件
  • ReactNative开发常用的三方模块
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • Spark RDD学习: aggregate函数
  • spring security oauth2 password授权模式
  • vue的全局变量和全局拦截请求器
  • 基于Android乐音识别(2)
  • 白色的风信子
  • 我们雇佣了一只大猴子...
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • %@ page import=%的用法
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (2)STL算法之元素计数
  • (23)Linux的软硬连接
  • (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
  • (附源码)ssm学生管理系统 毕业设计 141543
  • (六)c52学习之旅-独立按键
  • (一) springboot详细介绍
  • (转) Face-Resources
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转)负载均衡,回话保持,cookie
  • ***原理与防范
  • .net mvc 获取url中controller和action
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .pings勒索病毒的威胁:如何应对.pings勒索病毒的突袭?
  • /usr/local/nginx/logs/nginx.pid failed (2: No such file or directory)
  • [<死锁专题>]
  • [android] 练习PopupWindow实现对话框
  • [android] 请求码和结果码的作用
  • [Angular 基础] - 指令(directives)