2013-03-22 65 views
1

用途:.NET 2.0+; VS 2005;DataGridView在同一单元格上的文本后的图像;

我有我的以下自定义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); 
     } 
    } 




} 
+1

尝试处理CellPainting事件(http://msdn.microsoft.com/en-gb /library/system.windows.forms.datagridview.cellpainting.aspx)并使用'e.CellBounds.Location'&'e.CellBounds.Width'数学来调整图像偏移量。 – 2013-03-22 11:45:33

回答

0

成文字后显示的图像,你应该添加/更新这个代码到方法的LinkAndImageCellPaint

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); 
     } 
相关问题