解决方法
你可以创建一个Semaphore …并停止执行(将代码放入你的* .dpr文件),让你运行的应用程序到屏幕上。
var Semafor : THandle; begin // Don't start twice ... if already running bring this instance to front Semafor := CreateSemaphore(nil,1,'MY_APPLICATION_IS_RUNNING'); if ((Semafor <> 0) and // application is already running (GetLastError = ERROR_ALREADY_EXISTS)) then begin RestoreWindow('TMyApplication'); CloseHandle(Semafor); Halt; end; Application.CreateForm(....); Application.Initialize; Application.Run; CloseHandle(Semafor); end;
aFormName是应用程序中您的主表单类的名称。
procedure RestoreWindow(aFormName : string); var Wnd,App : HWND; begin Wnd := FindWindow(PChar(aFormName),nil); if (Wnd <> 0) then begin // Set Window to foreground App := GetWindowLong(Wnd,GWL_HWNDPARENT); if IsIconic(App) then ShowWindow(App,SW_RESTORE); SetForegroundwindow(App); end; end;