Updating a Gauge and Detecting a User Interrupt (Delphi 6.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 them away from the image, name them as follows: |
|
|
Name |
Caption |
|
btnMedian |
Do Median |
|
btnQuit |
Quit |
3. |
Add a TrackBar control to your main Form, and change the Max property of it to 100. |
|
4. |
On the Project pull-down menu, use the Import Type library… and select the LEAD Raster Process object library (14.5). |
|
5. |
Select LEAD Raster Process from the ActiveX tab and add it to your form. |
|
6. |
Add the Variables to the Private section of Unit1: |
fEscape: Boolean; //Use to indicate the user interrupt
fInProc: Boolean; //Use to avoid closing the form during processing
7. |
Code the btnMedian Click’s procedure as follows: |
procedure TForm1.btnMedianClick(Sender: TObject);
begin
//Enable the ProgressStatus event
LEADRasterProcess1.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
LEADRasterProcess1.Median (LEADRasterView1.Raster, 4);
//Clean up
fInProc := False; //Processing is no longer taking place
LEADRasterView1.ForceRepaint ( );
end;
8. |
Handle the LEADRasterProcess1’s OnProgressStatus and code LEADRasterProcess1ProgressStatus as follows: |
procedure TForm1.LEADRasterProcess1ProgressStatus(Sender: TObject;
iPercent: Smallint);
begin
Application.ProcessMessages (); //Detect Windows events
if (not fEscape) then // Look for the Click on the Quit button
begin
TrackBar1.Position := iPercent;
end
else
LEADRasterProcess1.EnableProgressEvent:= False; //Cancel the task
end;
9. |
Code the btnQuit click’s procedure as follows: |
procedure TForm1.btnQuitClick(Sender: TObject);
begin
fEscape:= True; //The user wants to quit
Trackbar1.Position := 0 ;
end;
10. |
Handle the Form1 OnCloseQuery event, and code FormCloseQuery 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;
11. |
Run your program to test it. |