C# 自定义鼠标样式

2021/9/27 17:12:42

本文主要是介绍C# 自定义鼠标样式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

这里调用系统 API 的方式。

▲ 样式效果

先准备好鼠标样式cur文件。

▲ 鼠标样式文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MouseSt
{
    using System.Runtime.InteropServices;
    using System.Reflection;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Cursor myCursor = new Cursor(Cursor.Current.Handle);
            IntPtr colrCursorHandle = MouseStye.LoadCursorFromFile("hand.cur");
            myCursor.GetType().InvokeMember("handle", BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.SetField, null, myCursor, new object[] { colrCursorHandle });
            this.pictureBox1.Cursor = myCursor; // pictureBox区域

            Cursor myCursor1 = new Cursor(Cursor.Current.Handle);
            IntPtr colrCursorHandle1 = MouseStye.LoadCursorFromFile("pen.cur");
            myCursor.GetType().InvokeMember("handle", BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.SetField, null, myCursor1, new object[] { colrCursorHandle1 });
            this.pictureBox2.Cursor = myCursor1;
        }
    }

    class MouseStye
    {
        [DllImport("user32.dll")]
        public static extern IntPtr LoadCursorFromFile(string fileName);
        [DllImport("user32.dll")]
        public static extern IntPtr SetCursor(IntPtr cursorHandle);
        [DllImport("user32.dll")]
        public static extern uint DestroyCursor(IntPtr cursorHandle);
    }
}

InvokeMember 第二个选项参数具体还没有深究,先照着写吧。



参考:
C# winForm 自定义鼠标样式的两种方法:
https://www.cnblogs.com/tianguook/archive/2010/08/03/1791572.html



这篇关于C# 自定义鼠标样式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程