系统自带的图标很无趣?没事,用图片换个风格试试!!

请看代码:


        private static Cursor CreateMyCursor(BitmapSource source, int width, int height)
        {
            const int f = 4;
            var bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var pen = new System.Drawing.Pen(System.Drawing.Brushes.Black, 2.0F);
            g.DrawRectangle(pen, new Rectangle(f, f, width - 2 * f, width - 2 * f));
            g.Flush();
            g.Dispose();
            pen.Dispose();
            return BitmapCursor.CreateBmpCursor(bmp);
        }

        public class BitmapCursor : SafeHandle
        {
            public override bool IsInvalid
            {
                get
                {
                    return handle == (IntPtr)(-1);
                }
            }

            public static Cursor CreateBmpCursor(Bitmap cursorBitmap)
            {

                var c = new BitmapCursor(cursorBitmap);

                return CursorInteropHelper.Create(c);
            }

            protected BitmapCursor(Bitmap cursorBitmap)
                : base((IntPtr)(-1), true)
            {
                handle = cursorBitmap.GetHicon();
            }

            protected override bool ReleaseHandle()
            {
                bool result = DestroyIcon(handle);

                handle = (IntPtr)(-1);

                return result;
            }

            [DllImport("user32")]
            private static extern bool DestroyIcon(IntPtr hIcon);
        }

本文会经常更新,请阅读原文: https://huchengv5.gitee.io//post/WPF-%E5%A6%82%E4%BD%95%E9%80%9A%E8%BF%87%E4%BD%8D%E5%9B%BE%E5%88%9B%E5%BB%BA%E9%BC%A0%E6%A0%87%E6%8C%87%E9%92%88%E5%9B%BE%E6%A0%87.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名胡承(包含链接: https://huchengv5.gitee.io/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系