program MyThreadTest;
uses
Windows,
Forms,
SysUtils,
Messages,
Dialogs,
Unit1 in 'Unit1.pas' {Form1},
{$R *.res}
var
myMutex,
FindHid: HWND;
MoudleName: string;
function EnumWndProc(hwnd: Thandle; param: Cardinal): bool; stdcall;
//由于用于api回調(diào)函數(shù),請使用windows傳統(tǒng)的參數(shù)傳遞方式stdcall
var
ClassName, WinMoudleName: string;
WinInstance: THandle;
begin
result := true;
SetLength(ClassName, 100);
GetClassName(hwnd, pchar(ClassName), length(ClassName)); //獲得當(dāng)前遍歷窗口的類名
ClassName := pchar(ClassName); //在字符串后加結(jié)束符,確定字符串結(jié)束
if UpperCase(ClassName) = UpperCase(TForm1.ClassName) then //比較類名
begin
WinInstance := GetWindowLong(hwnd, GWL_HINSTANCE); //獲得當(dāng)前遍歷窗口的實(shí)例
setlength(WinMoudleName, 100);
//獲得當(dāng)前遍歷窗口的程序文件名
GetModuleFileName(WinInstance, pchar(WinMoudleName), length(WinMoudleName));
WinMoudleName := pchar(WinMoudleName);
WinMoudleName :=ExtractFileName(WinMoudleName);
//MoudleName為工程全局變量,自身程序的文件名
if UpperCase(WinMoudleName) = UpperCase(MoudleName) then
begin
FindHid := hwnd;//FindHid為工程全局變量保存找到的句炳
result := false; //找到以后就結(jié)束遍歷
end;
end;
end;
begin
// CreateMutex建立互斥對(duì)象,并且給互斥對(duì)象起一個(gè)的名字
myMutex := CreateMutex(nil, false, 'hkOneCopy');
if WaitForSingleObject(myMutex, 0) <> wait_TimeOut then
//程序沒有被運(yùn)行過
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end else
begin
SetLength(MoudleName, 100);
//獲得自己程序文件名
GetModuleFileName(HInstance, pchar(MoudleName), length(MoudleName));
MoudleName := pchar(MoudleName);
MoudleName := ExtractFileName(MoudleName);
EnumWindows(@EnumWndProc, 0); //調(diào)用枚舉函數(shù)
if FindHid <> 0 then
begin
ShowWindow(FindHid,SW_RESTORE);
SetForegroundWindow(FindHid);
end;
end;
end.
[EnumWindows函數(shù)使用]:
EnumWindows 用來列舉屏幕上所有頂層窗口。
MSDN:
The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window。
uses
Windows,
Forms,
SysUtils,
Messages,
Dialogs,
Unit1 in 'Unit1.pas' {Form1},
{$R *.res}
var
myMutex,
FindHid: HWND;
MoudleName: string;
function EnumWndProc(hwnd: Thandle; param: Cardinal): bool; stdcall;
//由于用于api回調(diào)函數(shù),請使用windows傳統(tǒng)的參數(shù)傳遞方式stdcall
var
ClassName, WinMoudleName: string;
WinInstance: THandle;
begin
result := true;
SetLength(ClassName, 100);
GetClassName(hwnd, pchar(ClassName), length(ClassName)); //獲得當(dāng)前遍歷窗口的類名
ClassName := pchar(ClassName); //在字符串后加結(jié)束符,確定字符串結(jié)束
if UpperCase(ClassName) = UpperCase(TForm1.ClassName) then //比較類名
begin
WinInstance := GetWindowLong(hwnd, GWL_HINSTANCE); //獲得當(dāng)前遍歷窗口的實(shí)例
setlength(WinMoudleName, 100);
//獲得當(dāng)前遍歷窗口的程序文件名
GetModuleFileName(WinInstance, pchar(WinMoudleName), length(WinMoudleName));
WinMoudleName := pchar(WinMoudleName);
WinMoudleName :=ExtractFileName(WinMoudleName);
//MoudleName為工程全局變量,自身程序的文件名
if UpperCase(WinMoudleName) = UpperCase(MoudleName) then
begin
FindHid := hwnd;//FindHid為工程全局變量保存找到的句炳
result := false; //找到以后就結(jié)束遍歷
end;
end;
end;
begin
// CreateMutex建立互斥對(duì)象,并且給互斥對(duì)象起一個(gè)的名字
myMutex := CreateMutex(nil, false, 'hkOneCopy');
if WaitForSingleObject(myMutex, 0) <> wait_TimeOut then
//程序沒有被運(yùn)行過
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end else
begin
SetLength(MoudleName, 100);
//獲得自己程序文件名
GetModuleFileName(HInstance, pchar(MoudleName), length(MoudleName));
MoudleName := pchar(MoudleName);
MoudleName := ExtractFileName(MoudleName);
EnumWindows(@EnumWndProc, 0); //調(diào)用枚舉函數(shù)
if FindHid <> 0 then
begin
ShowWindow(FindHid,SW_RESTORE);
SetForegroundWindow(FindHid);
end;
end;
end.
[EnumWindows函數(shù)使用]:
EnumWindows 用來列舉屏幕上所有頂層窗口。
MSDN:
The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window。