Mega Code Archive

 
Categories / Delphi / Examples
 

Traverse the global list of all windows

Sometimes you may want to do something with all windows (and controls) on the screen, including non-Delphi windows. For such a purpose, you will use the API function EnumWindows. The following code includes the calls MakeProcInstance/ FreeProcInstance, which are needed in 16bit-Windows (including Delphi 1 under Win95). This sample code hides every existing window.. a rather useless example, but after all, it's just an example. function NextWindow (Wnd : HWnd;Form : TForm1) : Boolean; export; {$ifdef Win32} stdcall; {$endif} begin ShowWindow (Wnd, SW_HIDE); NextWindow := true; { next window, please } end; procedure TForm1.Sample; var EnumProc: TFarProc; begin { this works in Win32 } EnumWindows(@NextWindow,LongInt(Self)); { MakeProcInstance for Win16 } EnumProc := MakeProcInstance(@NextWindow, HInstance); EnumWindows (EnumProc, 0); FreeProcInstance(EnumProc); end;