这次换画DataGridViewTextBoxCell了

上海的夏天真是热,尽管我是个在汕头海边长大的孩子,受到这热浪还是一个劲得受不了

还好前天去华联贮了一堆面包零食,呆在屋子里两天都没出去,一个劲地猛啃

索性就一直画下去吧!反正画得兴起了,嘿嘿!这些内容都是从网上或MSDN学来的,只是今天一个个整理下,贴出来

看到别人能取个标题蛮好的,无奈打小语文没学好,实在没多少墨水,只能写这么拙的一个名了..........

DataGridView控件没有对在同一单元格内同时显示图标和文本提供支持,又只有在CELL里画了,嘿嘿

 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
            {
                //获取包含此单元格的列
                //查看此列是否是OwningTextAndImageColumn
                //有一种情况为空则不能赋图像
                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;
                    //当前单元格的样式

                    //而具体文字的位置则是在这里设置样式
                    //这里是设置图在字之前,这个好做
                    //如果要反过来,并且文字的长度不固定的话,则需取LOCATION+长度了
                    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.DrawImage(this.Image, cellBounds.Location.X + cellBounds.Width - 20, cellBounds.Location.Y);

                graphics.EndContainer(container);
            }
        }

        private TextAndImageColumn OwningTextAndImageColumn
        {
            //获取包含此单元格的列
            get
            {
                return this.OwningColumn as TextAndImageColumn;
            }
        }
    }


效果图喽:


农民伯伯是人,软件工程师也是人
画GDI是CODER,谈系统架构设计也是CODER
我写的代码还不够,尽管知道点OO,知道点OOD,但想发挥作用是需要质的变化的,量的积累的,
基础是最重要的,而我,则不再迷茫



posted @ 2008-07-05 13:23  yellowyu  阅读(2961)  评论(2编辑  收藏  举报