您现在的位置是:群英 > 开发技术 > 编程语言
如何在.net桌面程序中自定义鼠标光标,有哪些方法
Admin发表于 2022-11-01 17:32:50429 次浏览
今天这篇给大家分享的知识是“如何在.net桌面程序中自定义鼠标光标,有哪些方法”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,因此分享发大家做个参考,下文的讲解详细,步骤过程清晰,希望这篇“如何在.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两个函数来合成光标。完整代码如下: 

    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 IntPtr CreateIconIndirect(ref IconInfo icon);


            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
        }

        public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
        {
            var icon = new NativeMethods.IconInfo
            {
                xHotspot = xHotSpot,
                yHotspot = yHotSpot,
                fIcon = false
            };

            NativeMethods.GetIconInfo(bmp.GetHicon(), ref icon);
            return new Cursor(NativeMethods.CreateIconIndirect(ref icon));
        }
    }

测试代码为:

    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属性来自定义光标文件。

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

    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);
                }
            }
        }
    }

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

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

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



以上就是关于如何在.net桌面程序中自定义鼠标光标,有哪些方法的介绍啦,需要的朋友可以参考上述内容,希望对大家有帮助,想要了解更多,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-08-31 17:22:15 
摘要:本篇文章给大家带来了关于javascript的相关知识,其中主要介绍了querySelectorAll方法的使用,该方法可以返回文档中匹配指定 CSS 选择器的所有元素,返回 NodeList 对象,下面一起来看一下,希望对大家有帮助。
2022-09-08 18:02:21 
摘要:用jquery前需要导入。两种导入方式:1、在jquery官网下载jquery文件到本地,在HTML文档中使用script标签导入本地jquery文件,语法“<script src="本地jquery文件路径"></script>”;2、在HTML中使用script标签导入在线jquery文件,语法“<script src="在线jquery文件网址"></script>”。
2022-05-13 17:43:36 
摘要:这篇文章主要给大家介绍了关于laravel7学习之无限级分类的最新实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部