最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

WinForm程序

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

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

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

創(chuàng)建一個(gè)自定義光標(biāo),首先需要定義需要一個(gè)光標(biāo)結(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兩個(gè)函數(shù)來合成光標(biāo)。完整代碼如下: 

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

測(cè)試代碼為:

    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程序是非常類似的,一方面,它也可以通過光標(biāo)文件來實(shí)現(xiàn)寫入Cursor屬性來自定義光標(biāo)文件。

至于自己繪制光標(biāo),上面的代碼基本上也是可以復(fù)用的,不過相對(duì)的要重新封裝一下,完整代碼如下: 

    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的

封裝之后,是可以直接傳入U(xiǎn)IElement作為自繪制的光標(biāo)的,得益于WPF的強(qiáng)大繪圖功能,是可以非常容易的繪制漂亮的光標(biāo)的。測(cè)試代碼如下:

this.Cursor= CursorHelper.CreateCursor(new UserControl1());

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

相關(guān)文章

  • C#中的GDI+圖像編程詳解

    C#中的GDI+圖像編程詳解

    本文詳細(xì)講解了C#中的GDI+圖像編程,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#實(shí)現(xiàn)單例模式的多種方式

    C#實(shí)現(xiàn)單例模式的多種方式

    這篇文章介紹了C#實(shí)現(xiàn)單例模式的多種方式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)

    C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)

    這篇文章主要介紹了C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)在C#中,@符號(hào)不僅可以加在字符串常量之前,使字符串不作轉(zhuǎn)義之用,還可以加在變量名之前,使變量名與關(guān)鍵字不沖突,這種用法稱為“逐字標(biāo)識(shí)符”,需要的朋友可以參考下
    2015-06-06
  • C#批量插入數(shù)據(jù)到sqlserver的方法詳解

    C#批量插入數(shù)據(jù)到sqlserver的方法詳解

    這篇文章主要為大家詳細(xì)介紹了C#中四種可以批量插入數(shù)據(jù)到sqlserver的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2025-02-02
  • 在C#中如何使用JSON

    在C#中如何使用JSON

    JSON(JavaScript?Object?Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,具有易讀、易解析的特點(diǎn),廣泛用于API數(shù)據(jù)交互、配置文件等,本文介紹在C#中如何使用JSON,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖

    Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#使用udp如何實(shí)現(xiàn)消息的接收和發(fā)送

    C#使用udp如何實(shí)現(xiàn)消息的接收和發(fā)送

    這篇文章主要介紹了C#使用udp如何實(shí)現(xiàn)消息的接收和發(fā)送問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法

    C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法

    這篇文章主要介紹了C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-03-03
  • Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割

    Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割

    這篇文章主要為大家詳細(xì)介紹了Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C#匿名函數(shù)和匿名方法的使用

    C#匿名函數(shù)和匿名方法的使用

    本文主要介紹了C#匿名函數(shù)和匿名方法的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03

最新評(píng)論

黑龙江省| 甘孜| 寿宁县| 三原县| 日照市| 芦溪县| 庆元县| 合山市| 兖州市| 武宁县| 阿鲁科尔沁旗| 松溪县| 公安县| 图片| 万盛区| 岑巩县| 宜黄县| 东乡县| 昂仁县| 金塔县| 松滋市| 洪湖市| 盐池县| 星子县| 轮台县| 阿瓦提县| 桃源县| 大余县| 东港市| 永善县| 临泉县| 互助| 蕉岭县| 左云县| 监利县| 陇西县| 和平区| 宁津县| 吉木萨尔县| 龙井市| 炎陵县|