Updating a Gauge and Detecting a User Interrupt (C++ Builder)

Take the following steps to update a gauge during processing and detect a user interrupt:

1.

Start with the project that you created in Loading and Displaying an Image.

2.

image\btndbtn.gif Select the Button control; then add the control to your main form. Put the control at the top of the form to keep it away from the image.

3.

In the Object Inspector box, change the Button control's Caption property to Do Median.

4.

image\btndbtn.gif Select the Button control; then add another control to your main form. Put the control at the top of the form to keep it away from the image.

5.

In the Object Inspector box, change the Button control's Caption property to Quit.

6.

Build a gauge consisting of two rectangular shape objects (Shape1 and Shape2). Use Shape1 as the frame of the gauge. Use Shape2 as the fill color by putting it inside of Shape1, flush left. Change Shape2's brush color to any contrasting color, and change its Width property to 1.

7.

Add the following global variable declarations at the top of the Unit1 file:

bool fEscape; //Used with the Gauge example
bool fInProc; //Used with the Gauge example

8.

Code the Do Median button's Click procedure as follows:

void __fastcall TForm1::Button1Click(TObject *Sender)

{
//Enable the Status event
Lead1->EnableProgressEvent = True;

//Initialize the indicators
fEscape = False; //The user does not want to quit
fInProc = True; //Processing is taking place

//Perform a relatively slow median filter
Lead1->Median(5);

//Clean up
fInProc = False; //Processing is no longer taking place
Lead1->ForceRepaint();
}

9.

Code LEAD control's Status procedure as follows:

void __fastcall TForm1::Lead1Status(TObject *Sender, int Percent)
{
   TMsg WinMsg; //Windows message

 

//Are there any messages in the queue (like a mouse click) ?
while( PeekMessage(&WinMsg,Handle, 0, 0, PM_REMOVE) ) 
{
    TranslateMessage( &WinMsg ); //Translates virtual key codes.
    DispatchMessage( &WinMsg ); //Dispatches message to window.
}

if (!fEscape) // Look for the Click on the Quit button
  Shape2->Width = Shape1->Width * Percent / 100; //Udpate the gauge
else
  Lead1->EnableProgressEvent = False; //Cancel the task
}

10.

Code the Quit button's Click procedure as follows:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    fEscape = True; //The user wants to quit
    Shape2->Width = 1; //Set the gauge back to the beginning
}

11.

Code the main form's CloseQuery procedure as follows:

void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    if (fInProc) //If processing is taking place
        CanClose = False; //Do not let the user close the form

}

12.

Run your program to test it.