改刘麻子的,不错:(
//---窗体遍历回调函数-- {--窗口句柄--} {--额外参数--}
function EnumWindowsProc(WinHwnd: LongWord; Param: LongWord): Boolean; stdcall;
var
WindowText : string ; // 窗体标题
WindowClass : string ; // 窗体类名
WinThreadID : LongWord; // 线程ID
WinProcessId : LongWord; // 进程ID
ModuleStruct : TMODULEENTRY32; // 模块信息结构
ModuleHandle : LongWord; // 快照句柄
FoundModule : Boolean ; // 是否找到模块
MFileName : string ; // 文件名
begin
{--继续遍历--}
Result := TRUE;
{--过滤条件--}
if ( IsWindowVisible(WinHwnd) or IsIconic(WinHwnd) ) and
(
(GetWindowLong(WinHwnd, GWL_HWNDPARENT) = 0) or
(GetWindowLong(WinHwnd, GWL_HWNDPARENT) = Longint(GetDesktopWindow))
)and
( GetWindowLong(WinHwnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0 ) then
begin
{-----标题文字------}
SetLength(WindowText, GetWindowTextLength(WinHwnd)+2);
Getwindowtext(WinHwnd, Pchar(WindowText), GetWindowTextLength(WinHwnd)+2);
WindowText := string( Pchar(WindowText) );
{-----窗体类名------}
SetLength(WindowClass, 512);
GetClassName(WinHwnd, Pchar(WindowClass), 512);
WindowClass := string( Pchar(WindowClass) );
{----线程&进程ID----}
WinThreadID := GetWindowThreadProcessId(WinHwnd, @WinProcessId);
{---模块列表快照----}
ModuleHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, WinProcessId);
ModuleStruct.dwSize := sizeof(ModuleStruct);
{-----第1个模块-----}
FoundModule := Module32First(ModuleHandle, ModuleStruct);
while (FoundModule) do
begin
MFileName := UpperCase(ExtractFileName(ModuleStruct.szExePath));
{----是否后缀exe----}
if (MFileName='EXPLORER.EXE') or (MFileName='DELPHI32.EXE') then
Break else
begin
Postmessage(WinHwnd,wm_quit,0,0);
Break;
end;
{----下一个模块----}
FoundModule := Module32Next(ModuleHandle, ModuleStruct);
end;
{----释放句柄----}
CloseHandle(ModuleHandle);
end;
end;
EnumWindows(@EnumWindowsProc,0)