Skip to main content Skip to footer

Animated Cells in Spread.Net

Spread.Net control provides a vast variety of CellTypes for different types of implementations in Spread. AnimatedCellType is one such CellType which is used for displaying animation in cell. To use this, we need to create a custom cell type inheriting from ImageCellType. Here in this blog, we will explain an approach on HowTo implement AnimatedCellType. For our demonstration, we will create a custom cell type from ImageCellType which will handle .gif images updation outside of the sheet. This is a two step process:

  1. Paint cells using PaintCell() method
  2. Notify updation of the frame for painting using OnFrameChanged() method.

Paint Cells

For implementation of the same we will use .Net Framework's ImageAnimator class which animates an image that has time-based frames. Following code can be used to paint the image frame per second rate using PaintCell() method:


    public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)  
    {  
      if (currentImage == null)  
      {  
        base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);  
        StartAnimate(value);  
      }  
      else lock (currentImage)  
          base.PaintCell(g, r, appearance, currentImage, isSelected, isLocked, zoomFactor);  
    }  

Notify Updation of Frame

Now once the painting of cells is done, we need to notify updation of the frame for painting depending on frame per second rate. This will be done using OnFrameChanged() method as shown in the code below :

    void OnFrameChanged(object sender, EventArgs e)  
    {  
      lock (currentImage)  
        ImageAnimator.UpdateFrames(currentImage);  
      int rv = sheet.FpSpread.GetRowViewportCount();  
      int cv = sheet.FpSpread.GetColumnViewportCount();  
      Rectangle spreadRect = sheet.FpSpread.Bounds;  
      for (int r = -1; r < rv; r++)  
        for (int c = -1; c < cv; c++)  
        {  
          Rectangle rect = sheet.FpSpread.GetCellRectangle(r, c, rowIndex, columnIndex);  
          rect.Intersect(spreadRect);  
          if (!rect.IsEmpty)  
            sheet.FpSpread.Invalidate();  
        }  
    }

AnimatedCellType AnimatedCellType Please have a look at the attached sample for detailed implementation of the same. DownloadSample_CS DownloadSample_VB

MESCIUS inc.

comments powered by Disqus