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
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 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)
|
|
|
|
|
|
|
|
|
|
using System;
|
2014-03-25 14:26:08 +13:00
|
|
|
|
using System.Diagnostics;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
2017-12-06 03:59:18 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public class WindowInfo
|
|
|
|
|
{
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public IntPtr Handle { get; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-12-20 00:19:38 +13:00
|
|
|
|
public bool IsHandleCreated => Handle != IntPtr.Zero;
|
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public string Text => NativeMethods.GetWindowText(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public string ClassName => NativeMethods.GetClassName(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public Process Process => NativeMethods.GetProcessByWindowHandle(Handle);
|
2014-03-25 14:26:08 +13:00
|
|
|
|
|
2017-03-01 22:44:39 +13:00
|
|
|
|
public string ProcessName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
using (Process process = Process)
|
|
|
|
|
{
|
|
|
|
|
return process?.ProcessName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-18 01:00:20 +13:00
|
|
|
|
|
2021-01-17 06:10:06 +13:00
|
|
|
|
public string ProcessFilePath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
using (Process process = Process)
|
|
|
|
|
{
|
|
|
|
|
return process?.MainModule?.FileName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 18:34:18 +12:00
|
|
|
|
public string ProcessFileName => FileHelpers.GetFileNameSafe(ProcessFilePath);
|
2021-01-17 06:10:06 +13:00
|
|
|
|
|
|
|
|
|
public int ProcessId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
using (Process process = Process)
|
|
|
|
|
{
|
|
|
|
|
return process.Id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public Rectangle Rectangle => CaptureHelpers.GetWindowRectangle(Handle);
|
2015-12-18 01:00:20 +13:00
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public Rectangle ClientRectangle => NativeMethods.GetClientRect(Handle);
|
2014-09-13 18:50:17 +12:00
|
|
|
|
|
2021-09-15 14:12:23 +12:00
|
|
|
|
public WindowStyles Style
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (WindowStyles)(ulong)NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_STYLE);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.SetWindowLong(Handle, NativeConstants.GWL_STYLE, (IntPtr)value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WindowStyles ExStyle
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (WindowStyles)(ulong)NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_EXSTYLE);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.SetWindowLong(Handle, NativeConstants.GWL_EXSTYLE, (IntPtr)value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2022-03-05 22:26:16 +13:00
|
|
|
|
public bool TopMost
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ExStyle.HasFlag(WindowStyles.WS_EX_TOPMOST);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetWindowPos(value ? SpecialWindowHandles.HWND_TOPMOST : SpecialWindowHandles.HWND_NOTOPMOST,
|
|
|
|
|
SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public Icon Icon => NativeMethods.GetApplicationIcon(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public bool IsMaximized => NativeMethods.IsZoomed(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-07-04 18:11:35 +12:00
|
|
|
|
public bool IsMinimized => NativeMethods.IsIconic(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-10-16 09:41:25 +13:00
|
|
|
|
public bool IsVisible => NativeMethods.IsWindowVisible(Handle) && !IsCloaked;
|
|
|
|
|
|
|
|
|
|
public bool IsCloaked => NativeMethods.IsWindowCloaked(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2021-06-18 05:10:38 +12:00
|
|
|
|
public bool IsActive => NativeMethods.IsActive(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
public WindowInfo(IntPtr handle)
|
|
|
|
|
{
|
|
|
|
|
Handle = handle;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 20:12:03 +12:00
|
|
|
|
public void Activate()
|
|
|
|
|
{
|
2018-12-20 00:19:38 +13:00
|
|
|
|
if (IsHandleCreated)
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.SetForegroundWindow(Handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Restore()
|
|
|
|
|
{
|
|
|
|
|
if (IsHandleCreated)
|
|
|
|
|
{
|
2018-12-20 01:08:21 +13:00
|
|
|
|
NativeMethods.ShowWindow(Handle, (int)WindowShowStyle.Restore);
|
2018-12-20 00:19:38 +13:00
|
|
|
|
}
|
2015-09-25 20:12:03 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 14:12:23 +12:00
|
|
|
|
public void SetWindowPos(SetWindowPosFlags flags)
|
|
|
|
|
{
|
2022-03-05 22:26:16 +13:00
|
|
|
|
SetWindowPos(SpecialWindowHandles.HWND_TOP, 0, 0, 0, 0, flags);
|
2021-09-15 14:12:23 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetWindowPos(Rectangle rect, SetWindowPosFlags flags)
|
|
|
|
|
{
|
2022-03-05 22:26:16 +13:00
|
|
|
|
SetWindowPos(SpecialWindowHandles.HWND_TOP, rect.X, rect.Y, rect.Width, rect.Height, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetWindowPos(SpecialWindowHandles specialWindowHandles, SetWindowPosFlags flags)
|
|
|
|
|
{
|
|
|
|
|
SetWindowPos(specialWindowHandles, 0, 0, 0, 0, flags);
|
2021-09-15 14:12:23 +12:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 22:26:16 +13:00
|
|
|
|
public void SetWindowPos(SpecialWindowHandles specialWindowHandles, int x, int y, int width, int height, SetWindowPosFlags flags)
|
2021-09-15 14:12:23 +12:00
|
|
|
|
{
|
2022-03-05 22:26:16 +13:00
|
|
|
|
NativeMethods.SetWindowPos(Handle, (IntPtr)specialWindowHandles, x, y, width, height, flags);
|
2021-09-15 14:12:23 +12:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|