Home > .Net > Draw Progress Bar in Janus GridEx cell content

Draw Progress Bar in Janus GridEx cell content

Draw Progress Bar in Janus GridEx cell content

As following my previous post [post] how to create customizable cell content, I decided to post progressbar sample CSharp code.
See full implementation of Progress bar cell in Janus GridEx control.

C# Usage example

 

private readonly ProgressBarCellRenderer m_progressBarCellRenderer = new ProgressBarCellRenderer

                {BorderPen = new Pen(Color.LightGray)};

 

Perform drawing on each OnDrawGridArea method callback.

void OnDrawGridArea(object sender, DrawGridAreaEventArgs args)

{

     if (e.Column.Key == MyColumn)

     {

         GridEXCellCollection currentRowCells = e.Row.Cells;

 

         double total = GetValue();

 

         if (total == 0)

            return;

 

         m_progressBarCellRenderer.DrawProgressBar(

                                                args

                                              , total

                                              , "custom text");

      }

}


Class that implements all drawing logic of a Progress Bar. Its usage you sow in

OnDrawGridArea

method.

 

/// <summary>

/// Represents progress bar cell renderer for Janus GridEx.

/// </summary>

/// <example>

/// Create GridEx with column with key "MyColumn"

/// Set the column property OwnerDrawnMode=Cells

/// Override OnDrawGridArea method or subscribe on DrawGridArea event.

/// <code>

///protected override void OnDrawGridArea(DrawGridAreaEventArgs e)

///{

///    if (e.Column.Key == "MyColumn")

///    {

///        pbRenderer.DrawProgressBar(e, MyProgressValue);

///    }

///    base.OnDrawGridArea(e);

///}

/// </code>

/// </example>

public class ProgressBarCellRenderer : IDisposable

{

    private bool m_disposed;

 

    /// <summary>

    /// Initializes <see cref="ProgressBarCellRenderer"/> class.

    /// </summary>

        public ProgressBarCellRenderer()

        {

            BackgroundColorOuter = Color.FromArgb(192, 255, 192);

            BackgroundColorInner = Color.FromArgb(232, 255, 232);

            ElapsedColorOuter = Color.PowderBlue;

            BackgroundColorInner = Color.LightSkyBlue;

            BorderPen = new Pen(Color.Gray, 1);

            TextBrush = new SolidBrush(Color.Black);

            Margins = new Margins(0, 0, 0, 0);

        }

        /// <summary>

        /// Gets or sets elapsed bar outer color.

        /// </summary>

        public Color ElapsedColorOuter { get; set; }

        /// <summary>

        /// Gets or sets elapsed bar inner color.

        /// </summary>

        public Color ElapsedColorInner { get; set; }

        /// <summary>

        /// Gets or sets background bar outer color.

        /// </summary>

        public Color BackgroundColorOuter { get; set; }

        /// <summary>

        /// Gets or sets background bar inner color.

        /// </summary>

        public Color BackgroundColorInner { get; set; }

        /// <summary>

        /// Gets or sets border pen.

        /// </summary>

        public Pen BorderPen { get; set; }

        /// <summary>

        /// Gets or sets text brush.

        /// </summary>

        public Brush TextBrush { get; set; }

        /// <summary>

        /// Gets or sets bar margins.

        /// </summary>

        public Margins Margins { get; set; }

 

 

        /// <summary>

        /// Draws progress bar in given area. If you wish to customize the drawing use separate methods instead.

        /// </summary>

        /// <param name="area">Janus GridEX Draw Grid Area event arguments.</param>

        /// <param name="value">Progress value [0..1].</param>

        public void DrawProgressBar(DrawGridAreaEventArgs area, double value)

        {

            area.Handled = true;

 

            DrawBackgroundBar(area);

            DrawElapsedBar(area, value);

            DrawText(area, value);

            DrawBorder(area);

        }

 

        ///<summary>

        /// Draws progress bar in given area, with the given text

        ///</summary>

        ///<param name="area">Janus GridEX Draw Grid Area event arguments.</param>

        ///<param name="value">Progress value [0..1].</param>

        ///<param name="text">The custom text to be presented to the user.</param>

        public void DrawProgressBar(DrawGridAreaEventArgs area, double value, string text)

        {

            area.Handled = true;

 

            DrawBackgroundBar(area);

            DrawElapsedBar(area, value);

            DrawText(area, StringAlignment.Center, text);

            DrawBorder(area);

        }

 

        /// <summary>

        /// Draws the background bar of the progress.

        /// </summary>

        /// <param name="p_area">Drawing area event arguments.</param>

        public void DrawBackgroundBar(DrawGridAreaEventArgs p_area)

        {

            Rectangle rect = GetBarRectangle(p_area);

 

            Rectangle halfRect = Rectangle.Inflate(rect, 0, rect.Height / -4);

            p_area.PaintBackground();

 

            using (LinearGradientBrush br = new LinearGradientBrush(

                halfRect, BackgroundColorInner, BackgroundColorOuter, LinearGradientMode.Vertical)

                { WrapMode = WrapMode.TileFlipXY })

            {

              p_area.Graphics.FillRectangle(br, rect);

            }

        }

        /// <summary>

        /// Draws brogress bar border.

        /// </summary>

        /// <param name="p_area">Drawing area event arguments.</param>

        public virtual void DrawBorder(DrawGridAreaEventArgs p_area)

        {

            p_area.Graphics.DrawRectangle(BorderPen, GetBarRectangle(p_area));

        }

 

        private Rectangle GetBarRectangle(DrawGridAreaEventArgs p_area)

        {

            Rectangle rect = Rectangle.Inflate(p_area.Bounds, -2, -4);

            int leftOfBar = rect.X + Margins.Left;

            int topOfBar = rect.Y + Margins.Top;

 

            return new Rectangle(leftOfBar, topOfBar, rect.Width – Margins.Right – Margins.Left, rect.Height – Margins.Bottom – Margins.Top);

        }

 

        /// <summary>

        /// Draws the elapsed bar of the progress.

        /// </summary>

        /// <param name="p_area">Drawing area event arguments.</param>

        /// <param name="p_progress">Progress value.</param>

        public virtual void DrawElapsedBar(DrawGridAreaEventArgs p_area, double p_progress)

        {

            Rectangle rect = GetBarRectangle(p_area);

 

            rect.Width = (int)(rect.Width * p_progress);

            Rectangle halfRect = Rectangle.Inflate(rect, 0, rect.Height / -4);

            //now draw the rectangle:

            if (rect.Width > 0)

            {

                using (LinearGradientBrush br = new LinearGradientBrush(

                    halfRect, ElapsedColorInner, ElapsedColorOuter, LinearGradientMode.Vertical)

                    { WrapMode = WrapMode.TileFlipXY })

                {

                    p_area.Graphics.FillRectangle(br, rect);

                }

            }

        }

        /// <summary>

        /// Draws the progress value text in the center of the progress bar.

        /// </summary>

        /// <param name="area">Drawing area event arguments.</param>

        /// <param name="value">Progress value.</param>

        public virtual void DrawText(DrawGridAreaEventArgs area, double value)

        {

            DrawText(area, StringAlignment.Center ,value.ToString("#0%"));

        }

        /// <summary>

        /// Draws the custom text on the progress bar using given alignment.

        /// </summary>

        /// <param name="area">Drawing area event arguments.</param>

        /// <param name="text">Text to be drawn.</param>

        /// <param name="alignment">Text alignment.</param>

        public virtual void DrawText(DrawGridAreaEventArgs area, StringAlignment alignment, string text)

        {

            using (StringFormat format = new StringFormat(area.StringFormat))

            {

                format.Alignment = alignment;

                area.Graphics.DrawString(text, area.Font, TextBrush , area.Bounds, format);

            }

        }

 

        #region IDisposable Members

        /// <summary>

        /// Releases allocated resources.

        /// </summary>

        public void Dispose()

        {

            if (m_disposed)

                return;

            BorderPen.Dispose();

            TextBrush.Dispose();

            GC.SuppressFinalize(this);

            m_disposed = true;

        }

 

        ~ProgressBarCellRenderer()

        {

            Dispose();

        }

        #endregion

    }

}

 

Most of the code dealing with Rectangle drawing, but as you can see from code example below its not very complicated. Good luck 🙂

Categories: .Net Tags:
  1. No comments yet.
  1. No trackbacks yet.