Updating a Gauge and Detecting a User Interrupt (Delphi 4.0)
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. |
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. |
From the ActiveX controls, Add the LEvntSnk control to your form. |
|
6. |
Add the Variables to the Private section of Form1: |
fEscape: Boolean; //Use to indicate the user interrupt
fInProc: Boolean; //Use to avoid closing the form during processing
RasterProc: LEADRasterProcess;
7. |
Now, add the following to the Form's Create event to create the ILEADRasterProcess object |
// Create the Rasterproc object.
RasterProc:= CreateComObject (CLASS_LEADRasterProcess) as LEADRasterProcess;
LEADEventSink1.Connect (RasterProc, _LEADRasterProcessEvents);
8. |
Code the Button1 Click’s procedure as follows: |
procedure TForm1.Button1Click(Sender: TObject);
var
sRet: Smallint;
begin
//Enable the ProgressStatus event
RasterProc.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
RasterProc.Median (LEADRasterView1.Raster, 4);
//Clean up
fInProc := False; //Processing is no longer taking place
LEADRasterView1.ForceRepaint (sRet);
end;
9. |
Code the LEADEventSink1 OnInvoke event as follows: |
procedure TForm1. LEADEventSink1Invoke(Sender: TObject; DispID: Integer;
const IID: TGUID; LocaleID: Integer; Flags: Word; Params: tagDISPPARAMS;
varResult, ExcepInfo, ArgErr: Pointer);
var
iPrecent: OleVariant;
begin
iPrecent:= OleVariant(Params.rgvarg^[0]);
case (DispID) of
LEADRASTERPROCESSEVENTS_PROGRESSSTATUS:
begin
Application.ProcessMessages (); //Detect Windows events
if (not fEscape) then // Look for the Click on the Quit button
begin
TrackBar1.Position := iPrecent;
end
else
RasterProc.EnableProgressEvent:= False; //Cancel the task
end;
end;
end;
10. |
Code the Button2 click’s procedure as follows: |
procedure TForm1.Button2Click(Sender: TObject);
begin
fEscape:= True; //The user wants to quit
Trackbar1.Position := 0 ;
end;
11. |
Code the Form1 CloseQuery procedure as follows: |
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if (fInProc) then //If processing is taking place
CanClose:= False; //Do not let the user close the form
end;
12. |
On the Project pull-down menu, use the Import Type library… and select the LEAD Raster Process object library (14.5). |
13. |
At the beginning of the Unit1 file, add LTRASTERPROCLib_TLB to the uses section. For example: |
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, LTRASTERPROCLib_TLB;
14. |
Run your program to test it. |