首頁 > 軟體

C#實現自定義遊標並動態切換

2022-07-29 14:03:24

系統有很多遊標型別 :Cursors 類 (System.Windows.Input) | Microsoft Docs

本章介紹如何自定義遊標、並動態切換遊標型別。

動態切換遊標型別

以白板書寫為例:滑鼠操作時,Cursor為紅點;觸控時,Cursor為空;

public MainWindow()
    {
        InitializeComponent();
        MouseEnter += (s, e) =>
        {
            ShowMouseCursor(e);
        };
        MouseMove += (s, e) =>
        {
            ShowMouseCursor(e);
        };
        StylusMove += (s, e) =>
        {
            ShowNoneCursor();
        };
    }

設定遊標顯示:

private void ShowNoneCursor()
    {
        if (Cursor == Cursors.None)
        {
            return;
        }
        Cursor = Cursors.None;
        Mouse.UpdateCursor();
    }
    private void ShowMouseCursor(MouseEventArgs e)
    {
        if (e.StylusDevice != null && e.StylusDevice.Id > -1)
        {
            return;
        }
        if (Cursor == GetFillCursor())
        {
            return;
        }
        Cursor = GetFillCursor();
        Mouse.UpdateCursor();
    }
    private Cursor _fillCursor = null;
    private Cursor GetFillCursor()
    {
        return _fillCursor ?? (_fillCursor = CursorHelper.CreateFillCursor());
    }

觸控書寫時,會有個預設遊標,所以此處把觸控時的遊標置空Cursors.None。

Mouse.UpdateCursor()能強制更新遊標。當然,不呼叫這個更新方法肉眼其實也看不出啥。。。

遊標切換效果如上,前面一段是用滑鼠書寫,後面是觸控書寫,遊標型別有切換。紅點遊標自定義方案見下方。

自定義遊標

自定義一個純色的圓形遊標:

public static Cursor CreateFillCursor(int size = 24, Brush fillBrush = null)
    {
        int unitSize = size / 4;
        var bmp = new Bitmap(size, size);
        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.Clip = new Region(new Rectangle(0, 0, size, size));
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            using (var pen = new Pen(fillBrush ?? Brushes.Red, unitSize))
            {

                g.DrawEllipse(pen, new Rectangle(unitSize, unitSize, unitSize, unitSize));
            }
        }
        return BitmapCursor.CreateBmpCursor(bmp);
    }

也可以通過圖片資源BitmapSource來生成遊標:

public static Cursor CreateFromBitmapSource(BitmapSource source)
    {
        var bitmap = BitmapSourceToBitmap(source);
        return BitmapCursor.CreateBmpCursor(bitmap);
    }
    private static Bitmap BitmapSourceToBitmap(BitmapSource source)
    {
        using (var stream = new MemoryStream())
        {
            var e = new BmpBitmapEncoder();
            e.Frames.Add(BitmapFrame.Create(source));
            e.Save(stream);

            var bmp = new Bitmap(stream);

            return bmp;
        }
    }

BitmapCursor:

internal class BitmapCursor : SafeHandle
    {
        public override bool IsInvalid => 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);
    }

到此這篇關於C#實現自定義遊標並動態切換的文章就介紹到這了,更多相關C#遊標內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com