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\btncmd.gif Add 2 Button controls to your main form. Put them at the top of the form to keep it away from the image, name them as follows:

 

Name

Caption

 

Button1

Do Median

 

Button2

Quit

3.

Add a TrackBar control to your main Form, and change the Max property of it to 100.

4.

If you didn’t install LEvntSnk, install it.

 

On the Component pull-down menu, use install component …, and browse to LEADXX\Include\LEvntSnk.pas, and press Ok.

5.

Open the LEvntSnk.hpp file and delete those lines from the TLEADAbstractEventSink class.

private:
void *__IDispatch; /* IDispatch */

public:
operator IDispatch*(void) { return (IDispatch*)&__IDispatch; }
operator IUnknown*(void) { return (IUnknown*)&__IDispatch; }

 

And save the file.

6.

From the ActiveX controls, Add the LEvntSnk control to your form.

7.

Add the Variables to the Private section of Form1:

bool fEscape; //Use to indicate the user interrupt
bool fInProc; //Use to avoid closing the form during processing
LEADRasterProcess * pRasterProc; 

8.

Now, add the following to the Form's Create event to create the ILEADRasterProcess object

// Create the Rasterproc object.
 CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&pRasterProc);
 LEADEventSink1->Connect (pRasterProc, DIID__LEADRasterProcessEvents); 

9.

Code the Button1 Click’s procedure as follows:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//Enable the ProgressStatus event
pRasterProc->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
pRasterProc->Median (LEADRasterView1->Raster, 4);
//Clean up
fInProc = False; //Processing is no longer taking place
LEADRasterView1->ForceRepaint ();
}

10.

Code the LEADEventSink1 OnInvoke event as follows:

 void __fastcall TForm1:: LEADEventSink1Invoke(TObject *Sender, int DispID,
const TGUID &IID, int LocaleID, WORD Flags, tagDISPPARAMS &Params,
Pointer varResult, Pointer ExcepInfo, Pointer ArgErr)
{
 switch (DispID)
{
 case LEADRASTERPROCESSEVENTS_PROGRESSSTATUS:
{
 int iPrecent= (OleVariant)(Params.rgvarg[0]);

 Application->ProcessMessages (); //Detect Windows events
  if (!fEscape) // Look for the Click on the Quit button
 {
  TrackBar1->Position= iPrecent;
 }
  else
  pRasterProc->EnableProgressEvent = false; //Cancel the task
}
}
}

11.

Code the Button2 click’s procedure as follows:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
fEscape= True; //The user wants to quit
TrackBar1->Position= 0 ;
}

12.

Code the Form1 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
}

13.

On the Project pull-down menu, use the Import Type library… and select the LEAD Raster Process object library (14.5).

14.

At the beginning of the Unit1.h file include the file:

#include "LTRASTERPROCLib_TLB.h"

15.

Run your program to test it.