Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top