Click here to Skip to main content
15,920,828 members
Articles / Programming Languages / C#
Article

OnMouseClick generating automatic loop process (zoom or shift) using threads

Rate me:
Please Sign up or sign in to vote.
3.14/5 (2 votes)
9 Feb 20062 min read 36.1K   891   13   6
Using OnMouseClick to generate automatic loop processes (ie zoom or shift) using threads.

Sample Image

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.

C#
private Thread flashThread1=null;
private Thread flashThread2=null;

int initialLongPause=250;
double repeatShortPause=50;
....
....
....
protected void flashingButton1()
{
    button1.BackColor=Color.Red;
    //Initial long pause
    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;
            //Repeat shorter pause
            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.

C#
protected void button1_MouseDown(object sender, 
                  System.Windows.Forms.MouseEventArgs e)
{
    // Prevent repeats clicks that may add threads.
    button1.Enabled=false;
    // Thread instance and delegates
    flashThread1 = new Thread(new ThreadStart(flashingButton1));
    flashThread1.IsBackground = true;
    // Start thread.
    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.

C#
private void Form1_MouseUp(object sender, 
               System.Windows.Forms.MouseEventArgs e)
{
    button1.Enabled=true;
    button2.Enabled=true;
    if (flashThread1!=null)
        flashThread1.Abort();//Abort thread    
    if (flashThread2!=null)
        flashThread2.Abort();
    // Revert back to original colour. 
    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United Kingdom United Kingdom
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.

Comments and Discussions

 
GeneralGUI Controls and Threads Pin
HumanOsc14-Feb-06 0:29
HumanOsc14-Feb-06 0:29 
GeneralRe: GUI Controls and Threads Pin
riscy24-Feb-06 22:20
riscy24-Feb-06 22:20 
GeneralRe: GUI Controls and Threads Pin
HumanOsc6-Mar-06 2:54
HumanOsc6-Mar-06 2:54 
GeneralUsing a Timer would be simpler, and more Pin
Josh Smith9-Feb-06 12:52
Josh Smith9-Feb-06 12:52 
GeneralRe: Using a Timer would be simpler, and more Pin
riscy9-Feb-06 21:15
riscy9-Feb-06 21:15 
GeneralRe: Using a Timer would be simpler, and more Pin
riscy10-Feb-06 3:49
riscy10-Feb-06 3:49 

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.