示例#1
0
        // this code is adapted from Icon.GetIconSize please take this into account when changing this
        private Size GetIconSize(IntPtr iconHandle)
        {
            Size iconSize = Size;

            NativeMethods.ICONINFO info = new NativeMethods.ICONINFO();
            SafeNativeMethods.GetIconInfo(new HandleRef(this, iconHandle), info);
            NativeMethods.BITMAP bmp = new NativeMethods.BITMAP();

            if (info.hbmColor != IntPtr.Zero)
            {
                UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmColor), Marshal.SizeOf(typeof(NativeMethods.BITMAP)), bmp);
                SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmColor));
                iconSize = new Size(bmp.bmWidth, bmp.bmHeight);
            }
            else if (info.hbmMask != IntPtr.Zero)
            {
                UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmMask), Marshal.SizeOf(typeof(NativeMethods.BITMAP)), bmp);
                iconSize = new Size(bmp.bmWidth, bmp.bmHeight / 2);
            }

            if (info.hbmMask != IntPtr.Zero)
            {
                SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmMask));
            }
            return(iconSize);
        }
示例#2
0
        // this code is adapted from Icon.GetIconSize please take this into account when changing this
        private Size GetIconSize(IntPtr iconHandle)
        {
            Size iconSize = Size;

            NativeMethods.ICONINFO info = default;
            SafeNativeMethods.GetIconInfo(new HandleRef(this, iconHandle), ref info);
            NativeMethods.BITMAP bmp = new NativeMethods.BITMAP();

            if (info.hbmColor != IntPtr.Zero)
            {
                UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmColor), Marshal.SizeOf <NativeMethods.BITMAP>(), bmp);
                Interop.Gdi32.DeleteObject(info.hbmColor);
                iconSize = new Size(bmp.bmWidth, bmp.bmHeight);
            }
            else if (info.hbmMask != IntPtr.Zero)
            {
                UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmMask), Marshal.SizeOf <NativeMethods.BITMAP>(), bmp);
                iconSize = new Size(bmp.bmWidth, bmp.bmHeight / 2);
            }

            if (info.hbmMask != IntPtr.Zero)
            {
                Interop.Gdi32.DeleteObject(info.hbmMask);
            }
            return(iconSize);
        }
示例#3
0
        public static Size GetSize(this System.Windows.Forms.Cursor cursor)
        {
            var size = Size.Empty;
            var info = new NativeMethods.ICONINFO();

            NativeMethods.GetIconInfo(cursor.Handle, info);
            if (info.hbmColor != IntPtr.Zero)
            {
                using (var bm = Bitmap.FromHbitmap(info.hbmColor))
                    size = bm.Size;
            }
            else if (info.hbmMask != IntPtr.Zero)
            {
                using (var bm = Bitmap.FromHbitmap(info.hbmMask))
                    size = new Size(bm.Width, bm.Height / 2);
            }
            return(size);
        }
示例#4
0
 public static extern bool GetIconInfo(HandleRef hIcon, [In, Out] NativeMethods.ICONINFO info);
示例#5
0
文件: IconHelper.cs 项目: hughbe/wpf
        // Also used by PenCursorManager
        // Creates a 32 bit per pixel Icon or cursor.  This code is moved from framework\ms\internal\ink\pencursormanager.cs
        internal static NativeMethods.IconHandle CreateIconCursor(
            byte[] colorArray,
            int width,
            int height,
            int xHotspot,
            int yHotspot,
            bool isIcon)
        {
            //   1. We are going to generate a WIN32 color bitmap which represents the color cursor.
            //   2. Then we need to create a monochrome bitmap which is used as the cursor mask.
            //   3. At last we create a WIN32 HICON from the above two bitmaps
            NativeMethods.BitmapHandle colorBitmap = null;
            NativeMethods.BitmapHandle maskBitmap  = null;

            try
            {
                // 1) Create the color bitmap using colorArray
                // Fill in the header information
                NativeMethods.BITMAPINFO bi = new NativeMethods.BITMAPINFO(
                    width,                                      // width
                    -height,                                    // A negative value indicates the bitmap is top-down DIB
                    32                                          // biBitCount
                    );
                bi.bmiHeader_biCompression = NativeMethods.BI_RGB;

                IntPtr bits = IntPtr.Zero;
                colorBitmap = MS.Win32.UnsafeNativeMethods.CreateDIBSection(
                    new HandleRef(null, IntPtr.Zero),                // A device context. Pass null in if no DIB_PAL_COLORS is used.
                    ref bi,                                          // A BITMAPINFO structure which specifies the dimensions and colors.
                    NativeMethods.DIB_RGB_COLORS,                    // Specifies the type of data contained in the bmiColors array member of the BITMAPINFO structure
                    ref bits,                                        // An out Pointer to a variable that receives a pointer to the location of the DIB bit values
                    null,                                            // Handle to a file-mapping object that the function will use to create the DIB. This parameter can be null.
                    0                                                // dwOffset. This value is ignored if hSection is NULL
                    );

                if (colorBitmap.IsInvalid || bits == IntPtr.Zero)
                {
                    // Note we will release the GDI resources in the finally block.
                    return(NativeMethods.IconHandle.GetInvalidIcon());
                }

                // Copy the color bits to the win32 bitmap
                Marshal.Copy(colorArray, 0, bits, colorArray.Length);


                // 2) Now create the mask bitmap which is monochrome
                byte[] maskArray = GenerateMaskArray(width, height, colorArray);
                Invariant.Assert(maskArray != null);

                maskBitmap = UnsafeNativeMethods.CreateBitmap(width, height, 1, 1, maskArray);
                if (maskBitmap.IsInvalid)
                {
                    // Note we will release the GDI resources in the finally block.
                    return(NativeMethods.IconHandle.GetInvalidIcon());
                }

                // Now create HICON from two bitmaps.
                NativeMethods.ICONINFO iconInfo = new NativeMethods.ICONINFO();
                iconInfo.fIcon    = isIcon;         // fIcon == ture means creating an Icon, otherwise Cursor
                iconInfo.xHotspot = xHotspot;
                iconInfo.yHotspot = yHotspot;
                iconInfo.hbmMask  = maskBitmap;
                iconInfo.hbmColor = colorBitmap;

                return(UnsafeNativeMethods.CreateIconIndirect(iconInfo));
            }
            finally
            {
                if (colorBitmap != null)
                {
                    colorBitmap.Dispose();
                    colorBitmap = null;
                }

                if (maskBitmap != null)
                {
                    maskBitmap.Dispose();
                    maskBitmap = null;
                }
            }
        }
示例#6
0
        /// <summary>
        /// Converts icon to bitmap (with alpha channel if necessary).
        /// </summary>
        /// <param name="iconHandle">Source icon handle.</param>
        /// <returns>Result bitmap.</returns>
        public static Aurigma.GraphicsMill.Bitmap IconToAlphaBitmap(System.IntPtr iconHandle)
        {
            NativeMethods.ICONINFO ii = new NativeMethods.ICONINFO();
            NativeMethods.GetIconInfo(iconHandle, out ii);
            System.Drawing.Bitmap bmp = System.Drawing.Bitmap.FromHbitmap(ii.hbmColor);

            NativeMethods.DeleteObject(ii.hbmColor);
            NativeMethods.DeleteObject(ii.hbmMask);

            if (System.Drawing.Bitmap.GetPixelFormatSize(bmp.PixelFormat) < 32)
            {
                bmp.Dispose();
                return((Aurigma.GraphicsMill.Bitmap)System.Drawing.Bitmap.FromHicon(iconHandle));
            }

            System.Drawing.Bitmap dstBitmap = null;
            System.Drawing.Bitmap result    = null;
            bool isAlphaBitmap = false;

            System.Drawing.Imaging.BitmapData bmData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
            try
            {
                dstBitmap = new System.Drawing.Bitmap(bmData.Width, bmData.Height, bmData.Stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, bmData.Scan0);
                for (int y = 0; y < bmData.Height; y++)
                {
                    for (int x = 0; x < bmData.Width; x++)
                    {
                        System.Drawing.Color pixelColor = System.Drawing.Color.FromArgb(System.Runtime.InteropServices.Marshal.ReadInt32(bmData.Scan0, (bmData.Stride * y) + (4 * x)));
                        if (pixelColor.A > 0 & pixelColor.A < 255)
                        {
                            isAlphaBitmap = true;
                            break;
                        }
                    }
                    if (isAlphaBitmap)
                    {
                        break;
                    }
                }

                if (isAlphaBitmap)
                {
                    result = new System.Drawing.Bitmap(dstBitmap);
                }
                else
                {
                    result = System.Drawing.Bitmap.FromHicon(iconHandle);
                }
            }
            finally
            {
                if (dstBitmap != null)
                {
                    dstBitmap.Dispose();
                }

                bmp.UnlockBits(bmData);

                if (bmp != null)
                {
                    bmp.Dispose();
                }
            }

            return(new Aurigma.GraphicsMill.Bitmap(result));
        }