2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2021-07-29 15:22:51 +12:00
|
|
|
|
Copyright (c) 2007-2021 ShareX Team
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
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 2
|
|
|
|
|
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, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
2016-01-06 05:18:22 +13:00
|
|
|
|
using System.Threading;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-09-13 19:36:16 +12:00
|
|
|
|
public class WindowsRectangleList
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-08-07 14:22:45 +12:00
|
|
|
|
public IntPtr IgnoreHandle { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public bool IncludeChildWindows { get; set; }
|
|
|
|
|
|
2015-09-25 18:39:08 +12:00
|
|
|
|
private List<SimpleWindowInfo> windows;
|
2015-10-27 12:05:40 +13:00
|
|
|
|
private HashSet<IntPtr> parentHandles;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-01-06 05:18:22 +13:00
|
|
|
|
public List<SimpleWindowInfo> GetWindowInfoListAsync(int timeout)
|
|
|
|
|
{
|
|
|
|
|
List<SimpleWindowInfo> windowInfoList = null;
|
|
|
|
|
|
|
|
|
|
Thread t = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
windowInfoList = GetWindowInfoList();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
t.Start();
|
|
|
|
|
|
|
|
|
|
if (!t.Join(timeout))
|
|
|
|
|
{
|
|
|
|
|
t.Abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return windowInfoList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SimpleWindowInfo> GetWindowInfoList()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-09-25 18:39:08 +12:00
|
|
|
|
windows = new List<SimpleWindowInfo>();
|
2015-10-27 12:05:40 +13:00
|
|
|
|
parentHandles = new HashSet<IntPtr>();
|
|
|
|
|
|
2016-11-21 18:37:42 +13:00
|
|
|
|
EnumWindowsProc ewp = EvalWindow;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
NativeMethods.EnumWindows(ewp, IntPtr.Zero);
|
|
|
|
|
|
2015-09-25 18:39:08 +12:00
|
|
|
|
List<SimpleWindowInfo> result = new List<SimpleWindowInfo>();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-09-25 18:39:08 +12:00
|
|
|
|
foreach (SimpleWindowInfo window in windows)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
bool rectVisible = true;
|
|
|
|
|
|
2016-01-09 12:24:13 +13:00
|
|
|
|
if (!window.IsWindow)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-01-09 12:24:13 +13:00
|
|
|
|
foreach (SimpleWindowInfo window2 in result)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-01-09 12:24:13 +13:00
|
|
|
|
if (window2.Rectangle.Contains(window.Rectangle))
|
|
|
|
|
{
|
|
|
|
|
rectVisible = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rectVisible)
|
|
|
|
|
{
|
2015-09-25 18:39:08 +12:00
|
|
|
|
result.Add(window);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-13 18:50:17 +12:00
|
|
|
|
private bool EvalWindow(IntPtr hWnd, IntPtr lParam)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-09-13 18:50:17 +12:00
|
|
|
|
return CheckHandle(hWnd, true);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-13 18:50:17 +12:00
|
|
|
|
private bool EvalControl(IntPtr hWnd, IntPtr lParam)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-09-13 18:50:17 +12:00
|
|
|
|
return CheckHandle(hWnd, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckHandle(IntPtr handle, bool isWindow)
|
|
|
|
|
{
|
2016-10-16 11:12:15 +13:00
|
|
|
|
if (handle == IgnoreHandle || !NativeMethods.IsWindowVisible(handle) || (isWindow && NativeMethods.IsWindowCloaked(handle)))
|
2014-09-13 18:50:17 +12:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-09-25 18:39:08 +12:00
|
|
|
|
SimpleWindowInfo windowInfo = new SimpleWindowInfo(handle);
|
2014-09-13 18:50:17 +12:00
|
|
|
|
|
|
|
|
|
if (isWindow)
|
|
|
|
|
{
|
2015-12-18 01:00:20 +13:00
|
|
|
|
windowInfo.IsWindow = true;
|
2015-09-25 18:39:08 +12:00
|
|
|
|
windowInfo.Rectangle = CaptureHelpers.GetWindowRectangle(handle);
|
2014-09-13 18:50:17 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-09-25 18:39:08 +12:00
|
|
|
|
windowInfo.Rectangle = NativeMethods.GetWindowRect(handle);
|
2014-09-13 18:50:17 +12:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 18:39:08 +12:00
|
|
|
|
if (!windowInfo.Rectangle.IsValid())
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 12:05:40 +13:00
|
|
|
|
if (IncludeChildWindows && !parentHandles.Contains(handle))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-10-27 12:05:40 +13:00
|
|
|
|
parentHandles.Add(handle);
|
|
|
|
|
|
2016-11-21 18:37:42 +13:00
|
|
|
|
EnumWindowsProc ewp = EvalControl;
|
2014-09-13 18:50:17 +12:00
|
|
|
|
NativeMethods.EnumChildWindows(handle, ewp, IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isWindow)
|
|
|
|
|
{
|
|
|
|
|
Rectangle clientRect = NativeMethods.GetClientRect(handle);
|
|
|
|
|
|
2016-01-07 01:48:33 +13:00
|
|
|
|
if (clientRect.IsValid() && clientRect != windowInfo.Rectangle)
|
2014-09-13 18:50:17 +12:00
|
|
|
|
{
|
2015-09-25 18:39:08 +12:00
|
|
|
|
windows.Add(new SimpleWindowInfo(handle, clientRect));
|
2014-09-13 18:50:17 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 18:39:08 +12:00
|
|
|
|
windows.Add(windowInfo);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|