只让程序运行一个实例,如何实现已经运行的实例获得焦点并正常显示 ? ( 积分: 50 )

S

swlove

Unregistered / Unconfirmed
GUEST, unregistred user!
已经实现只让程序运行一个实例,但我想再次双击这程序时能正常显示该程序

在下代码中,发送ShowWindow(iHandle,SW_SHOWMAXIMIZED),这可以再次显示程序,但这是最大化显示,我想实现的是正常显示

而且我试过其他参数,如SW_NORMAL 之类的都不行

请问要如何修改才能实现我的要求呢 ? 谢谢

program Project1;

uses
Forms,Windows,
Unit1 in 'Unit1.pas ' {Form1};

{$R *.res}
const
iAtom= 'MyApplication ';
var
iHandle:THandle;
begin
if GlobalFindAtom(iAtom)=0 then
begin
GlobalAddAtom(iAtom);
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
GlobalDeleteAtom(GlobalFindAtom(iAtom));
end
else if Application.MessageBox( '程序已经在运行! ', '提示 ',MB_YESNO+MB_ICONASTERISK)=IDYES then
begin
iHandle:=FindWindow(nil, 'Form1 ');
if iHandle < > 0 then
ShowWindow(iHandle,SW_SHOWMAXIMIZED);
end;
end.
 
我想实现的是类似 声音控制软件 VolumeEasy 那样的效果
 
program Project1;

uses
Forms, Windows,
Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

var Mutex: THandle;

procedure CheckPrevInst;
var
PrevWnd: HWnd;
begin
Mutex:=CreateMutex(NIL, False, 'SingleInstanceProgramMutex');
if WaitForSingleObject(Mutex, 10000)=WAIT_TIMEOUT then Halt;
PrevWnd:=FindWindow('TForm1', 'Form1 Caption');//这里的 'Form1 Caption' 是主窗体的标题
if PrevWnd<>0 then PrevWnd:=GetWindow(PrevWnd, GW_OWNER);
if PrevWnd<>0 then begin
if IsIconic(PrevWnd) then
ShowWindow(PrevWnd, SW_SHOWNORMAL)
else
SetForegroundWindow(PrevWnd);
Halt;
end;
end;


begin
Application.Initialize;
try
CheckPrevInst;
Application.CreateForm(TForm1, Form1);
finally
Form1.HandleNeeded;
ReleaseMutex(Mutex);
CloseHandle(Mutex);
end;
Application.Run;
end.
 
全部复制过去,只是修改了窗体标题,但编译不了

编译时没有生成实例窗体

Delphi7下的
 
哈,可以了

在我代码的基础上,找到句柄后,SetForegroundWindow(iHandle);
 
如果你的系统Form1已经关闭了呢?
这样也行?
 
突然发现一个问题就是:

如果生成的实例名和窗体名一样,那么隐藏的程序就不能正常显示
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
603
import
I
顶部