/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using GreenshotPlugin.UnmanagedHelpers;
using System;
using System.Collections.Generic;
using System.Text;
namespace GreenshotPlugin.Core {
///
/// EnumWindows wrapper for .NET
///
public class WindowsEnumerator {
///
/// Returns the collection of windows returned by GetWindows
///
public IList Items { get; private set; }
///
/// Gets all child windows of the specified window
///
/// Window Handle to get children for
/// Window Classname to copy, use null to copy all
public WindowsEnumerator GetWindows(IntPtr hWndParent, string classname) {
Items = new List();
User32.EnumChildWindows(hWndParent, WindowEnum, IntPtr.Zero);
bool hasParent = !IntPtr.Zero.Equals(hWndParent);
string parentText = null;
if (hasParent) {
var title = new StringBuilder(260, 260);
User32.GetWindowText(hWndParent, title, title.Capacity);
parentText = title.ToString();
}
var windows = new List();
foreach (var window in Items) {
if (hasParent) {
window.Text = parentText;
window.ParentHandle = hWndParent;
}
if (classname == null || window.ClassName.Equals(classname)) {
windows.Add(window);
}
}
Items = windows;
return this;
}
///
/// The enum Windows callback.
///
/// Window Handle
/// Application defined value
/// 1 to continue enumeration, 0 to stop
private int WindowEnum(IntPtr hWnd, int lParam)
{
return OnWindowEnum(hWnd) ? 1 : 0;
}
///
/// Called whenever a new window is about to be added
/// by the Window enumeration called from GetWindows.
/// If overriding this function, return true to continue
/// enumeration or false to stop. If you do not call
/// the base implementation the Items collection will
/// be empty.
///
/// Window handle to add
/// True to continue enumeration, False to stop
private bool OnWindowEnum(IntPtr hWnd) {
if (!WindowDetails.IsIgnoreHandle(hWnd)) {
Items.Add(new WindowDetails(hWnd));
}
return true;
}
}
}