Click here to Skip to main content
Licence CPOL
First Posted 10 Oct 2009
Views 37,571
Downloads 1,425
Bookmarked 125 times

Pulse Button

By Niel M.Thomas | 10 Oct 2009
How to create a button that radiates pulses

1

2
1 vote, 2.3%
3
6 votes, 13.6%
4
37 votes, 84.1%
5
4.85/5 - 44 votes
1 removed
μ 4.82, σa 0.92 [?]
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:

/// <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

License

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

About the Author

Niel M.Thomas

Software Developer (Senior)

Denmark Denmark

Member
Name: Niel Morgan Thomas
Born: 1970 in Denmark
Education:
Dataengineer from Odense Technical University.
More than 15 years in IT-business.
Current employment:
Working with application development in a major Danish company that produce medical equipment.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberJavedsamrt2:53 14 Jan '12  
QuestionProblem to download project PinmemberEdward11112:43 28 Oct '11  
GeneralMy vote of 5 PinmemberDon_Hard16:14 15 Apr '11  
GeneralPerformance Pinmembersecurigy21:24 26 Oct '10  
GeneralRe: Performance PinmemberNiel M.Thomas21:59 26 Oct '10  
Generalvery nice full Pinmemberbalajitkp120:30 9 Jul '10  
GeneralNice Pinmemberrushsky18:07 25 Oct '09  
GeneralVery Nice PinmemberHeywood1:23 12 Oct '09  
GeneralAbove and Beyond Pinmemberaspdotnetdev23:06 10 Oct '09  
GeneralBeautiful PinmemberSimpzon10:29 10 Oct '09  
QuestionNicely structured and clearly explained. PinmemberMike Rich9:58 10 Oct '09  
AnswerRe: Nicely structured and clearly explained. PinmemberSimpzon10:25 10 Oct '09  
GeneralGood job PinmemberLaserson9:00 10 Oct '09  
GeneralVeru nice PinmemberEli Nurman8:57 10 Oct '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 10 Oct 2009
Article Copyright 2009 by Niel M.Thomas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid