Click here to Skip to main content
15,868,016 members
Articles / Multimedia / GDI+

Pulse Button

Rate me:
Please Sign up or sign in to vote.
4.90/5 (57 votes)
10 Oct 2009CPOL2 min read 115.9K   7.7K   161   25
How to create a button that radiates pulses

The PulseButtonTest application

Introduction

This article demonstrates how to make a button with a dynamic element (pulses) using .NET 2.0 and GDI+. The control utilizes the .NET Framework's Button class.

The Button States

Here are the different button states:

The different button states

The MouseOver is just shown with a white (transparent) border and the focus is shown with a solid orange. The pressed state is the same as the default but without the reflex.

The Graphics

Here are the different elements of the button displayed:

The graphic elements of the PulseButton

Both Image and Text property can be set. The button supports two kinds of shapes: round and rectangular. The rectangular shape can have rounded corners.

Architecture

The control consists of a single class PulseButton that inherits from the System.Windows.Forms.Button class:

The PulseButton Class

Using the Code

To test the button, just download the demo project and build it using Visual Studio 2008.
Click the different PulseButtons in order to reference them in the property grid.

Here's a brief description of the properties:

  • ButtonColorBottom - The color of the centre's bottom
  • ButtonColorTop - The color of the centre's top
  • CornerRadious - The radius of the corner when Shape is set to rectangle
  • FocusColor - The color of the border that indicates focused
  • ForeColor - The color of the Text
  • Interval - The timer interval, default 50 [ms] (this property is not browsable)
  • NumberOfPulses - Number of pulses, 1 - 3 give the best results
  • PulseColor - The color of the pulses
  • PulseWidth - The width of the pulses - should be less than half of the control width
  • ShapeType - Round or Rectangle
  • PulseSpeed - The speed of the pulses, a value between 0.1 - 2 looks OK

Code

The pulses are updated using a System.Windows.Forms.Timer. The routine that renders the pulses looks like this:

C#
/// <summary>
/// Handles the pulse timer tick.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> 
/// instance containing the event data.</param>
private void PulseTimerTick(object sender, EventArgs e)
{
  pulseTimer.Enabled = false;
  InflatePulses();
  Invalidate();
  pulseTimer.Enabled = true;
}

/// <summary>
/// Inflates the pulses.
/// </summary>
private void InflatePulses()
{
  for (var i = 0; i < pulses.Length; i++)
    {
      pulses[i].Inflate(PulseSpeed, PulseSpeed);
      if (pulses[i].Width > Width || pulses[i].Height > Height || 
			pulses[i].X < 0 || pulses[i].Y < 0)
        pulses[i] = new RectangleF(pulseWidth, pulseWidth, 
		Width - 2 * pulseWidth, Height - 2 * pulseWidth);
      pulseColors[i] = Color.FromArgb((int)(Math.Min(pulses[i].X * 255 / 
					pulseWidth, 255)), PulseColor);
    }
}

The pulses are inflated using the PulseSpeed, when a pulse exceeds the bounds of the control then the size is reset. The pulses color gets more transparent when moving to the edge of the control.

Points of Interest

The regular Button control covers a lot more than what just meets the eye, so we gain a lot by inheriting from it. Another possibility is to inherit from ButtonBase and implement IButtonControl to avoid getting what you don't need, but that would be much more effort.

History

  • 10th October, 2009: First version of control 1.0
  • June 2015: WPF version found here:
    Image 5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Denmark Denmark
Name: Niel Morgan Thomas
Born: 1970 in Denmark
Education:
Dataengineer from Odense Technical University.
More than 20 years in IT-business.
Current employment:
Cloud architect at University College Lillebaelt

Comments and Discussions

 
QuestionHow to use it in MFC? Pin
Jason.LYJ29-Feb-16 19:04
professionalJason.LYJ29-Feb-16 19:04 
AnswerRe: How to use it in MFC? Pin
Niel M.Thomas29-Feb-16 20:17
professionalNiel M.Thomas29-Feb-16 20:17 
QuestionHow to change start posistion of pulses Pin
N. Henrik Lauridsen23-May-15 1:29
N. Henrik Lauridsen23-May-15 1:29 
QuestionVery very good Pin
N. Henrik Lauridsen21-May-15 23:19
N. Henrik Lauridsen21-May-15 23:19 
AnswerRe: Very very good Pin
Niel M.Thomas22-May-15 4:35
professionalNiel M.Thomas22-May-15 4:35 
Questionблагодарю Pin
Member 1016869223-Jul-13 2:35
Member 1016869223-Jul-13 2:35 
GeneralMy vote of 5 Pin
claudiotronic30-Apr-13 22:25
claudiotronic30-Apr-13 22:25 
GeneralMy vote of 5 Pin
xiaox2y22-Nov-12 22:22
xiaox2y22-Nov-12 22:22 
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 8:36
professionalKanasz Robert27-Sep-12 8:36 
QuestionVery nice Pin
BillW337-Sep-12 11:03
professionalBillW337-Sep-12 11:03 
QuestionMy vote of 5 Pin
Philippe from Orleans15-Apr-12 23:22
Philippe from Orleans15-Apr-12 23:22 
GeneralMy vote of 5 Pin
Jαved14-Jan-12 1:53
professionalJαved14-Jan-12 1:53 
QuestionProblem to download project Pin
Edward11128-Oct-11 11:43
Edward11128-Oct-11 11:43 
GeneralMy vote of 5 Pin
Eduardo_David15-Apr-11 15:14
Eduardo_David15-Apr-11 15:14 
GeneralPerformance Pin
securigy26-Oct-10 20:24
securigy26-Oct-10 20:24 
GeneralRe: Performance Pin
Niel M.Thomas26-Oct-10 20:59
professionalNiel M.Thomas26-Oct-10 20:59 
Hi securigy, try this for stopping the pulsation
<br />
private bool running;<br />
<br />
public bool Start<br />
{<br />
  get { return running;}<br />
  set { running = value;<br />
        pulseTimer.Enabled = true;}<br />
}<br />
<br />
Change this method:<br />
<br />
private void PulseTimerTick(object sender, EventArgs e)<br />
{<br />
  pulseTimer.Enabled = false;<br />
  InflatePulses();<br />
  Invalidate();<br />
  if (running)<br />
    pulseTimer.Enabled = true;<br />
}<br />

[With best regards ]
[Niel M. Thomas ]



Generalvery nice full Pin
balajitkp19-Jul-10 19:30
balajitkp19-Jul-10 19:30 
GeneralNice Pin
rushsky25-Oct-09 17:07
rushsky25-Oct-09 17:07 
GeneralVery Nice Pin
Heywood12-Oct-09 0:23
Heywood12-Oct-09 0:23 
GeneralAbove and Beyond Pin
AspDotNetDev10-Oct-09 22:06
protectorAspDotNetDev10-Oct-09 22:06 
GeneralBeautiful Pin
Simpzon10-Oct-09 9:29
Simpzon10-Oct-09 9:29 
QuestionNicely structured and clearly explained. Pin
Mike Rich10-Oct-09 8:58
Mike Rich10-Oct-09 8:58 
AnswerRe: Nicely structured and clearly explained. Pin
Simpzon10-Oct-09 9:25
Simpzon10-Oct-09 9:25 
GeneralGood job Pin
Laserson10-Oct-09 8:00
Laserson10-Oct-09 8:00 
GeneralVeru nice Pin
Eli Nurman10-Oct-09 7:57
Eli Nurman10-Oct-09 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.