C# Form实现自定义光标

WinForm代码如下:1usingSystem;2usingSystem.Reflection;3usingSystem.Runtime.InteropServices;4us

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

WinForm代码如下:

 1 using System;
 2 using System.Reflection;
 3 using System.Runtime.InteropServices;
 4 using System.Windows.Forms;
 5 
 6 namespace KeleyiColorCursor
 7 {
 8 public partial class CursorForm : Form
 9 {
10 [DllImport("user32.dll")]
11 public static extern IntPtr LoadCursorFromFile(string fileName);
12 
13 [DllImport("user32.dll")]
14 public static extern IntPtr SetCursor(IntPtr cursorHandle);
15 
16 [DllImport("user32.dll")]
17 public static extern uint DestroyCursor(IntPtr cursorHandle);
18 
19 public CursorForm()
20 {
21 InitializeComponent();
22 Cursor myCursor = new Cursor(Cursor.Current.Handle);
23 string m_cursorFile = @"D:\keleyi\pen_r.cur";
24 
25 //pen_r.cur文件下载地址:
26 //http://down.keleyi.com/files/cursors/pen_r.cur
27 
28 
29 //或者设为动态光标
30 // m_cursorFile = @"D:\keleyi\dinosau2.ani";
31 //下载地址: http://down.keleyi.com/files/cursors/dinosau2.ani
32 
33 IntPtr colorCursorHandle = LoadCursorFromFile(m_cursorFile);
34 
35 
36 myCursor.GetType().InvokeMember("handle", BindingFlags.Public |
37 BindingFlags.NonPublic | BindingFlags.Instance |
38 BindingFlags.SetField, null, myCursor,
39 new object[] { colorCursorHandle });
40 this.Cursor = myCursor;
41 }
42 }
43 }

 

运行后,如果把光标移入窗体,光标消失了,那是因为没有下载光标文件到特定文件夹。
光标文件下载:http://down.keleyi.com/files/cursors/dinosau2.ani
http://down.keleyi.com/files/cursors/pen_r.cur

转载自:http://keleyi.com/a/bjad/vvks7sw7.htm

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120735.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号