1st question: I want to show preview of selected screensaver on some control (such as TPanel). I checked on
MSDN how to do it. I wanted do it with ShellExecute():
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
P:
string;
begin
Edit1.Text := ExtractShortPathName(Edit1.Text);
P := Format('
"%s" /p %d', [Edit1.Text, Panel1.Handle]);
Edit2.Text := P;
ShellExecute(
Handle, '
open', PChar(P),
nil,
nil, SW_SHOW);
end;
Don't work (also without ")
2nd attempt:
Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
var
P, A:
string;
begin
Edit1.Text := ExtractShortPathName(Edit1.Text);
P := Format('
%s', [Edit1.Text]);
A := Format('
/p %d', [Panel1.Handle]);
Edit2.Text := P + A;
ShellExecute(
Handle, '
open', PChar(P), PChar(A),
nil, SW_SHOW);
end;
Also don't work
3rd attempt (with CreateProcess()). I wrote simple class for further usage:
Delphi-Quellcode:
unit ProcessUtils;
interface
uses Windows;
var
Process: THandle;
type
TProcess =
class
public class function Create(lpApplicationName, lpCommandLine:
String): THandle;
public class function Terminate(hProcess: THandle): Boolean;
end;
implementation
class function TProcess.Create(lpApplicationName, lpCommandLine:
String): THandle;
var
Startup: TStartupInfo;
Process: TProcessInformation;
Created: Boolean;
begin
Result := 0;
FillChar(Startup, SizeOf(TStartupInfo), #0);
FillChar(Process, SizeOf(TProcessInformation), #0);
Startup.cb := SizeOf(TStartupInfo);
Created := CreateProcess(PChar(lpApplicationName), PChar('
' + lpCommandLine),
nil,
nil, False,
CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,
nil,
nil, Startup, Process
);
if Created
then
begin
Result := Process.hProcess;
//WaitForSingleObject(Process.hProcess, INFINITE);
//CloseHandle(Process.hThread);
//CloseHandle(Process.hProcess);
end;
end;
class function TProcess.Terminate(hProcess: THandle): Boolean;
var
pId: DWORD;
begin
Result := False;
// GetWindowThreadProcessId(hProcess, @pId);
// hProcess := OpenProcess(PROCESS_TERMINATE, False, pId);
if hProcess <> 0
then
begin
Result := TerminateProcess(hProcess, 0);
if Result
then
CloseHandle(hProcess)
;
end;
end;
initialization
Process := 0;
end.
And try to load ssaver preview:
Delphi-Quellcode:
procedure TForm1.Button3Click(Sender: TObject);
begin
Edit2.Text := Format('"%s" /p %d', [Edit1.Text, Panel1.Handle]);
Process := TProcess.Create(Edit1.Text, Format('/p %d', [Panel1.Handle]));
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
TProcess.Terminate(Process);
end;
Screensaver starts, but not preview in panel - I see config dialog (in normal mode, not in this panel
)
When I use console to execute Edit2.Text, preview is loaded in Panel1
What I'm doing wrong and how to do it well
And also I have 2nd question: How to determine whether the screensaver have setup dialog?