Introduction
I was developing a chart based application where zoom (or shift) is used to magnify a region of the waveform, and there are 20 zoom levels. The Windows Form has two buttons which represent Zoom-In and Zoom-Out. Individual OnButtonClick
events implement the zoom, and it could be repeatedly called as many times until the desired zoom level is achieved. However, this involves a lot of clicking. The more appropriate solution would be to click the mouse (MouseDown
) and keep it down where it loops the zoom (or shift) process until the desired zoom level is achieved via MouseUp
.
In this code example, the code responds to the mouse pointer via the buttons, and rather than implement the zoom functionality, it initiates the colour loop pattern in the event of mouse down and continues to do so until the mouse up event. In addition, when the MouseDown
event occurs, the colour pattern pulsates once for 250msec, it then pulsate the colour in a loop every 100msec until the MouseUp
event.
Background
There are several techniques but this code does not require the use of Application.DoEvents()
, which is considered as a bad programming practice. See this blog entry.
Mouse messages could be acquired from the system via WndProc
or Control
, but the coding procedure appears to be highly technical. In this case, the coding provides a simpler solution for beginners or intermediate-level programmers.
Using the code
In the event of MouseDown
via clicking on button1
, it creates a thread instance with a delegate to the code below. The code below handles the button colour pulsation and timing. The timing is set by a global variable (at the top of the code). The code below is for button1
and is similar for button2.
private Thread flashThread1=null;
private Thread flashThread2=null;
int initialLongPause=250;
double repeatShortPause=50;
....
....
....
protected void flashingButton1()
{
button1.BackColor=Color.Red;
System.Threading.Thread.Sleep(initialLongPause);
DateTime nextEvent =
DateTime.Now.AddMilliseconds(repeatShortPause);
TimeSpan ts;
while (button1.Enabled==false)
{
ts = nextEvent.Subtract(DateTime.Now);
if (ts.TotalMilliseconds <= 0)
{
button1.BackColor=Color.Red;
System.Threading.Thread.Sleep(
Convert.ToInt32(repeatShortPause));
nextEvent =
DateTime.Now.AddMilliseconds(repeatShortPause);
}
System.Threading.Thread.Sleep(5);
button1.BackColor=Color.Gainsboro;
}
}
When the pointer goes over button1
and it is clicked, the MouseDown
event will execute the code below. This creates the thread instance and executes the above code.
protected void button1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
button1.Enabled=false;
flashThread1 = new Thread(new ThreadStart(flashingButton1));
flashThread1.IsBackground = true;
flashThread1.Start();
}
Note: When using the OnMouseClick
, it seem to prevent MouseDown
or MouseUp
events from occurring (which could be used to disable the colour pulsation). This is where Application.DoEvent()
comes in handy.
The code below handles MouseUp
which is based on the Windows form rather than the individual buttons. It aborts all active threads. This way, it prevents thread exception errors.
private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
button1.Enabled=true;
button2.Enabled=true;
if (flashThread1!=null)
flashThread1.Abort();
if (flashThread2!=null)
flashThread2.Abort();
button1.BackColor=Color.Gainsboro;
button2.BackColor=Color.Gainsboro;
}
Points of Interest
This is the first time I publish this code, and I would like to encourage useful feedback and techniques to improve the above code, and make revisions as time goes by. I am happy to accept negative comments as long they are educational. I would welcome advice on WndProc
or Control
relating to reading the mouse state (using polling rather than events).
History
This is the first code release.
Electronic Engineer for embedded microcontrol projects (C and C++) and linear.
Keen C# programmer for external interface and electronics related application.
Still can't make up mind stick with NET 1.1 or upgrade to NET 2.0(!). Bill Gate will start to fish everyone to Vista, ie Halo-2 need Vista (damn him!)
Hobby:- Ski (racer), Sci-Fi, Game and books.