最好用窗体名称判断吧: 比如发消息给A的时候: if (Form1<>nil) and (Form1.Caption='与A对话') then begin Form1.show; Form1.BringTOFront; end else begin Form1:=TForm1.Create(nil); Form1.Caption:='与A对话'; Form1.Show; end;
1.声明函数如下: FUNCTION ShowWindow(winhandle:integer; wincommand:integer):integer;stdcall;external'User32.dll'; FUNCTION BringWindowToTop(HWND:integer):integer;stdcall;external'User32.dll'; FUNCTION FindWindowA(Winhandle:integer; wintitle:string) :integer;stdcall;external'User32.dll'; 2. procedure TForm1.Button1Click(Sender: TObject); Var handle:integer; begin handle:= FindWindowA( 0, 'Form2' ); If handle > 0 Then begin BringWindowToTop(handle); ShowWindow(handle ,5 ); end else begin Application.CreateForm(TForm2, Form2); Form2.showmodal; end;end;
(1)项目文件 Project1.dpr program Project1; uses Forms, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin if CreateMutex then begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end else begin DestroyMutex; end; end.(2)============== unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm1 = class(TForm) private { Private declarations } public { Public declarations } end;var Form1: TForm1; function CreateMutex: Boolean; procedure DestroyMutex;implementationvar Mutex: hWnd;{$R *.dfm}procedure DestroyMutex; begin if Mutex <> 0 then CloseHandle(Mutex); end;function CreateMutex: Boolean; var PrevInstHandle: THandle; AppTitle: PChar; begin AppTitle := StrAlloc(100); StrPCopy(AppTitle, Application.Title); Result := True; Mutex := Windows.CreateMutex(nil, False, AppTitle); if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin Result := False; SetWindowText(Application.Handle, ''); PrevInstHandle := FindWindow(nil, AppTitle); if PrevInstHandle <> 0 then begin if IsIconic(PrevInstHandle) then ShowWindow(PrevInstHandle, SW_RESTORE) else BringWindowToTop(PrevInstHandle); SetForegroundWindow(PrevInstHandle); end; if Mutex <> 0 then Mutex := 0; end; StrDispose(AppTitle); end;end.
比如发消息给A的时候:
if (Form1<>nil) and (Form1.Caption='与A对话') then
begin
Form1.show; Form1.BringTOFront;
end
else
begin
Form1:=TForm1.Create(nil);
Form1.Caption:='与A对话';
Form1.Show;
end;
记不清楚了
FUNCTION ShowWindow(winhandle:integer; wincommand:integer):integer;stdcall;external'User32.dll';
FUNCTION BringWindowToTop(HWND:integer):integer;stdcall;external'User32.dll';
FUNCTION FindWindowA(Winhandle:integer; wintitle:string) :integer;stdcall;external'User32.dll';
2.
procedure TForm1.Button1Click(Sender: TObject);
Var handle:integer;
begin
handle:= FindWindowA( 0, 'Form2' );
If handle > 0 Then
begin
BringWindowToTop(handle);
ShowWindow(handle ,5 );
end
else
begin
Application.CreateForm(TForm2, Form2);
Form2.showmodal;
end;end;
eg.
已生成的窗口实例:
Form1.=好友A
Form1.=好友B
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
if CreateMutex then begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end else begin
DestroyMutex;
end;
end.(2)==============
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
function CreateMutex: Boolean;
procedure DestroyMutex;implementationvar Mutex: hWnd;{$R *.dfm}procedure DestroyMutex;
begin
if Mutex <> 0 then
CloseHandle(Mutex);
end;function CreateMutex: Boolean;
var
PrevInstHandle: THandle;
AppTitle: PChar;
begin
AppTitle := StrAlloc(100);
StrPCopy(AppTitle, Application.Title);
Result := True;
Mutex := Windows.CreateMutex(nil, False, AppTitle);
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin
Result := False;
SetWindowText(Application.Handle, '');
PrevInstHandle := FindWindow(nil, AppTitle);
if PrevInstHandle <> 0 then begin
if IsIconic(PrevInstHandle) then
ShowWindow(PrevInstHandle, SW_RESTORE)
else
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
end;
if Mutex <> 0 then
Mutex := 0;
end;
StrDispose(AppTitle);
end;end.