Create a semi-transparent cursor from an image

后端 未结 4 2226
深忆病人
深忆病人 2021-02-14 23:12

Is it possible to create a cursor from an image and have it be semi-transparent?

I\'m currently taking a custom image and overylaying the mouse cursor image. It would b

4条回答
  •  甜味超标
    2021-02-14 23:59

    I've tried following example, and it was working fine...

        public struct IconInfo
        {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }
    
    
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
    
        [DllImport("user32.dll")]
        public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
    
        public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
        {
            IntPtr ptr = bmp.GetHicon();
            IconInfo tmp = new IconInfo();
            GetIconInfo(ptr, ref tmp);
            tmp.xHotspot = xHotSpot;
            tmp.yHotspot = yHotSpot;
            tmp.fIcon = false;
            ptr = CreateIconIndirect(ref tmp);
            return new Cursor(ptr);
        }
    

    And i've put this on button click event (you can call from where you like):

    Bitmap b = new Bitmap("D:/Up.png");
    this.Cursor = CreateCursor(b, 5, 5);
    

    And the Up.png image is saved with 75% opacity in AdobePhotoshop.

提交回复
热议问题