欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)自定義光標(biāo)并動態(tài)切換

 更新時間:2022年07月29日 10:50:16   作者:唐宋元明清2188  
這篇文章主要為大家詳細(xì)介紹了如何利用C#語言實(shí)現(xiàn)自定義光標(biāo)、并動態(tài)切換光標(biāo)類型,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

系統(tǒng)有很多光標(biāo)類型 :Cursors 類 (System.Windows.Input) | Microsoft Docs

本章介紹如何自定義光標(biāo)、并動態(tài)切換光標(biāo)類型。

動態(tài)切換光標(biāo)類型

以白板書寫為例:鼠標(biāo)操作時,Cursor為紅點(diǎn);觸摸時,Cursor為空;

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

設(shè)置光標(biāo)顯示:

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

觸摸書寫時,會有個默認(rèn)光標(biāo),所以此處把觸摸時的光標(biāo)置空Cursors.None。

Mouse.UpdateCursor()能強(qiáng)制更新光標(biāo)。當(dāng)然,不調(diào)用這個更新方法肉眼其實(shí)也看不出啥。。。

光標(biāo)切換效果如上,前面一段是用鼠標(biāo)書寫,后面是觸摸書寫,光標(biāo)類型有切換。紅點(diǎn)光標(biāo)自定義方案見下方。

自定義光標(biāo)

自定義一個純色的圓形光標(biāo):

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來生成光標(biāo):

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

到此這篇關(guān)于C#實(shí)現(xiàn)自定義光標(biāo)并動態(tài)切換的文章就介紹到這了,更多相關(guān)C#光標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論