public void DrawIconUnstretched(Icon icon, Rectangle targetRect) { Bitmap currentBitmap = new Bitmap(icon.Width, icon.Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(currentBitmap); g.DrawIconUnstretched(icon, new Rectangle(0, 0, targetRect.Width, targetRect.Height)); this.DrawImage(currentBitmap, targetRect); // currentBitmap.Dispose(); }
public void RenderTreeNode( Graphics g, ITreeInfo treeInfo, TreeNode treeNode, Rectangle nodeRectangle, Rectangle clip ) { bool isLast = (treeNode.Index == treeNode.ParentCollection.Count - 1); Size ecSize = GetGlyphSize( g, treeNode.IsExpanded ); Point ecCenter = new Point( nodeRectangle.X + _leftSep + ecSize.Width / 2, nodeRectangle.Y + (nodeRectangle.Height - ecSize.Height) / 2 + ecSize.Height / 2 ); using( Brush brush = new HatchBrush( HatchStyle.Percent50, SystemColors.Window, SystemColors.GrayText ) ) using( Pen pen = new Pen( brush ) ) { g.DrawLine( pen, ecCenter, new Point( ecCenter.X + 12, ecCenter.Y ) ); g.DrawLine( pen, ecCenter, new Point( ecCenter.X, nodeRectangle.Y ) ); if( !isLast ) { g.DrawLine( pen, ecCenter, new Point( ecCenter.X, nodeRectangle.Bottom ) ); } } int textX = nodeRectangle.X + ecSize.Width + _leftSep + _ecSep; if( treeNode.Icon != null ) { Icon image = treeNode.Icon; g.DrawIconUnstretched( image, new Rectangle( textX, nodeRectangle.Y + (nodeRectangle.Height - image.Height) / 2, image.Width, image.Height ) ); textX += image.Width + _imageSep; } if( treeInfo.IsSelected( treeNode ) ) { Brush brush = treeInfo.IsTreeFocused() ? SystemBrushes.Highlight : SystemBrushes.Control; g.FillRectangle( brush, textX, nodeRectangle.Y, nodeRectangle.Right - textX + 2, nodeRectangle.Height - 1 ); if( treeInfo.IsTreeFocused() ) { using( Brush hatchBrush = new HatchBrush( HatchStyle.Percent50, SystemColors.Highlight ) ) using( Pen pen = new Pen( hatchBrush ) ) { g.DrawRectangle( pen, textX, nodeRectangle.Y, nodeRectangle.Right - textX + 2, nodeRectangle.Height - 1 ); } } } WinFormsUtility.Drawing.GdiPlusEx.DrawString ( g, treeNode.Text, treeNode.Font, SystemColors.ControlText , new Rectangle( textX + 2, nodeRectangle.Y + _verticalSep, int.MaxValue, int.MaxValue ) , WinFormsUtility.Drawing.GdiPlusEx.TextSplitting.SingleLineEllipsis, WinFormsUtility.Drawing.GdiPlusEx.Ampersands.Display ); if( treeNode.ChildNodes.Count > 0 ) { DrawGlyph( g, new Point( nodeRectangle.X + _leftSep, nodeRectangle.Y + (nodeRectangle.Height - ecSize.Height) / 2 ), treeNode.IsExpanded ); } }
// PaintPrivate is used in three places that need to duplicate the paint code: // 1. DataGridViewCell::Paint method // 2. DataGridViewCell::GetContentBounds // 3. DataGridViewCell::GetErrorIconBounds // // if computeContentBounds is true then PaintPrivate returns the contentBounds // else if computeErrorIconBounds is true then PaintPrivate returns the errorIconBounds // else it returns Rectangle.Empty; private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { // Parameter checking. // One bit and one bit only should be turned on Debug.Assert(paint || computeContentBounds || computeErrorIconBounds); Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds); Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint); Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds); Debug.Assert(cellStyle != null); if (paint && DataGridViewCell.PaintBorder(paintParts)) { PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle resultBounds; Rectangle valBounds = cellBounds; Rectangle borderWidths = BorderWidths(advancedBorderStyle); valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; if (valBounds.Width > 0 && valBounds.Height > 0 && (paint || computeContentBounds)) { Rectangle imgBounds = valBounds; if (cellStyle.Padding != Padding.Empty) { if (this.DataGridView.RightToLeftInternal) { imgBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { imgBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } imgBounds.Width -= cellStyle.Padding.Horizontal; imgBounds.Height -= cellStyle.Padding.Vertical; } bool cellSelected = (elementState & DataGridViewElementStates.Selected) != 0; SolidBrush br = this.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected) ? cellStyle.SelectionBackColor : cellStyle.BackColor); if (imgBounds.Width > 0 && imgBounds.Height > 0) { Image img = formattedValue as Image; Icon ico = null; if (img == null) { ico = formattedValue as Icon; } if (ico != null || img != null) { DataGridViewImageCellLayout imageLayout = this.ImageLayout; if (imageLayout == DataGridViewImageCellLayout.NotSet) { if (this.OwningColumn is DataGridViewImageColumn) { imageLayout = ((DataGridViewImageColumn) this.OwningColumn).ImageLayout; Debug.Assert(imageLayout != DataGridViewImageCellLayout.NotSet); } else { imageLayout = DataGridViewImageCellLayout.Normal; } } if (imageLayout == DataGridViewImageCellLayout.Stretch) { if (paint) { if (DataGridViewCell.PaintBackground(paintParts)) { DataGridViewCell.PaintPadding(g, valBounds, cellStyle, br, this.DataGridView.RightToLeftInternal); } if (DataGridViewCell.PaintContentForeground(paintParts)) { if (img != null) { // bug 21949: Graphics.DrawImage does not treat well scaled images // we have to pass an ImageAttribute ImageAttributes attr = new ImageAttributes(); attr.SetWrapMode(WrapMode.TileFlipXY); g.DrawImage(img, imgBounds, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attr); attr.Dispose(); } else { g.DrawIcon(ico, imgBounds); } } } resultBounds = imgBounds; } else { Rectangle imgBounds2 = ImgBounds(imgBounds, (img == null) ? ico.Width : img.Width, (img == null) ? ico.Height : img.Height, imageLayout, cellStyle); resultBounds = imgBounds2; if (paint) { if (DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } if (DataGridViewCell.PaintContentForeground(paintParts)) { //paint the image Region reg = g.Clip; g.SetClip(Rectangle.Intersect(Rectangle.Intersect(imgBounds2, imgBounds), Rectangle.Truncate(g.VisibleClipBounds))); if (img != null) { g.DrawImage(img, imgBounds2); } else { g.DrawIconUnstretched(ico, imgBounds2); } g.Clip = reg; } } } } else { if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } resultBounds = Rectangle.Empty; } } else { if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } resultBounds = Rectangle.Empty; } Point ptCurrentCell = this.DataGridView.CurrentCellAddress; if (paint && DataGridViewCell.PaintFocus(paintParts) && ptCurrentCell.X == this.ColumnIndex && ptCurrentCell.Y == rowIndex && this.DataGridView.ShowFocusCues && this.DataGridView.Focused) { // Draw focus rectangle ControlPaint.DrawFocusRectangle(g, valBounds, Color.Empty, br.Color); } if (this.DataGridView.ShowCellErrors && paint && DataGridViewCell.PaintErrorIcon(paintParts)) { PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, valBounds, errorText); } } else if (computeErrorIconBounds) { if (!String.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(valBounds); } else { resultBounds = Rectangle.Empty; } } else { Debug.Assert(valBounds.Height <= 0 || valBounds.Width <= 0); resultBounds = Rectangle.Empty; } return resultBounds; }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { Rectangle empty; Point point; if (paint && DataGridViewCell.PaintBorder(paintParts)) { this.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle cellValueBounds = cellBounds; Rectangle rectangle3 = this.BorderWidths(advancedBorderStyle); cellValueBounds.Offset(rectangle3.X, rectangle3.Y); cellValueBounds.Width -= rectangle3.Right; cellValueBounds.Height -= rectangle3.Bottom; if (((cellValueBounds.Width <= 0) || (cellValueBounds.Height <= 0)) || (!paint && !computeContentBounds)) { if (computeErrorIconBounds) { if (!string.IsNullOrEmpty(errorText)) { return base.ComputeErrorIconBounds(cellValueBounds); } return Rectangle.Empty; } return Rectangle.Empty; } Rectangle destRect = cellValueBounds; if (cellStyle.Padding != Padding.Empty) { if (base.DataGridView.RightToLeftInternal) { destRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { destRect.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } destRect.Width -= cellStyle.Padding.Horizontal; destRect.Height -= cellStyle.Padding.Vertical; } bool flag = (elementState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None; SolidBrush cachedBrush = base.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && flag) ? cellStyle.SelectionBackColor : cellStyle.BackColor); if ((destRect.Width > 0) && (destRect.Height > 0)) { Image image = formattedValue as Image; Icon icon = null; if (image == null) { icon = formattedValue as Icon; } if ((icon != null) || (image != null)) { DataGridViewImageCellLayout imageLayout = this.ImageLayout; switch (imageLayout) { case DataGridViewImageCellLayout.NotSet: if (base.OwningColumn is DataGridViewImageColumn) { imageLayout = ((DataGridViewImageColumn) base.OwningColumn).ImageLayout; } else { imageLayout = DataGridViewImageCellLayout.Normal; } break; case DataGridViewImageCellLayout.Stretch: if (paint) { if (DataGridViewCell.PaintBackground(paintParts)) { DataGridViewCell.PaintPadding(g, cellValueBounds, cellStyle, cachedBrush, base.DataGridView.RightToLeftInternal); } if (DataGridViewCell.PaintContentForeground(paintParts)) { if (image != null) { ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetWrapMode(WrapMode.TileFlipXY); g.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); imageAttr.Dispose(); } else { g.DrawIcon(icon, destRect); } } } empty = destRect; goto Label_037E; } Rectangle a = this.ImgBounds(destRect, (image == null) ? icon.Width : image.Width, (image == null) ? icon.Height : image.Height, imageLayout, cellStyle); empty = a; if (paint) { if (DataGridViewCell.PaintBackground(paintParts) && (cachedBrush.Color.A == 0xff)) { g.FillRectangle(cachedBrush, cellValueBounds); } if (DataGridViewCell.PaintContentForeground(paintParts)) { Region clip = g.Clip; g.SetClip(Rectangle.Intersect(Rectangle.Intersect(a, destRect), Rectangle.Truncate(g.VisibleClipBounds))); if (image != null) { g.DrawImage(image, a); } else { g.DrawIconUnstretched(icon, a); } g.Clip = clip; } } } else { if ((paint && DataGridViewCell.PaintBackground(paintParts)) && (cachedBrush.Color.A == 0xff)) { g.FillRectangle(cachedBrush, cellValueBounds); } empty = Rectangle.Empty; } } else { if ((paint && DataGridViewCell.PaintBackground(paintParts)) && (cachedBrush.Color.A == 0xff)) { g.FillRectangle(cachedBrush, cellValueBounds); } empty = Rectangle.Empty; } Label_037E: point = base.DataGridView.CurrentCellAddress; if (((paint && DataGridViewCell.PaintFocus(paintParts)) && ((point.X == base.ColumnIndex) && (point.Y == rowIndex))) && (base.DataGridView.ShowFocusCues && base.DataGridView.Focused)) { ControlPaint.DrawFocusRectangle(g, cellValueBounds, Color.Empty, cachedBrush.Color); } if ((base.DataGridView.ShowCellErrors && paint) && DataGridViewCell.PaintErrorIcon(paintParts)) { base.PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, cellValueBounds, errorText); } return empty; }
// Draw the cursor. public void Draw(Graphics g, Rectangle targetRect) { if(icon != null) { g.DrawIconUnstretched(icon, targetRect); } }