zoukankan      html  css  js  c++  java
  • 在Winform的DataGridView的单元格中同时显示文本和图标,以及树形结构的示例

    • 单元格中同时显示文本和图标

    The DataGridView control does not have any built-in support for showing an icon and text in the same cell. Through the different painting customization events, such as the CellPaint event, you can easily display an icon next to the text in the cell.

    The following example extends the DataGridViewTextColumn and cell to paint an image next to the text. The sample uses the DataGridViewCellStyle.Padding property to adjust the text location and overrides the Paint method to paint an icon. This sample can be simplified by handling the CellPainting event and performing similar code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;

    namespace JrtGenerator.Utility
    {
        
    public class TextAndImageColumn : DataGridViewTextBoxColumn
        {
            
    private Image imageValue;
            
    private Size imageSize;
            
    public TextAndImageColumn()
            {
                
    this.CellTemplate = new TextAndImageCell();
            }

            
    public override object Clone()
            {
                TextAndImageColumn c 
    = base.Clone() as TextAndImageColumn;
                c.imageValue 
    = this.imageValue;
                c.imageSize 
    = this.imageSize;
                
    return c;
            }

            
    public Image Image
            {
                
    get
                {
                    
    return this.imageValue;
                }
                
    set
                {
                    
    if (this.Image != value)
                    {
                        
    this.imageValue = value;
                        
    this.imageSize = value.Size;
                        
    if (this.InheritedStyle != null)
                        {
                            Padding inheritedPadding 
    = this.InheritedStyle.Padding;
                            
    this.DefaultCellStyle.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
                        }
                    }
                }
            }

            
    private TextAndImageCell TextAndImageCellTemplate
            {
                
    get
                {
                    
    return this.CellTemplate as TextAndImageCell;
                }
            }
            
    internal Size ImageSize
            {
                
    get
                {
                    
    return imageSize;
                }
            }
        }

        
    public class TextAndImageCell : DataGridViewTextBoxCell
        {
            
    private Image imageValue;

            
    private Size imageSize;

            
    public override object Clone()
            {
                TextAndImageCell c 
    = base.Clone() as TextAndImageCell;
                c.imageValue 
    = this.imageValue;
                c.imageSize 
    = this.imageSize;
                
    return c;
            }

            
    public Image Image
            {
                
    get
                {
                    
    if (this.OwningColumn == null || this.OwningTextAndImageColumn == null)
                    {
                        
    return imageValue;
                    }
                    
    else if (this.imageValue != null)
                    {
                        
    return this.imageValue;
                    }
                    
    else
                    {
                        
    return this.OwningTextAndImageColumn.Image;
                    }
                }
                
    set
                {
                    
    if (this.imageValue != value)
                    {
                        
    this.imageValue = value;
                        
    this.imageSize = value.Size;
                        Padding inheritedPadding 
    = this.InheritedStyle.Padding;
                        
    this.Style.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
                    }
                }
            }

            
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
            {
                
    // Paint the base content
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                
    if (this.Image != null)
                {
                    
    // Draw the image clipped to the cell.
                    System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
                    graphics.SetClip(cellBounds);
                    graphics.DrawImageUnscaled(
    this.Image, cellBounds.Location);
                    graphics.EndContainer(container);
                }
            }

            
    private TextAndImageColumn OwningTextAndImageColumn
            {
                
    get
                {
                    
    return this.OwningColumn as TextAndImageColumn;
                }
            }
        }

    }

  • 相关阅读:
    在dll里malloc/new/cvCreate分配内存,在exe里free/Releases释放内存时会出错。
    其原因可能是堆被损坏,这说明 100BloodCellSegTest.exe 中或它所加载的任何 DLL 中有 Bug。
    牛顿方法(Newton's Method)
    广义线性模型(Generalized Linear Models)
    逻辑回归(Logistic Regression)
    局部加权回归、欠拟合、过拟合(Locally Weighted Linear Regression、Underfitting、Overfitting)
    java并发编程实战《三》互斥锁(上)
    关于java链接装载的思考
    java并发编程实战《二》java内存模型
    (转)技术人的王者路,看看你在哪个段位?
  • 原文地址:https://www.cnblogs.com/Fooo/p/1341429.html
Copyright © 2011-2022 走看看