我希望我的应用程序只有一个实例,并能处理新触发的实例的参数。我想使用参数,并根据这些参数进行一些处理。
我在相关问题上尝试了大卫·赫弗南(David Heffernan)的answer,但在FMX和Delphi Rio文档中没有发现与TCreateParams相关的任何内容。
无法在此处发表评论,因为我没有声誉。
program StartupProject;
uses
SysUtils,Messages,Windows,Forms,Form3 in 'firetest.pas' {MainForm};
{$R *.res}
procedure Main;
var
i: Integer;
Arg: string;
Window: HWND;
CopyDataStruct: TCopyDataStruct;
begin
Window := FindWindow(SWindowClassname,nil);
if Window=0 then begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm,MainForm);
Application.Run;
end else begin
FillChar(CopyDataStruct,Sizeof(CopyDataStruct),0);
for i := 1 to ParamCount do begin
Arg := ParamStr(i);
CopyDataStruct.cbdata := (Length(Arg)+1)*SizeOf(Char);
CopyDataStruct.lpData := pchar(Arg);
SendMessage(Window,WM_COPYDATA,NativeInt(@CopyDataStruct));
end;
setforegroundWindow(Window);
end;
end;
begin
Main;
end.
unit fireTest;
interface
uses
...
type
TForm3 = class(TForm)
procedure FormCreate(Sender: TObject);
public
procedure ProcessArgument(const Arg: string);
{ Public declarations }
protected
procedure WMCopyData(var Message: TWMCopyData); message WM_COPYDATA;
var
Form3: TForm3;
implementation
{$R *.dfm}
{ TMainForm }
procedure TForm3.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 1 to ParamCount do begin
ProcessArgument(ParamStr(i));
end;
end;
procedure TForm3.ProcessArgument(const Arg: string);
begin
Memo1.Lines.Add(arg);
end;
procedure TForm3.WMCopyData(var Message: TWMCopyData);
var
Arg: string;
begin
SetString(Arg,pchar(Message.CopyDataStruct.lpData),(Message.CopyDataStruct.cbdata div SizeOf(Char))-1);
ProcessArgument(Arg);
//Application.Restore;
//Application.BringToFront;
end;
end.