// 查找MDI子窗体
function ChildFormIsExist(aMDIForm: TForm; aChildFormCaption: String): Boolean;
var
I: Integer;
FormIsExist: Boolean;
begin
Result := False;
if not Assigned(aMDIForm) then Exit;
FormIsExist := False;
for I := 0 to aMDIForm.MDIChildCount - 1 do
begin
if Trim(aMDIForm.MDIChildren[I].Caption) = Trim(aChildFormCaption) then
FormIsExist := True;
end;
if FormIsExist then
Result := True;
end;
创建子窗体的时候首先调用方法ChildFormIsExist判断子窗体是否存在。
if not ChildFormIsExist(frmMain, '子窗体的Caption') then
begin
// 创建子窗体
end;
在dpr文件中:
var
hMutex : Thandle;
WaitResult : word;
Mf : DWORD;
S:String;
begin
hMutex := createMutex(nil,false,pchar(utCommon.SysName));
WaitResult := WaitForSingleObject(hMutex,10);
if ( waitResult = WAIT_TIMEOUT ) then
begin
S:=ParamStr(1);//实际上是您所双击的文件名(含路径)
Mf:=FindWindow('TMainForm', ‘主窗口的Caption');
if (Mf<>0) and (GlobalFindAtom(PChar(S))=0) then
begin
SendMessage(Mf, WM_User+1, GlobalAddAtom(PChar(S)), Length(S)+1);
WaitForSingleObject(hMutex,10);
end;
SetForegroundWindow (Mf);
end
else
begin
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
ReleaseMutex(hMutex);
end;
CloseHandle(hMutex); // close the mutex handle
end.
主窗口:
procedure TMainForm.WndProc(var Message: TMessage);
begin
if Message.Msg=WM_User+1 then
begin
FillChar(OpenFileName, Sizeof(OpenFileName),0);
GlobalGetAtomName(Message.WParam, @OpenFileName, Message.LParam);
Show;
WindowState := wsMaximized;
Application.BringToFront;
OpenTest(OpenFileName); //打开一个子窗口显示文件
GlobalDeleteAtom(Message.WParam);
end
else
Inherited WndProc(Message);
end;