This example describes how to call the specified Windows program in your own program.
Start a new project and add 4 TButton components to the blank form. The form after you add the component is shown in Figure 1.
Figure 1 form after adding a component
Where the WinExec button is used to invoke the specified Windows program with the WinExec function. The ShellExecute button is used to invoke the specified Windows program with the ShellExecute function. The CreateProcess button and the TerminateProcess button are used to implement a more comprehensive operation on the invoked program, which is used to invoke the specified Windows program, while the latter is used to terminate the invoked Windows program.
The response code to invoke the specified Windows program in the form of the WinExec function is as follows:
procedure TForm1.btnWinExecClick(Sender: TObject);
begin
WinExec(’Notepad.exe’,SW_MAXIMIZE);
end;
The 1th parameter of the WinExec function represents the path and name of the invoked program, and the 2nd parameter represents how the program is displayed.
The response code to invoke the specified Windows program in the form of the ShellExecute function is as follows:
procedure TForm1.btnShellExecuteClick(Sender: TObject);
begin
ShellExecute(Application.Handle,’Open’,’NotePad.exe’,PChar(’C:\AutoExec.bat’),nil,SW_SHOWNORMAL);
end;
Where ShellExecute is used to open the program file associated with the specified file. The 1th parameter specifies a handle to a window, and the 2nd parameter specifies the class that the program runs, such as ' Open ' or ' print '; the 3rd parameter specifies a program name or file name that you want to print or open with the associated program, or if an executable file is specified in the 3rd argument, Then the 4th parameter is the argument passed to the executing program, and the 5th parameter specifies the full path of the executable program. The last parameter is used to set how the program runs.