1

用途: .NET 2.0+; VS 2005;

次のカスタム DataGridView があります。以下のコード。

これで、私はTextandImageCellを持っていますが、イラストは常に次のようなセル内の画像の後にテキストの順序になっています

(画像 | テキスト)

ただし、私の要件は、次のようなセルで逆の方法で使用することです

(テキスト | 画像)

これに対する回避策はありますか?

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

namespace MyWindowsApplication
{
    public class DataGridViewRadioButtonColumn : DataGridViewCheckBoxColumn
    {
        internal const int radioButtonSize = 14;

        public DataGridViewRadioButtonColumn()
            : base()
        {
            this.ReadOnly = true;
        }

        internal static void Paint(Graphics g, Rectangle cellBounds, bool state)
        {
            cellBounds.Inflate(-1, -1);

            Brush drawBrsh = new SolidBrush(SystemColors.Control);

            g.FillRectangle(drawBrsh, cellBounds);

            cellBounds.Inflate(0, -((cellBounds.Height - radioButtonSize) / 2));

            ControlPaint.DrawRadioButton(g, cellBounds, (state) ? ButtonState.Checked : ButtonState.Normal);

            drawBrsh.Dispose();
        }
    }

    public class DataGridViewRadioButtonCell : DataGridViewCheckBoxCell
    {

    }

    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; }
        }
    }

    public class LinkAndImageColumn : DataGridViewLinkColumn
    {
        private Image imageValue;
        private Size imageSize;

        public LinkAndImageColumn()
        {
            this.CellTemplate = new LinkAndImageCell();
        }

        public override object Clone()
        {
            LinkAndImageColumn c = base.Clone() as LinkAndImageColumn;

            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 LinkAndImageCell LinkAndImageCellTemplate
        {
            get { return this.CellTemplate as LinkAndImageCell; }
        }

        internal Size ImageSize
        {
            get { return imageSize; }
        }
    }

    public class LinkAndImageCell : DataGridViewLinkCell
    {
        private Image imageValue;
        private Size imageSize;

        public override object Clone()
        {
            LinkAndImageCell c = base.Clone() as LinkAndImageCell;

            c.imageValue = this.imageValue;
            c.imageSize = this.imageSize;

            return c;
        }



        public Image Image
        {
            get
            {
                if (this.OwningColumn == null ||  this.OwningLinkAndImageColumn == null)
                {
                    return imageValue;
                }
                else if (this.imageValue != null)
                {
                    return this.imageValue;
                }
                else
                {
                    return this.OwningLinkAndImageColumn.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 LinkAndImageColumn OwningLinkAndImageColumn
        {
            get { return this.OwningColumn as LinkAndImageColumn; }
        }

    }

    public class CustomDataGridView : DataGridView
    {
        private DataGridViewRow _currentSelectedRow = null;

        public DataGridViewRow CurrentSelectedRow
        {
            get { return _currentSelectedRow; }
            set { _currentSelectedRow = value; }
        }

        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            bool b = false;

            if (e.RowIndex > -1)
            {
                DataGridViewCell cell = this[e.ColumnIndex, e.RowIndex];
                if (cell.OwningColumn is DataGridViewCheckBoxColumn)
                {
                    if (cell.FormattedValue != null)
                    {
                        for (int i = 0; i < this.RowCount; i++)
                        {
                            this[e.ColumnIndex, i].Value = false;
                        }
                        cell.Value = true;
                    }
                }
            }
            base.OnCellClick(e);
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            bool b = false;

            if ((e.RowIndex > -1) && (e.ColumnIndex > -1) && (this.Columns[e.ColumnIndex] is DataGridViewRadioButtonColumn))
            {
                e.PaintBackground(e.CellBounds, true);

                if (e.Value != null)
                {
                    if (e.Value.ToString() == "1")
                    {
                        b = true;
                    }
                    else b = false;
                }

                DataGridViewRadioButtonColumn.Paint(e.Graphics, e.CellBounds, b);

                e.Handled = true;
            }
            base.OnCellPainting(e);
        }

        protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                this.CurrentSelectedRow = this.Rows[e.RowIndex];
                this.CurrentCell = this[0, e.RowIndex];
                this.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Gold;
            }
            base.OnCellMouseDown(e);
        }
    }




}
4

1 に答える 1

0

テキストの後に画像を表示するには、このコードをクラスのPaintメソッドに追加/更新する必要がありますLinkAndImageCell

if (this.Image != null)
        {
            Size textSize = TextRenderer.MeasureText(value.ToString(), cellStyle.Font, cellBounds.Size);
            int imageXLocation = cellBounds.Location.X + textSize.Width;
            int imageYLocation = cellBounds.Location.Y + textSize.Height / 2 ;
            Point imageLocation = new Point(imageXLocation, imageYLocation);
            GraphicsContainer container = graphics.BeginContainer();
            graphics.SetClip(cellBounds);
            graphics.DrawImageUnscaled(Image, imageLocation);
            graphics.EndContainer(container);
        }
于 2014-05-26T16:20:44.547 に答える