Delphi单程序运行的问题请教
业务是这样:我们开发了一个程序,我登录这个程序后,比如我隐藏到系统托盘了,然后我再去开一个这个程序,但是一台电脑不能启动多个,所有再打开时就直接把隐藏在托盘的那个弹出来 这个有办法实现吗? 望指导,谢谢
举个例子:
比如我是之前已经打开QQ登录了,然后隐藏到托盘了,当我再次点击桌面的QQ快捷方式的时候就是直接把已经登录的QQ面板弹出来显示在桌面,就是类似于这个样子;
万分感谢! delphi 系统托盘
[解决办法]
const
AllowedInstances = 1;
var
MyAppName, MyClassName: array[0..255] of Char;
NumFound: Integer;
LastFound, MyPopup: HWND;
function LookAtAllWindows(Handle: HWND; Temp: LongInt): BOOL; stdcall;
var
WindowName, ClassName: Array[0..255] of Char;
begin
// Go get the windows class name
if GetClassName(Handle, ClassName, SizeOf(ClassName)) > 0 then
// Is the window class the same ?
if StrComp(ClassName, MyClassName) = 0 then
// Get its window caption
if GetWindowText(Handle, WindowName, SizeOf(WindowName)) > 0 then
// Does this have the same window title ?
if StrComp(WindowName, MyAppName) = 0 then
begin
inc(NumFound);
// Are the handles different ?
if Handle <> Application.Handle then
// Save it so we can bring it to the top later.
LastFound := Handle;
end;
result := true;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
NumFound := 0; LastFound := 0;
// First, determine what this application'name is.
GetWindowText(Application.Handle, MyAppName, SizeOf(MyAppName));
// Now determine the class name for this application
GetClassName(Application.Handle, MyClassName, SizeOf(MyClassName));
// Now count how many others out there are Delphi apps with this title
EnumWindows(@LookAtAllWindows, 0);
if NumFound > AllowedInstances then
// there is another instance running, bring it to the front !
begin
MyPopup := GetLastActivePopup(LastFound);
// Bring it to the top in the Z-Order
BringWindowToTop(LastFound);
// Is the window iconized ?
if IsIconic(MyPopup) then
// Restore it to its original position
ShowWindow(MyPopup, SW_RESTORE)
else
// Bring it to the front
SetForegroundWindow(MyPopup);
// Halt this instance
Halt;
end;
end;