close
文章出處

上海的夏天真是熱,盡管我是個在汕頭海邊長大的孩子,受到這熱浪還是一個勁得受不了

還好前天去華聯貯了一堆面包零食,呆在屋子里兩天都沒出去,一個勁地猛啃

索性就一直畫下去吧!反正畫得興起了,嘿嘿!這些內容都是從網上或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,但想發揮作用是需要質的變化的,量的積累的,
基礎是最重要的,而我,則不再迷茫




不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 AutoPoster 的頭像
    AutoPoster

    互聯網 - 大數據

    AutoPoster 發表在 痞客邦 留言(0) 人氣()