Default
Google

How can I get the list of all opened windows?

Applies To: C++Builder 1 or higher
Category: Knowledge Base

Applications such as WinSight 32® shipped in the C++Builder package can retrieve the list of all opened windows and show their corresponding class. How can I do this in my own application?
 
 
 1.
  Start a new project using File | New Application.
 
 2.
  Add a ListView. Set its ViewStyle to vsReport. Edit the
      Columns property and add two columns. The first one will contain the
      caption and the second one the class of the window.
 
 3.
  Declare the callback function by inserting the following in
      your source code:
 
bool CALLBACK EnumWindowsProc(HWND hWnd, TListView *ListView1)
{
    char WindowName[80], ClassName[80];
 
    GetWindowText(hWnd, WindowName, 80);
    GetClassName(hWnd, ClassName, 80);
 
    TListItem *item;
    item = ListView1->Items->Add();
    item->Caption = WindowName;
    item->SubItems->Add(ClassName);
 
    return true;
}
 
 4.
  Add a Button. Double-click its OnClick event:
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    EnumWindows((WNDENUMPROC)EnumWindowsProc,
      (LPARAM)ListView1);
}
 
The Win32 API function EnumWindows enumerates each window handle-by-handle until all the windows are finished. Each time a window is checked, the function sends the information the callback function EnumWindowsProc.
Once you got the handle of a window, it is easy to get the information. You can use GetWindowText to retrieve the caption and GetClassName to retrieve the class name.

C++Builder Developer's Network
Copyright © Yoto Yotov



Acquiring image from ProHosting Banner Exchange