Creating a Phosphrus Line

HI,
I AM interested in creating an effect like phosphorus on the screen where you draw a line from point A to point  B and it remains for a second then slowly disperses.

Thanks

Peter
schenkpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Here is an example form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

  /// <summary>
  /// Summary description for frmFadingLine.
  /// </summary>
public class frmFadingLine : System.Windows.Forms.Form
{
  private System.Windows.Forms.CheckBox checkBox1;
  private System.Windows.Forms.Timer timerFade;
  private System.Windows.Forms.Timer timerAppear;
  private System.ComponentModel.IContainer components;

  public frmFadingLine()
  {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                  ControlStyles.UserPaint |
                  ControlStyles.DoubleBuffer,true);


  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if(components != null)
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  [STAThread]
  static void Main()
  {
    Application.Run(new frmFadingLine());
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
    this.components = new System.ComponentModel.Container();
    this.checkBox1 = new System.Windows.Forms.CheckBox();
    this.timerFade = new System.Windows.Forms.Timer(this.components);
    this.timerAppear = new System.Windows.Forms.Timer(this.components);
    this.SuspendLayout();
    //
    // checkBox1
    //
    this.checkBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
    this.checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
    this.checkBox1.Location = new System.Drawing.Point(188, 12);
    this.checkBox1.Name = "checkBox1";
    this.checkBox1.Size = new System.Drawing.Size(68, 32);
    this.checkBox1.TabIndex = 0;
    this.checkBox1.Text = "Draw Line";
    this.checkBox1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
    //
    // timerFade
    //
    this.timerFade.Tick += new System.EventHandler(this.timerFade_Tick);
    //
    // timerAppear
    //
    this.timerAppear.Interval = 1000;
    this.timerAppear.Tick += new System.EventHandler(this.timerAppear_Tick);
    //
    // frmGraphicsEffects
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
    this.ClientSize = new System.Drawing.Size(268, 266);
    this.Controls.Add(this.checkBox1);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.Name = "frmGraphicsEffects";
    this.Text = "Graphic Effects";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmGraphicsEffects_Paint);
    this.ResumeLayout(false);

  }
  #endregion

  private Color _lineColor = Color.Red;

  private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
  {

    this.timerAppear.Enabled = this.checkBox1.Checked;
    this.Invalidate();

  }

  private void frmGraphicsEffects_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {

    e.Graphics.Clear(this.BackColor);

    if (this.checkBox1.Checked)
    {
      Pen pen = new Pen(this._lineColor);
      e.Graphics.DrawLine(pen, new Point(20, 20), new Point(250, 250));

    }
 
  }

  private void timerAppear_Tick(object sender, System.EventArgs e)
  {
    this.timerAppear.Enabled = false;
    this.timerFade.Enabled = true;
  }

  private void timerFade_Tick(object sender, System.EventArgs e)
  {

    int red = this._lineColor.R;
    int green = this._lineColor.G;
    int blue = this._lineColor.B;
    int alpha = this._lineColor.A;

    if (alpha > 0)
    {
      alpha = Math.Max(alpha - 20, 0);

      this._lineColor = Color.FromArgb(alpha, red, green, blue);
    }
    else
    {
      this.checkBox1.Checked = false;
      this.timerFade.Enabled = false;
    }
    this.Invalidate();
  }

}

Notes:

1) 'checkBox1' draws the line, and starts the 'timerAppear' timer

2) timerAppear controls how long the line appears (1000 ms = 1s)

3) timerAppear starts the 'timerFade' timer

4) timerFade decreases the alpha channel for the line color from 255 (solid) to 0 (totally transparent)

5) The speed of the fade can be controlled by setting timerFade interval, or by changing the step for the alpha channel decrease (set to 20):

      alpha = Math.Max(alpha - 20, 0);

6) There is a check to keep the alpha value between 0 and 255.

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
schenkpAuthor Commented:
You Rock thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.