CloseMainWindow: Boolean;
The CloseMainWindow method closes the process that has the user interface by sending the command to the main window to close.
The method returns True if the running process has the main window and the command to close is sent to the window. The method returns False if the process has no main window or is already running.
Executing the example requires a form with the Button1 button, the Label component named Label1 and a timer named Timer1. It is assumed that Microsoft Office is installed in the operating system.
Add links to the Host and Ui system assemblies.
Class TESTForm: Form
Button1: Button;
Button2: Button;
Label1: Label;
Timer1: Timer;
WinProc: IWinProcess;
WinInfo: IWinShellExecuteInfo;
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Begin
WinInfo := New WinShellExecuteInfo.Create;
WinInfo.File := "excel.exe";
WinProc := New WinProcess.Create;
WinProc.Start(WinInfo);
Timer1.Enabled := True;
End Sub Button1OnClick;
Sub Button2OnClick(Sender: Object; Args: IMouseEventArgs);
Begin
WinProc.CloseMainWindow;
End Sub Button2OnClick;
Sub Timer1OnTimer(Sender: Object; Args: IEventArgs);
Begin
If WinProc <> Null Then
If Not WinProc.HasExited Then
Label1.Text := "Process is being executed";
Else
Label1.Text := "Process is complete. Completion code:" + WinProc.ExitCode.ToString;
End If;
Else
Label1.Text := "Process is not started"
End If;
End Sub Timer1OnTimer;
End Class TESTForm;
Microsoft Excel starts after starting the form by clicking the Button1 button. The execution status is controlled by the timer procedure. The corresponding message is displayed in the Label1 component. The running Microsoft Excel closes on clicking the Button2 button.
See also: