Exemple #1
0
        // Draws this image to a graphics object.  The drawing command originates on the graphics
        // object, but a graphics object generally has no idea how to render a given image.  So,
        // it passes the call to the actual image.  This version crops the image to the given
        // dimensions and allows the user to specify a rectangle within the image to draw.
        private void DrawIcon(IntPtr dc, Rectangle imageRect, Rectangle targetRect, bool stretch)
        {
            int imageX = 0;
            int imageY = 0;
            int imageWidth;
            int imageHeight;
            int targetX      = 0;
            int targetY      = 0;
            int targetWidth  = 0;
            int targetHeight = 0;

            Size cursorSize = Size;

            // Compute the dimensions of the icon if needed.
            if (!imageRect.IsEmpty)
            {
                imageX      = imageRect.X;
                imageY      = imageRect.Y;
                imageWidth  = imageRect.Width;
                imageHeight = imageRect.Height;
            }
            else
            {
                imageWidth  = cursorSize.Width;
                imageHeight = cursorSize.Height;
            }

            if (!targetRect.IsEmpty)
            {
                targetX      = targetRect.X;
                targetY      = targetRect.Y;
                targetWidth  = targetRect.Width;
                targetHeight = targetRect.Height;
            }
            else
            {
                targetWidth  = cursorSize.Width;
                targetHeight = cursorSize.Height;
            }

            int drawWidth, drawHeight;
            int clipWidth, clipHeight;

            if (stretch)
            {
                drawWidth  = cursorSize.Width * targetWidth / imageWidth;
                drawHeight = cursorSize.Height * targetHeight / imageHeight;
                clipWidth  = targetWidth;
                clipHeight = targetHeight;
            }
            else
            {
                drawWidth  = cursorSize.Width;
                drawHeight = cursorSize.Height;
                clipWidth  = targetWidth < imageWidth ? targetWidth : imageWidth;
                clipHeight = targetHeight < imageHeight ? targetHeight : imageHeight;
            }

            // The ROP is SRCCOPY, so we can be simple here and take
            // advantage of clipping regions.  Drawing the cursor
            // is merely a matter of offsetting and clipping.
            IntPtr hSaveRgn = SaveClipRgn(dc);

            try
            {
                SafeNativeMethods.IntersectClipRect(new HandleRef(this, dc), targetX, targetY, targetX + clipWidth, targetY + clipHeight);
                SafeNativeMethods.DrawIconEx(new HandleRef(null, dc),
                                             targetX - imageX,
                                             targetY - imageY,
                                             new HandleRef(this, _handle),
                                             drawWidth,
                                             drawHeight,
                                             0,
                                             NativeMethods.NullHandleRef,
                                             SafeNativeMethods.DI_NORMAL);
            }
            finally
            {
                RestoreClipRgn(dc, hSaveRgn);
            }
        }
        private void DrawIcon(IntPtr dc, Rectangle imageRect, Rectangle targetRect, bool stretch)
        {
            int width;
            int height;
            int num9;
            int num10;
            int num11;
            int num12;
            int x    = 0;
            int y    = 0;
            int num5 = 0;
            int num6 = 0;
            int num7 = 0;
            int num8 = 0;

            System.Drawing.Size size = this.Size;
            if (!imageRect.IsEmpty)
            {
                x      = imageRect.X;
                y      = imageRect.Y;
                width  = imageRect.Width;
                height = imageRect.Height;
            }
            else
            {
                width  = size.Width;
                height = size.Height;
            }
            if (!targetRect.IsEmpty)
            {
                num5 = targetRect.X;
                num6 = targetRect.Y;
                num7 = targetRect.Width;
                num8 = targetRect.Height;
            }
            else
            {
                num7 = size.Width;
                num8 = size.Height;
            }
            if (stretch)
            {
                num9  = (size.Width * num7) / width;
                num10 = (size.Height * num8) / height;
                num11 = num7;
                num12 = num8;
            }
            else
            {
                num9  = size.Width;
                num10 = size.Height;
                num11 = (num7 < width) ? num7 : width;
                num12 = (num8 < height) ? num8 : height;
            }
            IntPtr hRgn = SafeNativeMethods.SaveClipRgn(dc);

            try
            {
                SafeNativeMethods.IntersectClipRect(new HandleRef(this, dc), num5, num6, num5 + num11, num6 + num12);
                SafeNativeMethods.DrawIconEx(new HandleRef(null, dc), num5 - x, num6 - y, new HandleRef(this, this.handle), num9, num10, 0, System.Drawing.NativeMethods.NullHandleRef, 3);
            }
            finally
            {
                SafeNativeMethods.RestoreClipRgn(dc, hRgn);
            }
        }