Search code examples
windowscygwin

How can I list visible windows and their ID?


Being a linux fan, when I have to work on windows I use cygwin and a virtual desktop called VirtuaWin. I have it set up so that I have 12 active desktops (two screens each) and can quickly switch using Alt-F1 .. Alt-F12.

Now, since it's windows, it has to be rebooted on a regular basis. Every time that happens, I need to relaunch dozens of applications and move them to the desktop I'm used to have them on (e.g. eclipse and other code-dev on #1, R on #3, chrome on #4, outlook on #8, etc.) What I'd like to do is write a script that opens all these applications and moves their windows to the appropriate desktop.

I saw that there is a command-line use of VirtuaWin to effect certain functions on open windows, i.e. VirtuaWin.exe -msg <Msg> <wParam> <lParam> where "<Msg>, <wParam> and <lParam> are numbers as per the standard Windows SendMessage function" (as per the help built in VirtuaWin). Apparently, VirtuaWin.exe -msg 1049 $wn 2 would move window with the ID $wn to the 2nd desktop.

Great. But the question is: how, from e.g. a bash script, do I get the window IDs for each application?


Solution

  • Maybe you'll find this example useful.

    #include <windows.h>
    #include <stdio.h>
    
    
    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
    
      printf("HWND: %u\n", hwnd);
    
      char className[256];
      if (GetClassName(hwnd, className, sizeof className) == 0)
        fprintf(stderr, "GetClassName failed.\n");
      else
        printf("Class Name: %s\n", className);
    
      char windowText[256];  // text in the window's title bar
      if (GetWindowText(hwnd, windowText, sizeof windowText) == 0)
        fprintf(stderr, "GetWindowText failed.\n");
      else
        printf("Window Text: %s\n", windowText);
    
      putchar('\n');
    
      return TRUE;
    }
    
    
    int main() {
    
      BOOL ret = EnumDesktopWindows(
        NULL,             // Desktop to enumerate (NULL is default)
        EnumWindowsProc,  // Callback function
        0);               // lParam value to callback function
    
      if (ret == 0) fprintf(stderr, "EnumDesktopWindows failed.\n");
    
      return 0;
    }