Xceed Grid for WinForms v4.3 Documentation
PaintingViewer class

Welcome to Xceed Grid for WinForms v4.3 > Advanced Concepts > Extending the grid's classes > CellEditorManager and CellViewerManager extensions > How to derive from the CellViewerManager class > PaintingViewer class

The PaintingViewer class demonstrates how to override the PaintCellBackground, PaintCellForeground, and GetFittedHeightCore methods to create a custom painting CellViewerManager.

VB.NET Copy Code

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Xceed.Grid.Viewers
Imports Xceed.Grid
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO 

Class PaintingViewer
   Inherits CellViewerManager

  Shared Sub New()
     Dim stream As Stream = GetType(PaintingViewer).Assembly.GetManifestResourceStream("Xceed.Grid.Samples.Favorites.PNG") 

     ' We clone the bitmap so that exceptions do not occur when the source stream is disposed of.    
     Dim image As New Bitmap( New Bitmap( stream ), New Size( 16, 16 ) )

     PaintingViewer.ValueImage = image 

     stream.Dispose()
  End Sub 

   Public Sub New()
     MyBase.New()
  End Sub 

   Protected Overrides Sub PaintCellBackground( ByVal cell As Cell, _
                                                ByVal e As GridPaintEventArgs, _
                                                ByRef handled As Boolean )
     Dim quantity As Single = 0 

     If( ( Not cell.Value Is Nothing ) And ( Not cell.Value Is DBNull.Value ) And _
                                           ( Not cell.Value Is cell.NullValue ) ) Then
       quantity = CType( Convert.ChangeType( cell.Value, GetType( Single ) ), Single )
     End If    

     Dim x As Integer = e.DisplayRectangle.X
     Dim y As Integer = e.DisplayRectangle.Y + ( e.DisplayRectangle.Height - PaintingViewer.ValueImage.Height ) / 2

     Dim imageHeight As Integer = PaintingViewer.ValueImage.Height
     Dim i As Integer 

     For i = 1 To quantity Step 1
       e.Graphics.DrawImage( PaintingViewer.ValueImage, x, y )
       x += PaintingViewer.ValueImage.Width
     Next i 

     ' Notify the cell that its background has been painted by the CellViewerManager.
     handled = True
  End Sub 

   Protected Overrides Sub PaintCellForeground( ByVal cell As Cell, _
                                          ByVal e As GridPaintEventArgs, ByRef handled As Boolean )

     ' Notify the cell that its foreground has been painted by the CellViewerManager.
     handled = True
  End Sub 

   Protected Overrides Function GetFittedHeightCore( ByVal cell As Cell, _
                                                     ByVal mode As AutoHeightMode, _
                                                     ByVal cellDisplayWidth As Integer, _
                                                     ByVal graphics As Graphics, _
                                                     ByVal printing As Boolean ) As Integer
     Return PaintingViewer.ValueImage.Height + 2
  End Function 

   Private Shared ValueImage As Image
End Class

C# Copy Code

using System;
using System.Collections.Generic;
using System.Text;
using Xceed.Grid.Viewers;
using Xceed.Grid;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

namespace WindowsApplication29
{
   class PaintingViewer : CellViewerManager
   {
    static PaintingViewer()
    {
       using( Stream stream = typeof( PaintingViewer ).Assembly.GetManifestResourceStream( "Xceed.Grid.Samples.Favorites.PNG" ) )
       {
        // We clone the bitmap so that exceptions do not occur in the case where the source stream
        // is disposed of.     
        Bitmap image = new Bitmap( new Bitmap( stream ), new Size( 16, 16 ) );
        PaintingViewer.ValueImage = image;
       }
    }    

    public PaintingViewer()
       : base()
    {
    

     protected override void PaintCellBackground( Cell cell, GridPaintEventArgs e, ref bool handled )
    {
       float quantity = 0; 

       if( ( cell.Value != null ) && ( cell.Value != DBNull.Value ) &&
                                     ( cell.Value != cell.NullValue ) )

        quantity = ( float )Convert.ChangeType( cell.Value, typeof( float ) ); 

        int x = e.DisplayRectangle.X;
        int y = e.DisplayRectangle.Y +
                     ( ( e.DisplayRectangle.Height - PaintingViewer.ValueImage.Height ) / 2 );

        int imageHeight = PaintingViewer.ValueImage.Height;

        for( int i = 1; i <= quantity; i++ )
        {
          e.Graphics.DrawImage( PaintingViewer.ValueImage, x, y );
          x += PaintingViewer.ValueImage.Width;
        }      

        // Notify the cell that its background has been painted by the CellViewerManager.
        handled = true;
    }    

     protected override void PaintCellForeground( Cell cell, GridPaintEventArgs e, ref bool handled )
    {
       // Notify the cell that its foreground has been painted by the CellViewerManager.
       handled = true;
    

     protected override int GetFittedHeightCore( Cell cell, 
                                                AutoHeightMode mode, 
                                                int cellDisplayWidth, 
                                                Graphics graphics, bool printing )
    {
       return PaintingViewer.ValueImage.Height + 2;
    

    static private Image ValueImage;     
   }
}