亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#對桌面應(yīng)用程序自定義鼠標光標

 更新時間:2022年06月27日 11:32:57   作者:天方  
這篇文章介紹了C#對桌面應(yīng)用程序自定義鼠標光標的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

有的時候,一個自定義的鼠標光標能給你的程序增色不少。本文這里介紹一下如何在.net桌面程序中自定義鼠標光標。由于.net的桌面程序分為WinForm和WPF兩種,這里分別介紹一下。

WinForm程序

對于WinForm程序,可以通過修改Control.Cursor屬性來實現(xiàn)光標的修改,如果我們有光標文件的話,可以直接通過如下代碼實現(xiàn)自定義光標:

this.Cursor = new Cursor("myCursor.cur");

但這種方式不是本文介紹的重點,本文主要介紹如何自己繪制光標,這樣則具有更多的可控性和靈活性。

創(chuàng)建一個自定義光標,首先需要定義需要一個光標結(jié)構(gòu) ICONINFO ,它的.net版本如下:

    public struct IconInfo
    {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }

然后通過GetIconInfo and CreateIconIndirect兩個函數(shù)來合成光標。完整代碼如下: 

    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程序是非常類似的,一方面,它也可以通過光標文件來實現(xiàn)寫入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());

到此這篇關(guān)于C#對桌面應(yīng)用程序自定義鼠標光標的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#編程實現(xiàn)獲取文件夾中所有文件的文件名

    C#編程實現(xiàn)獲取文件夾中所有文件的文件名

    這篇文章主要介紹了C#編程實現(xiàn)獲取文件夾中所有文件的文件名,可實現(xiàn)獲取特定目錄下制定類型文件名稱的功能,涉及C#針對文件與目錄的遍歷、查詢等操作相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Unity3D實現(xiàn)飛機大戰(zhàn)游戲(2)

    Unity3D實現(xiàn)飛機大戰(zhàn)游戲(2)

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)飛機大戰(zhàn)游戲的第二部分,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C#中int[][]與int[,]的使用與區(qū)別

    C#中int[][]與int[,]的使用與區(qū)別

    本文主要介紹了C#中int[][]與int[,]的使用與區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • WPF實現(xiàn)監(jiān)聽快捷鍵的方式分享

    WPF實現(xiàn)監(jiān)聽快捷鍵的方式分享

    這篇文章主要為大家詳細介紹了WPF實現(xiàn)監(jiān)聽快捷鍵的幾種方式,文中的示例代碼講解詳細,具有一定的借鑒與學習價值,需要的可以了解一下
    2023-03-03
  • 深入解析C#設(shè)計模式編程中對建造者模式的運用

    深入解析C#設(shè)計模式編程中對建造者模式的運用

    這篇文章主要介紹了C#設(shè)計模式編程中對建造者模式的運用,文中還介紹了在.NET框架下建造者模式編寫思路的實現(xiàn),需要的朋友可以參考下
    2016-02-02
  • C#實現(xiàn)會移動的文字效果

    C#實現(xiàn)會移動的文字效果

    這篇文章主要為大家詳細介紹了C#實現(xiàn)會移動的文字效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 事務(wù)在c#中的使用

    事務(wù)在c#中的使用

    這篇文章介紹了事務(wù)在c#中的使用,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • 如何通過IL了解C#類的構(gòu)造函數(shù)淺析

    如何通過IL了解C#類的構(gòu)造函數(shù)淺析

    這篇文章主要給大家介紹了關(guān)于如何通過IL了解C#類的構(gòu)造函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • c#中Linq to Sql 增刪除的實例

    c#中Linq to Sql 增刪除的實例

    c#中Linq to Sql 增刪除的實例,需要的朋友可以參考一下
    2013-05-05
  • C#字符串中去除多余的空格保留一個的實例

    C#字符串中去除多余的空格保留一個的實例

    下面小編就為大家分享一篇C#字符串中去除多余的空格保留一個的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12

最新評論