program MyThreadTest;
uses
Windows
Forms
SysUtils
Messages
Dialogs
Unit
{$R *
var
myMutex
FindHid: HWND;
MoudleName: string;
function EnumWndProc(hwnd: Thandle; param: Cardinal): bool; stdcall;
//由於用於api回調函數
var
ClassName
WinInstance: THandle;
begin
result := true;
SetLength(ClassName
GetClassName(hwnd
ClassName := pchar(ClassName); //在字符串後加結束符
if UpperCase(ClassName) = UpperCase(TForm
begin
WinInstance := GetWindowLong(hwnd
setlength(WinMoudleName
//獲得當前遍歷窗口的程序文件名
GetModuleFileName(WinInstance
WinMoudleName := pchar(WinMoudleName);
WinMoudleName :=ExtractFileName(WinMoudleName);
//MoudleName為工程全局變量
if UpperCase(WinMoudleName) = UpperCase(MoudleName) then
begin
FindHid := hwnd;//FindHid為工程全局變量保存找到的句炳
result := false; //找到以後就結束遍歷
end;
end;
end;
begin
// CreateMutex建立互斥對象
myMutex := CreateMutex(nil
if WaitForSingleObject(myMutex
//程序沒有被運行過
begin
Application
Application
Application
end else
begin
SetLength(MoudleName
//獲得自己程序文件名
GetModuleFileName(HInstance
MoudleName := pchar(MoudleName);
MoudleName := ExtractFileName(MoudleName);
EnumWindows(@EnumWndProc
if FindHid <>
begin
ShowWindow(FindHid
SetForegroundWindow(FindHid);
end;
end;
end
[EnumWindows函數使用]
EnumWindows 用來列舉屏幕上所有頂層窗口
MSDN
The EnumWindows function enumerates all top
[
From:http://tw.wingwit.com/Article/program/Delphi/201311/24833.html