1

I want to get all Windows that I could take a screenshot, application windows. I am trying to use EnumWindows.

public delegate bool CallBackPtr(IntPtr hwnd, int lParam);

[DllImport("user32.dll")]
private static extern int EnumWindows(CallBackPtr callPtr, int lPar);

public static List<IntPtr> EnumWindows()
{
    var result = new List<IntPtr>();

    EnumWindows(new User32.CallBackPtr((hwnd, lParam) =>
    {
        result.Add(hwnd);
        return true;
    }), 0);

    return result;
}

However this is returning more Windows than I expect, such as:

tooltip

tooltip

blackthing

I want to grab only Windows like Visual Studio, Skype, Explorer, Chrome...

Should I be using another method? Or how do I check if it is a window I want to grab?

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • As you have handles of windows try to filter them by style. Explore [this](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633584(v=vs.85).aspx) for more information. – Hamlet Hakobyan Feb 01 '14 at 20:22

1 Answers1

5

Perhaps checking the window's style for a title bar will do what you want:

[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

static bool IsAppWindow(IntPtr hWnd)
{
    int style = GetWindowLong(hWnd, -16); // GWL_STYLE

    // check for WS_VISIBLE and WS_CAPTION flags
    // (that the window is visible and has a title bar)
    return (style & 0x10C00000) == 0x10C00000;
}

But this won't work for some of the fancier apps which are fully custom drawn.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110