?
Solved

Creating a Phosphrus Line

Posted on 2006-04-11
2
Medium Priority
?
159 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:schenkp
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16438021
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
 

Author Comment

by:schenkp
ID: 16438453
You Rock thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

840 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