WPF中自定义鼠标

参考自:https://blog.csdn.net/hjnth/article/details/56483935

!!另外可以通过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);

                    }

                }

            }

        }



命名空间:using System.Runtime.InteropServices;

using System.Security.Permissions;

using Microsoft.Win32.SafeHandles;

using System.IO;

using System.Windows.Interop;



在MainWindow中调用此函数:

public MainWindow()

        {

            InitializeComponent();

            this.Cursor = CursorHelper.CreateCursor(elips);//elips是自定义控件的名字

        }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。