深圳全飞鸿
标题:
delphi单开的实现标准pbjustone
[打印本页]
作者:
admin
时间:
2019-6-1 16:38
标题:
delphi单开的实现标准pbjustone
单开的实现标准pbjustone
该功能在terminal.exe pack_ctn.exe中实现 该部分在terminal.exe上有十多年的验证是可行的,无需再单独做功能测试!
以下内容放在dpr中! 可能需要头
windows, SysUtils,
version 1: 收录于2019-03-20
{$R *.res}
var
MYHANDLE:Thandle;
MyAppName, MyClassName: array[0..255] of Char;
LastFound, MyPopup: HWND;
function LookAtAllWindows(Handle: HWND; Temp: LongInt): BOOL; stdcall;
var
WindowName, ClassName: Array[0..255] of Char;
begin
if GetClassName(Handle, ClassName, SizeOf(ClassName)) > 0 then
if StrComp(ClassName, MyClassName) = 0 then
if GetWindowText(Handle, WindowName, SizeOf(WindowName)) > 0 then
if StrComp(WindowName, MyAppName) = 0 then
begin
if Handle <> Application.Handle then
LastFound := Handle;
end;
IF LASTFOUND=0 THEN
result := true
ELSE
RESULT :=FALSE;
end;
begin
LastFound := 0;
GetWindowText(Application.Handle, MyAppName, SizeOf(MyAppName));
GetClassName(Application.Handle, MyClassName, SizeOf(MyClassName));
EnumWindows(@LookAtAllWindows, 0);
if LastFound <> 0 then
begin
MyPopup := GetLastActivePopup(LastFound);
BringWindowToTop(LastFound);
if IsIconic(MyPopup) then
ShowWindow(MyPopup, SW_RESTORE)
else
SetForegroundWindow(MyPopup);
Halt;
end ELSE
begin
//
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(Txx, XX);
Application.Run;
//
end;
end.
复制代码
注意:如是用户将程序改一个名字,这种方法就卡不住了。如果要卡得更严一点,则用mutex来:
加两个变量:
hmutex : HWND;
gt : Integer;
begin
hmutex:=CreateMutex(nil,True,'KB_TD@2008'); //注意改名
gt := GetLastError;
if gt = Error_ALREADY_EXISTS then {如果发现互斥体对象}
begin
Application.MessageBox('程序已经运行','提示',MB_OK);
Application.Terminate;
ReleaseMutex(hmutex);{释放互斥体}
Halt;
end;
//
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TDataModule2, DataModule2);
//Form1.ScaleBy(200,100);
Application.Run;
//
CloseHandle(hmutex);
//CloseHandle(OpenMutex(MUTEX_ALL_ACCESS,True,'KB_TD@2008'));
end;
复制代码
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2