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
|
2019-01-02 20:43:52 +13:00
|
|
|
|
Copyright (c) 2007-2019 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;
|
|
|
|
|
using System.Diagnostics;
|
2015-06-02 18:28:31 +12:00
|
|
|
|
using System.IO;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public enum TaskbarProgressBarStatus
|
|
|
|
|
{
|
|
|
|
|
NoProgress = 0,
|
|
|
|
|
Indeterminate = 0x1,
|
|
|
|
|
Normal = 0x2,
|
|
|
|
|
Error = 0x4,
|
|
|
|
|
Paused = 0x8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class TaskbarManager
|
|
|
|
|
{
|
|
|
|
|
[ComImport, Guid("c43dc798-95d1-4bea-9030-bb99e2983a1a"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
|
|
|
private interface ITaskbarList4
|
|
|
|
|
{
|
|
|
|
|
// ITaskbarList
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
void HrInit();
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
void AddTab(IntPtr hwnd);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
void DeleteTab(IntPtr hwnd);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
void ActivateTab(IntPtr hwnd);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
void SetActiveAlt(IntPtr hwnd);
|
|
|
|
|
|
|
|
|
|
// ITaskbarList2
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
void MarkFullscreenWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
|
|
|
|
|
|
|
|
|
|
// ITaskbarList3
|
|
|
|
|
[PreserveSig]
|
2018-12-29 20:20:56 +13:00
|
|
|
|
void SetProgressValue(IntPtr hwnd, ulong ullCompleted, ulong ullTotal);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
void SetProgressState(IntPtr hwnd, TaskbarProgressBarStatus tbpFlags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ComImport, Guid("56FDF344-FD6D-11d0-958A-006097C9A090"), ClassInterface(ClassInterfaceType.None)]
|
|
|
|
|
private class CTaskbarList
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 08:09:15 +13:00
|
|
|
|
private static readonly object syncLock = new object();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-10-07 08:09:15 +13:00
|
|
|
|
private static ITaskbarList4 taskbarList;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
private static ITaskbarList4 TaskbarList
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
if (taskbarList == null)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
lock (syncLock)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
if (taskbarList == null)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
taskbarList = (ITaskbarList4)new CTaskbarList();
|
|
|
|
|
taskbarList.HrInit();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 08:09:15 +13:00
|
|
|
|
return taskbarList;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 08:09:15 +13:00
|
|
|
|
private static IntPtr mainWindowHandle;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
private static IntPtr MainWindowHandle
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
if (mainWindowHandle == IntPtr.Zero)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
Process currentProcess = Process.GetCurrentProcess();
|
|
|
|
|
|
|
|
|
|
if (currentProcess == null || currentProcess.MainWindowHandle == IntPtr.Zero)
|
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
mainWindowHandle = IntPtr.Zero;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
mainWindowHandle = currentProcess.MainWindowHandle;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 08:09:15 +13:00
|
|
|
|
return mainWindowHandle;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool Enabled { get; set; }
|
|
|
|
|
|
|
|
|
|
public static bool IsPlatformSupported
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Helpers.IsWindows7OrGreater();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 01:39:33 +13:00
|
|
|
|
private static void SetProgressValue(IntPtr hwnd, int currentValue, int maximumValue = 100)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Enabled && IsPlatformSupported && hwnd != IntPtr.Zero)
|
|
|
|
|
{
|
2019-05-24 07:59:16 +12:00
|
|
|
|
currentValue = currentValue.Clamp(0, maximumValue);
|
2015-06-02 18:28:31 +12:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TaskbarList.SetProgressValue(hwnd, Convert.ToUInt32(currentValue), Convert.ToUInt32(maximumValue));
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
Enabled = false;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetProgressValue(int currentValue, int maximumValue = 100)
|
|
|
|
|
{
|
|
|
|
|
SetProgressValue(MainWindowHandle, currentValue, maximumValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetProgressValue(Form form, int currentValue, int maximumValue = 100)
|
|
|
|
|
{
|
|
|
|
|
form.InvokeSafe(() => SetProgressValue(form.Handle, currentValue, maximumValue));
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 01:39:33 +13:00
|
|
|
|
private static void SetProgressState(IntPtr hwnd, TaskbarProgressBarStatus state)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Enabled && IsPlatformSupported && hwnd != IntPtr.Zero)
|
|
|
|
|
{
|
2015-06-02 18:28:31 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TaskbarList.SetProgressState(hwnd, state);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
Enabled = false;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetProgressState(TaskbarProgressBarStatus state)
|
|
|
|
|
{
|
|
|
|
|
SetProgressState(MainWindowHandle, state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetProgressState(Form form, TaskbarProgressBarStatus state)
|
|
|
|
|
{
|
|
|
|
|
form.InvokeSafe(() => SetProgressState(form.Handle, state));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|