private BufferedGraphics AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
 {
     if (Interlocked.CompareExchange(ref this.busy, 1, 0) != 0)
     {
         return this.AllocBufferInTempManager(targetGraphics, targetDC, targetRectangle);
     }
     this.targetLoc = new Point(targetRectangle.X, targetRectangle.Y);
     try
     {
         Graphics graphics;
         if (targetGraphics != null)
         {
             IntPtr hdc = targetGraphics.GetHdc();
             try
             {
                 graphics = this.CreateBuffer(hdc, -this.targetLoc.X, -this.targetLoc.Y, targetRectangle.Width, targetRectangle.Height);
             }
             finally
             {
                 targetGraphics.ReleaseHdcInternal(hdc);
             }
         }
         else
         {
             graphics = this.CreateBuffer(targetDC, -this.targetLoc.X, -this.targetLoc.Y, targetRectangle.Width, targetRectangle.Height);
         }
         this.buffer = new BufferedGraphics(graphics, this, targetGraphics, targetDC, this.targetLoc, this.virtualSize);
     }
     catch
     {
         this.busy = 0;
         throw;
     }
     return this.buffer;
 }
Example #2
0
 /// <include file='doc\ImageList.uex' path='docs/doc[@for="ImageList.Draw2"]/*' />
 /// <devdoc>
 ///     Draw the image indicated by the given index using the location, size
 ///     and raster op code specified.  The image is stretched or compressed as
 ///     necessary to fit the bounds provided.
 /// </devdoc>
 public void Draw(Graphics g, int x, int y, int width, int height, int index) {
     if (index < 0 || index >= Images.Count)
         throw new ArgumentOutOfRangeException("index", SR.GetString(SR.InvalidArgument, "index", index.ToString(CultureInfo.CurrentCulture)));
     IntPtr dc = g.GetHdc();
     try {
         SafeNativeMethods.ImageList_DrawEx(new HandleRef(this, Handle), index, new HandleRef(g, dc), x, y,
                                width, height, NativeMethods.CLR_NONE, NativeMethods.CLR_NONE, NativeMethods.ILD_TRANSPARENT);
     }
     finally {
         g.ReleaseHdcInternal(dc);
     }
 }
 public virtual void PaintValue(object val, Graphics g, Rectangle rect, Rectangle clipRect, PaintValueFlags paintFlags)
 {
     string lastValueString;
     PropertyGridView gridEntryHost = this.GridEntryHost;
     int valuePaintIndent = 0;
     Color textColor = gridEntryHost.GetTextColor();
     if (this.ShouldRenderReadOnly)
     {
         textColor = this.GridEntryHost.GrayTextColor;
     }
     if ((paintFlags & PaintValueFlags.FetchValue) != PaintValueFlags.None)
     {
         if ((this.cacheItems != null) && this.cacheItems.useValueString)
         {
             lastValueString = this.cacheItems.lastValueString;
             val = this.cacheItems.lastValue;
         }
         else
         {
             val = this.PropertyValue;
             lastValueString = this.GetPropertyTextValue(val);
             if (this.cacheItems == null)
             {
                 this.cacheItems = new CacheItems();
             }
             this.cacheItems.lastValueString = lastValueString;
             this.cacheItems.useValueString = true;
             this.cacheItems.lastValueTextWidth = -1;
             this.cacheItems.lastValueFont = null;
             this.cacheItems.lastValue = val;
         }
     }
     else
     {
         lastValueString = this.GetPropertyTextValue(val);
     }
     Brush backgroundBrush = this.GetBackgroundBrush(g);
     if ((paintFlags & PaintValueFlags.DrawSelected) != PaintValueFlags.None)
     {
         backgroundBrush = SystemBrushes.Highlight;
         textColor = SystemColors.HighlightText;
     }
     Brush brush = backgroundBrush;
     g.FillRectangle(brush, clipRect);
     if (this.IsCustomPaint)
     {
         valuePaintIndent = gridEntryHost.GetValuePaintIndent();
         Rectangle a = new Rectangle(rect.X + 1, rect.Y + 1, gridEntryHost.GetValuePaintWidth(), gridEntryHost.GetGridEntryHeight() - 2);
         if (!Rectangle.Intersect(a, clipRect).IsEmpty)
         {
             System.Drawing.Design.UITypeEditor uITypeEditor = this.UITypeEditor;
             if (uITypeEditor != null)
             {
                 uITypeEditor.PaintValue(new PaintValueEventArgs(this, val, g, a));
             }
             a.Width--;
             a.Height--;
             g.DrawRectangle(SystemPens.WindowText, a);
         }
     }
     rect.X += valuePaintIndent + gridEntryHost.GetValueStringIndent();
     rect.Width -= valuePaintIndent + (2 * gridEntryHost.GetValueStringIndent());
     bool boldFont = ((paintFlags & PaintValueFlags.CheckShouldSerialize) != PaintValueFlags.None) && this.ShouldSerializePropertyValue();
     if ((lastValueString != null) && (lastValueString.Length > 0))
     {
         Font f = this.GetFont(boldFont);
         if (lastValueString.Length > 0x3e8)
         {
             lastValueString = lastValueString.Substring(0, 0x3e8);
         }
         int num2 = this.GetValueTextWidth(lastValueString, g, f);
         bool flag2 = false;
         if ((num2 >= rect.Width) || this.GetMultipleLines(lastValueString))
         {
             flag2 = true;
         }
         if (!Rectangle.Intersect(rect, clipRect).IsEmpty)
         {
             if ((paintFlags & PaintValueFlags.PaintInPlace) != PaintValueFlags.None)
             {
                 rect.Offset(1, 2);
             }
             else
             {
                 rect.Offset(1, 1);
             }
             Matrix transform = g.Transform;
             IntPtr hdc = g.GetHdc();
             IntNativeMethods.RECT lpRect = IntNativeMethods.RECT.FromXYWH((rect.X + ((int) transform.OffsetX)) + 2, (rect.Y + ((int) transform.OffsetY)) - 1, rect.Width - 4, rect.Height);
             IntPtr hfont = this.GetHfont(boldFont);
             int crColor = 0;
             int clr = 0;
             Color color2 = ((paintFlags & PaintValueFlags.DrawSelected) != PaintValueFlags.None) ? SystemColors.Highlight : this.GridEntryHost.BackColor;
             try
             {
                 crColor = System.Windows.Forms.SafeNativeMethods.SetTextColor(new HandleRef(g, hdc), System.Windows.Forms.SafeNativeMethods.RGBToCOLORREF(textColor.ToArgb()));
                 clr = System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(g, hdc), System.Windows.Forms.SafeNativeMethods.RGBToCOLORREF(color2.ToArgb()));
                 hfont = System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(g, hdc), new HandleRef(null, hfont));
                 int nFormat = 0x2960;
                 if (gridEntryHost.DrawValuesRightToLeft)
                 {
                     nFormat |= 0x20002;
                 }
                 if (this.ShouldRenderPassword)
                 {
                     if (passwordReplaceChar == '\0')
                     {
                         if (Environment.OSVersion.Version.Major > 4)
                         {
                             passwordReplaceChar = '●';
                         }
                         else
                         {
                             passwordReplaceChar = '*';
                         }
                     }
                     lastValueString = new string(passwordReplaceChar, lastValueString.Length);
                 }
                 IntUnsafeNativeMethods.DrawText(new HandleRef(g, hdc), lastValueString, ref lpRect, nFormat);
             }
             finally
             {
                 System.Windows.Forms.SafeNativeMethods.SetTextColor(new HandleRef(g, hdc), crColor);
                 System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(g, hdc), clr);
                 hfont = System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(g, hdc), new HandleRef(null, hfont));
                 g.ReleaseHdcInternal(hdc);
             }
             if (flag2)
             {
                 this.ValueToolTipLocation = new Point(rect.X + 2, rect.Y - 1);
             }
             else
             {
                 this.ValueToolTipLocation = InvalidPoint;
             }
         }
     }
 }
        private BufferedGraphics AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle) {

            int oldBusy = Interlocked.CompareExchange(ref busy, BUFFER_BUSY_PAINTING, BUFFER_FREE);

            // In the case were we have contention on the buffer - i.e. two threads 
            // trying to use the buffer at the same time, we just create a temp 
            // buffermanager and have the buffer dispose of it when it is done.
            //
            if (oldBusy != BUFFER_FREE) {
                Debug.WriteLineIf(DoubleBuffering.TraceWarning, "Attempt to have two buffers for a buffer manager... allocating temp buffer manager");
                return AllocBufferInTempManager(targetGraphics, targetDC, targetRectangle);
            }

            #if DEBUG
            if (DoubleBuffering.TraceVerbose) {
                stackAtBusy = new StackTrace().ToString();
            }
            #endif

            Graphics surface;
            this.targetLoc = new Point(targetRectangle.X, targetRectangle.Y);

            try {
                if (targetGraphics != null) {
                    IntPtr destDc = targetGraphics.GetHdc();
                    try {
                        surface = CreateBuffer(destDc, -targetLoc.X, -targetLoc.Y, targetRectangle.Width, targetRectangle.Height);
                    }
                    finally {
                        targetGraphics.ReleaseHdcInternal(destDc);
                    }
                }
                else {
                    surface = CreateBuffer(targetDC, -targetLoc.X, -targetLoc.Y, targetRectangle.Width, targetRectangle.Height);
                }

                this.buffer = new BufferedGraphics(surface, this, targetGraphics, targetDC, targetLoc, virtualSize);
            }
            catch {
                this.busy = BUFFER_FREE; // free the buffer so it can be disposed.
                throw;
            }
            return this.buffer;
        }
Example #5
0
        /// <devdoc>
        ///     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.
        /// </devdoc>
        // This method is way more powerful than what we expose, but I'll leave it in place.
        private void DrawImageCore(Graphics graphics, Rectangle imageRect, Rectangle targetRect, bool stretch) {
            // Support GDI+ Translate method
            targetRect.X += (int) graphics.Transform.OffsetX;
            targetRect.Y += (int) graphics.Transform.OffsetY;

            int rop = 0xcc0020; // RasterOp.SOURCE.GetRop();
            IntPtr dc = graphics.GetHdc();

            try { // want finally clause to release dc
                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) {
                    // Short circuit the simple case of blasting an icon to the
                    // screen
                    //
                    if (targetWidth == imageWidth && targetHeight == imageHeight
                        && imageX == 0 && imageY == 0 && rop == NativeMethods.SRCCOPY
                        && imageWidth == cursorSize.Width && imageHeight == cursorSize.Height) {
                        SafeNativeMethods.DrawIcon(new HandleRef(graphics, dc), targetX, targetY, new HandleRef(this, handle));
                        return;
                    }

                    drawWidth = cursorSize.Width * targetWidth / imageWidth;
                    drawHeight = cursorSize.Height * targetHeight / imageHeight;
                    clipWidth = targetWidth;
                    clipHeight = targetHeight;
                }
                else {
                    // Short circuit the simple case of blasting an icon to the
                    // screen
                    //
                    if (imageX == 0 && imageY == 0 && rop == NativeMethods.SRCCOPY
                        && cursorSize.Width <= targetWidth && cursorSize.Height <= targetHeight
                        && cursorSize.Width == imageWidth && cursorSize.Height == imageHeight) {
                        SafeNativeMethods.DrawIcon(new HandleRef(graphics, dc), targetX, targetY, new HandleRef(this, handle));
                        return;
                    }

                    drawWidth = cursorSize.Width;
                    drawHeight = cursorSize.Height;
                    clipWidth = targetWidth < imageWidth ? targetWidth : imageWidth;
                    clipHeight = targetHeight < imageHeight ? targetHeight : imageHeight;
                }

                if (rop == NativeMethods.SRCCOPY) {
                    // 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.
                    //
                    SafeNativeMethods.IntersectClipRect(new HandleRef(this, Handle), targetX, targetY, targetX+clipWidth, targetY+clipHeight);
                    SafeNativeMethods.DrawIconEx(new HandleRef(graphics, dc), targetX - imageX, targetY - imageY,
                                       new HandleRef(this, handle), drawWidth, drawHeight, 0, NativeMethods.NullHandleRef, NativeMethods.DI_NORMAL);
                    // Let GDI+ restore clipping
                    return;
                }

                Debug.Fail("Cursor.Draw does not support raster ops.  How did you even pass one in?");
            }
            finally {
                graphics.ReleaseHdcInternal(dc);
            }
        }
Example #6
0
        public void Render(Graphics target) {
            if (target != null) {
                IntPtr targetDC = target.GetHdc();

                try {
                    RenderInternal(new HandleRef(target, targetDC), this);
                }
                finally {
                    target.ReleaseHdcInternal(targetDC);
                }
            }
        }
 private void DrawImageCore(Graphics graphics, Rectangle imageRect, Rectangle targetRect, bool stretch)
 {
     targetRect.X += (int) graphics.Transform.OffsetX;
     targetRect.Y += (int) graphics.Transform.OffsetY;
     int num = 0xcc0020;
     IntPtr hdc = graphics.GetHdc();
     try
     {
         int width;
         int height;
         int num10;
         int num11;
         int num12;
         int num13;
         int num2 = 0;
         int num3 = 0;
         int x = 0;
         int y = 0;
         int num8 = 0;
         int num9 = 0;
         System.Drawing.Size size = this.Size;
         if (!imageRect.IsEmpty)
         {
             num2 = imageRect.X;
             num3 = imageRect.Y;
             width = imageRect.Width;
             height = imageRect.Height;
         }
         else
         {
             width = size.Width;
             height = size.Height;
         }
         if (!targetRect.IsEmpty)
         {
             x = targetRect.X;
             y = targetRect.Y;
             num8 = targetRect.Width;
             num9 = targetRect.Height;
         }
         else
         {
             num8 = size.Width;
             num9 = size.Height;
         }
         if (stretch)
         {
             if ((((num8 == width) && (num9 == height)) && ((num2 == 0) && (num3 == 0))) && (((num == 0xcc0020) && (width == size.Width)) && (height == size.Height)))
             {
                 System.Windows.Forms.SafeNativeMethods.DrawIcon(new HandleRef(graphics, hdc), x, y, new HandleRef(this, this.handle));
                 return;
             }
             num10 = (size.Width * num8) / width;
             num11 = (size.Height * num9) / height;
             num12 = num8;
             num13 = num9;
         }
         else
         {
             if ((((num2 == 0) && (num3 == 0)) && ((num == 0xcc0020) && (size.Width <= num8))) && (((size.Height <= num9) && (size.Width == width)) && (size.Height == height)))
             {
                 System.Windows.Forms.SafeNativeMethods.DrawIcon(new HandleRef(graphics, hdc), x, y, new HandleRef(this, this.handle));
                 return;
             }
             num10 = size.Width;
             num11 = size.Height;
             num12 = (num8 < width) ? num8 : width;
             num13 = (num9 < height) ? num9 : height;
         }
         if (num == 0xcc0020)
         {
             System.Windows.Forms.SafeNativeMethods.IntersectClipRect(new HandleRef(this, this.Handle), x, y, x + num12, y + num13);
             System.Windows.Forms.SafeNativeMethods.DrawIconEx(new HandleRef(graphics, hdc), x - num2, y - num3, new HandleRef(this, this.handle), num10, num11, 0, System.Windows.Forms.NativeMethods.NullHandleRef, 3);
         }
     }
     finally
     {
         graphics.ReleaseHdcInternal(hdc);
     }
 }
 public static void DrawNoResizeHandle(Graphics graphics, Rectangle bounds, bool isPrimary, Glyph glyph)
 {
     IntPtr hdc = graphics.GetHdc();
     try
     {
         IntPtr handle = System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, isPrimary ? grabHandleFillBrushPrimary : grabHandleFillBrush));
         IntPtr ptr3 = System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, grabHandlePenPrimary));
         System.Design.SafeNativeMethods.Rectangle(new HandleRef(glyph, hdc), bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
         System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, handle));
         System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, ptr3));
     }
     finally
     {
         graphics.ReleaseHdcInternal(hdc);
     }
 }
 public static void DrawLockedHandle(Graphics graphics, Rectangle bounds, bool isPrimary, Glyph glyph)
 {
     IntPtr hdc = graphics.GetHdc();
     try
     {
         IntPtr handle = System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, grabHandlePenPrimary));
         IntPtr ptr3 = System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, grabHandleFillBrushPrimary));
         System.Design.SafeNativeMethods.RoundRect(new HandleRef(glyph, hdc), bounds.Left + LOCKHANDLEUPPER_OFFSET, bounds.Top, (bounds.Left + LOCKHANDLEUPPER_OFFSET) + LOCKHANDLESIZE_UPPER, bounds.Top + LOCKHANDLESIZE_UPPER, 2, 2);
         System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, isPrimary ? grabHandleFillBrushPrimary : grabHandleFillBrush));
         System.Design.SafeNativeMethods.Rectangle(new HandleRef(glyph, hdc), bounds.Left, bounds.Top + LOCKHANDLELOWER_OFFSET, bounds.Right, bounds.Bottom);
         System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, ptr3));
         System.Design.SafeNativeMethods.SelectObject(new HandleRef(glyph, hdc), new HandleRef(glyph, handle));
     }
     finally
     {
         graphics.ReleaseHdcInternal(hdc);
     }
 }
 public void Draw(Graphics g, int x, int y, int width, int height, int index)
 {
     if ((index < 0) || (index >= this.Images.Count))
     {
         throw new ArgumentOutOfRangeException("index", System.Windows.Forms.SR.GetString("InvalidArgument", new object[] { "index", index.ToString(CultureInfo.CurrentCulture) }));
     }
     IntPtr hdc = g.GetHdc();
     try
     {
         System.Windows.Forms.SafeNativeMethods.ImageList_DrawEx(new HandleRef(this, this.Handle), index, new HandleRef(g, hdc), x, y, width, height, -1, -1, 1);
     }
     finally
     {
         g.ReleaseHdcInternal(hdc);
     }
 }