コード例 #1
0
        internal static IconHandle CreateIconCursor(byte[] colorArray, int width, int height, int xHotspot, int yHotspot, bool isIcon)
        {
            BitmapHandle colorBitmap = null;
            BitmapHandle maskBitmap  = null;

            try
            {
                var bi = new InteropValues.BITMAPINFO(width, -height, 32)
                {
                    biCompression = InteropValues.BI_RGB
                };

                var bits = IntPtr.Zero;
                colorBitmap = InteropMethods.CreateDIBSection(new HandleRef(null, IntPtr.Zero), ref bi, InteropValues.DIB_RGB_COLORS, ref bits, null, 0);

                if (colorBitmap.IsInvalid || bits == IntPtr.Zero)
                {
                    return(IconHandle.GetInvalidIcon());
                }

                Marshal.Copy(colorArray, 0, bits, colorArray.Length);
                var maskArray = GenerateMaskArray(width, height, colorArray);

                maskBitmap = InteropMethods.CreateBitmap(width, height, 1, 1, maskArray);
                if (maskBitmap.IsInvalid)
                {
                    return(IconHandle.GetInvalidIcon());
                }

                var iconInfo = new InteropValues.ICONINFO
                {
                    fIcon    = isIcon,
                    xHotspot = xHotspot,
                    yHotspot = yHotspot,
                    hbmMask  = maskBitmap,
                    hbmColor = colorBitmap
                };

                return(InteropMethods.CreateIconIndirect(iconInfo));
            }
            finally
            {
                colorBitmap?.Dispose();
                maskBitmap?.Dispose();
            }
        }