Creating a Phosphrus Line

Posted on 2006-04-11
Last Modified: 2010-08-05
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.


Question by:schenkp
    LVL 96

    Accepted Solution

    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

        // TODO: Add any constructor code after InitializeComponent call
        this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                      ControlStyles.UserPaint |


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

      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);
        // 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.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);


      private Color _lineColor = Color.Red;

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

        this.timerAppear.Enabled = this.checkBox1.Checked;


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


        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);
          this.checkBox1.Checked = false;
          this.timerFade.Enabled = false;



    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.


    Author Comment

    You Rock thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now