这段代码保证程序之运行一次,如果有一个实例的话,就Restore, SetForeGroundWindows,
在application1的工程文件里加入以下代码,执行后可以Resotr和变成前台窗口,但是反复2~3 次后,最小化按钮就没用了,why??const
AppTitle = 'Form1';
var
Mutex:THandle;begin
Mutex:=CreateMutex(nil,True, AppTitle);
if GetLastError<>ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
Application.Title := 'Form1';
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
Mutex:= FindWindow(nil, AppTitle);
ShowWindow(Mutex, SW_RESTORE);
SetForegroundWindow(Mutex);
end; ReleaseMutex(Mutex);
在application1的工程文件里加入以下代码,执行后可以Resotr和变成前台窗口,但是反复2~3 次后,最小化按钮就没用了,why??const
AppTitle = 'Form1';
var
Mutex:THandle;begin
Mutex:=CreateMutex(nil,True, AppTitle);
if GetLastError<>ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
Application.Title := 'Form1';
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
Mutex:= FindWindow(nil, AppTitle);
ShowWindow(Mutex, SW_RESTORE);
SetForegroundWindow(Mutex);
end; ReleaseMutex(Mutex);
Forms, Windows,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}var hMutex,FindHid:HWND;
MoudleName:string;function EnumWndProc(hwnd:Thandle;param:Cardinal):bool;stdcall;
//由于用于api回调函数,请使用windows传统的参数传递方式stdcall
var
ClassName,WinMoudleName:string;
WinInstance:THandle;
begin
result:=true;
SetLength(ClassName,100);
GetClassName(hwnd,pchar(ClassName),length(ClassName));//获得当前遍历窗口的类名
ClassName:=pchar(ClassName);//在字符串后加结束符,确定字符串结束
if ClassName=TForm1.ClassName then//比较
begin
WinInstance:=GetWindowLong(hwnd,GWL_HINSTANCE);//获得当前遍历窗口的实例
setlength(WinMoudleName,100);
GetModuleFileName(WinInstance,pchar(WinMoudleName),length(WinMoudleName));
//获得当前遍历窗口的程序文件名
WinMoudleName:=pchar(WinMoudleName);
if WinMoudleName=MoudleName then//MoudleName为工程全局变量,自身程序的文件名
begin
FindHid:=hwnd;//FindHid为工程全局变量保存找到的句炳
result:=false;//找到以后就结束遍历
end;
end;
end;begin
hMutex:=CreateMutex(nil,false,'hkOneCopy');
if WaitForSingleObject(hMutex,0)<>wait_TimeOut then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else begin
SetLength(MoudleName,100);
GetModuleFileName(HInstance,pchar(MoudleName),length(MoudleName));
MoudleName:=pchar(MoudleName);
EnumWindows(@EnumWndProc,0);//调用枚举函数
if FindHid<>0 then
SetForegroundWindow(FindHid);
end;end.代码出自:http://dev.csdn.net/develop/article/20/20379.shtm
const
UM_RESTORE_APPLICATION=WM_User+101;procedure TForm1.UMRestoreApplication(var Message: TMessage);
begin
if IsIconic(application.Handle) then
application.Restore
else
application.BringToFront ;
end;在Dpr中就可发这个消息
SendMessage(Mutex, UM_RESTORE_APPLICATION, 0, 0);
但我试过可以的啊,可能是不合你的要求吧,
多谢两位,接分!