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)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib.Properties;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-04-21 08:50:31 +12:00
|
|
|
|
using System.Reflection;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Text;
|
2018-08-04 01:54:48 +12:00
|
|
|
|
using System.Threading.Tasks;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Encoder = System.Drawing.Imaging.Encoder;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public static class Extensions
|
|
|
|
|
{
|
|
|
|
|
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
|
|
|
|
{
|
|
|
|
|
if (source == null) throw new ArgumentNullException("source");
|
|
|
|
|
if (action == null) throw new ArgumentNullException("action");
|
|
|
|
|
|
|
|
|
|
foreach (T item in source)
|
|
|
|
|
{
|
|
|
|
|
action(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first,
|
|
|
|
|
IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
|
|
|
|
|
{
|
|
|
|
|
using (IEnumerator<TFirst> e1 = first.GetEnumerator())
|
|
|
|
|
using (IEnumerator<TSecond> e2 = second.GetEnumerator())
|
|
|
|
|
{
|
|
|
|
|
while (e1.MoveNext() && e2.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
yield return resultSelector(e1.Current, e2.Current);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] GetBytes(this Image img)
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
img.Save(ms, img.RawFormat);
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Stream GetStream(this Image img)
|
|
|
|
|
{
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
img.Save(ms, img.RawFormat);
|
|
|
|
|
return ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ImageCodecInfo GetCodecInfo(this ImageFormat format)
|
|
|
|
|
{
|
|
|
|
|
return ImageCodecInfo.GetImageEncoders().FirstOrDefault(info => info.FormatID.Equals(format.Guid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetMimeType(this ImageFormat format)
|
|
|
|
|
{
|
|
|
|
|
ImageCodecInfo codec = format.GetCodecInfo();
|
|
|
|
|
|
|
|
|
|
if (codec != null) return codec.MimeType;
|
|
|
|
|
|
|
|
|
|
return "image/unknown";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsValidIndex<T>(this T[] array, int index)
|
|
|
|
|
{
|
|
|
|
|
return array != null && index >= 0 && index < array.Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsValidIndex<T>(this List<T> list, int index)
|
|
|
|
|
{
|
|
|
|
|
return list != null && index >= 0 && index < list.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T ReturnIfValidIndex<T>(this T[] array, int index)
|
|
|
|
|
{
|
|
|
|
|
if (array.IsValidIndex(index)) return array[index];
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T ReturnIfValidIndex<T>(this List<T> list, int index)
|
|
|
|
|
{
|
|
|
|
|
if (list.IsValidIndex(index)) return list[index];
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Last<T>(this T[] array, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (array.Length > index) return array[array.Length - index - 1];
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Last<T>(this List<T> list, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (list.Count > index) return list[list.Count - index - 1];
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double ToDouble(this Version value)
|
|
|
|
|
{
|
2018-05-17 01:27:11 +12:00
|
|
|
|
return (Math.Max(value.Major, 0) * Math.Pow(10, 12)) +
|
|
|
|
|
(Math.Max(value.Minor, 0) * Math.Pow(10, 9)) +
|
|
|
|
|
(Math.Max(value.Build, 0) * Math.Pow(10, 6)) +
|
2014-05-29 17:39:06 +12:00
|
|
|
|
Math.Max(value.Revision, 0);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsValid(this Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
return rect.Width > 0 && rect.Height > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 00:53:18 +12:00
|
|
|
|
public static Point Add(this Point point, int offset)
|
|
|
|
|
{
|
|
|
|
|
return point.Add(offset, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Point Add(this Point point, int offsetX, int offsetY)
|
|
|
|
|
{
|
|
|
|
|
return new Point(point.X + offsetX, point.Y + offsetY);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 22:13:16 +12:00
|
|
|
|
public static Point Add(this Point point, Point offset)
|
|
|
|
|
{
|
|
|
|
|
return new Point(point.X + offset.X, point.Y + offset.Y);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-24 08:10:51 +13:00
|
|
|
|
public static Size Offset(this Size size, int offset)
|
|
|
|
|
{
|
2016-08-24 06:02:52 +12:00
|
|
|
|
return size.Offset(offset, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Size Offset(this Size size, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
return new Size(size.Width + width, size.Height + height);
|
2013-12-24 08:10:51 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-28 12:53:38 +12:00
|
|
|
|
public static Rectangle Offset(this Rectangle rect, int offset)
|
2013-12-24 08:10:51 +13:00
|
|
|
|
{
|
2018-05-17 01:27:11 +12:00
|
|
|
|
return new Rectangle(rect.X - offset, rect.Y - offset, rect.Width + (offset * 2), rect.Height + (offset * 2));
|
2013-12-24 08:10:51 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Rectangle LocationOffset(this Rectangle rect, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
return new Rectangle(rect.X + x, rect.Y + y, rect.Width, rect.Height);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-14 10:34:30 +12:00
|
|
|
|
public static Rectangle LocationOffset(this Rectangle rect, Point offset)
|
|
|
|
|
{
|
|
|
|
|
return rect.LocationOffset(offset.X, offset.Y);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-28 12:53:38 +12:00
|
|
|
|
public static Rectangle LocationOffset(this Rectangle rect, int offset)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-08-28 12:53:38 +12:00
|
|
|
|
return rect.LocationOffset(offset, offset);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Rectangle SizeOffset(this Rectangle rect, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
return new Rectangle(rect.X, rect.Y, rect.Width + width, rect.Height + height);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-28 12:53:38 +12:00
|
|
|
|
public static Rectangle SizeOffset(this Rectangle rect, int offset)
|
|
|
|
|
{
|
|
|
|
|
return rect.SizeOffset(offset, offset);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public static string Join<T>(this T[] array, string separator = " ")
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
if (array != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (T t in array)
|
|
|
|
|
{
|
|
|
|
|
if (sb.Length > 0 && !string.IsNullOrEmpty(separator)) sb.Append(separator);
|
|
|
|
|
sb.Append(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void BeginUpdate(this RichTextBox rtb)
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.SendMessage(rtb.Handle, (int)WindowsMessages.SETREDRAW, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void EndUpdate(this RichTextBox rtb)
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.SendMessage(rtb.Handle, (int)WindowsMessages.SETREDRAW, 1, 0);
|
|
|
|
|
rtb.Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 10:38:02 +13:00
|
|
|
|
public static void AddContextMenu(this RichTextBox rtb)
|
|
|
|
|
{
|
|
|
|
|
if (rtb.ContextMenuStrip == null)
|
|
|
|
|
{
|
2019-03-01 03:58:11 +13:00
|
|
|
|
ContextMenuStrip cms = new ContextMenuStrip()
|
|
|
|
|
{
|
|
|
|
|
ShowImageMargin = false
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-01 04:44:26 +13:00
|
|
|
|
ToolStripMenuItem tsmiUndo = new ToolStripMenuItem(Resources.Extensions_AddContextMenu_Undo);
|
|
|
|
|
tsmiUndo.Click += (sender, e) => rtb.Undo();
|
|
|
|
|
cms.Items.Add(tsmiUndo);
|
|
|
|
|
|
|
|
|
|
ToolStripMenuItem tsmiRedo = new ToolStripMenuItem(Resources.Extensions_AddContextMenu_Redo);
|
|
|
|
|
tsmiRedo.Click += (sender, e) => rtb.Redo();
|
|
|
|
|
cms.Items.Add(tsmiRedo);
|
|
|
|
|
|
|
|
|
|
cms.Items.Add(new ToolStripSeparator());
|
|
|
|
|
|
2014-10-18 00:08:33 +13:00
|
|
|
|
ToolStripMenuItem tsmiCut = new ToolStripMenuItem(Resources.Extensions_AddContextMenu_Cut);
|
2013-11-25 10:38:02 +13:00
|
|
|
|
tsmiCut.Click += (sender, e) => rtb.Cut();
|
|
|
|
|
cms.Items.Add(tsmiCut);
|
2019-03-01 03:58:11 +13:00
|
|
|
|
|
2014-10-18 00:08:33 +13:00
|
|
|
|
ToolStripMenuItem tsmiCopy = new ToolStripMenuItem(Resources.Extensions_AddContextMenu_Copy);
|
2013-11-25 10:38:02 +13:00
|
|
|
|
tsmiCopy.Click += (sender, e) => rtb.Copy();
|
|
|
|
|
cms.Items.Add(tsmiCopy);
|
2019-03-01 03:58:11 +13:00
|
|
|
|
|
2014-10-18 00:08:33 +13:00
|
|
|
|
ToolStripMenuItem tsmiPaste = new ToolStripMenuItem(Resources.Extensions_AddContextMenu_Paste);
|
2013-11-25 10:38:02 +13:00
|
|
|
|
tsmiPaste.Click += (sender, e) => rtb.Paste();
|
|
|
|
|
cms.Items.Add(tsmiPaste);
|
2019-03-01 03:58:11 +13:00
|
|
|
|
|
2019-03-01 04:44:26 +13:00
|
|
|
|
ToolStripMenuItem tsmiDelete = new ToolStripMenuItem(Resources.Extensions_AddContextMenu_Delete);
|
2019-03-01 04:30:09 +13:00
|
|
|
|
tsmiDelete.Click += (sender, e) => rtb.SelectedText = "";
|
|
|
|
|
cms.Items.Add(tsmiDelete);
|
|
|
|
|
|
|
|
|
|
cms.Items.Add(new ToolStripSeparator());
|
|
|
|
|
|
2019-03-01 04:44:26 +13:00
|
|
|
|
ToolStripMenuItem tsmiSelectAll = new ToolStripMenuItem(Resources.Extensions_AddContextMenu_SelectAll);
|
2019-03-01 04:30:09 +13:00
|
|
|
|
tsmiSelectAll.Click += (sender, e) => rtb.SelectAll();
|
|
|
|
|
cms.Items.Add(tsmiSelectAll);
|
|
|
|
|
|
2019-03-01 03:58:11 +13:00
|
|
|
|
cms.Opening += (sender, e) =>
|
|
|
|
|
{
|
2019-03-01 04:44:26 +13:00
|
|
|
|
tsmiUndo.Enabled = !rtb.ReadOnly && rtb.CanUndo;
|
|
|
|
|
tsmiRedo.Enabled = !rtb.ReadOnly && rtb.CanRedo;
|
2019-03-01 03:58:11 +13:00
|
|
|
|
tsmiCut.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
|
|
|
|
|
tsmiCopy.Enabled = rtb.SelectionLength > 0;
|
|
|
|
|
tsmiPaste.Enabled = !rtb.ReadOnly && Clipboard.ContainsText();
|
2019-03-01 04:30:09 +13:00
|
|
|
|
tsmiDelete.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
|
|
|
|
|
tsmiSelectAll.Enabled = rtb.TextLength > 0 && rtb.SelectionLength < rtb.TextLength;
|
2019-03-01 03:58:11 +13:00
|
|
|
|
};
|
|
|
|
|
|
2013-11-25 10:38:02 +13:00
|
|
|
|
rtb.ContextMenuStrip = cms;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 10:39:10 +13:00
|
|
|
|
public static void SupportSelectAll(this TextBox tb)
|
|
|
|
|
{
|
|
|
|
|
tb.KeyDown += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.Control && e.KeyCode == Keys.A)
|
|
|
|
|
{
|
|
|
|
|
tb.SelectAll();
|
|
|
|
|
e.SuppressKeyPress = true;
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public static void SaveJPG(this Image img, Stream stream, int quality)
|
|
|
|
|
{
|
2019-05-24 07:59:16 +12:00
|
|
|
|
quality = quality.Clamp(0, 100);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
EncoderParameters encoderParameters = new EncoderParameters(1);
|
|
|
|
|
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
|
|
|
|
|
img.Save(stream, ImageFormat.Jpeg.GetCodecInfo(), encoderParameters);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 08:44:58 +12:00
|
|
|
|
public static void SaveJPG(this Image img, string filepath, int quality)
|
|
|
|
|
{
|
2017-07-10 11:04:19 +12:00
|
|
|
|
using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
|
|
|
{
|
|
|
|
|
SaveJPG(img, fs, quality);
|
|
|
|
|
}
|
2014-04-23 08:44:58 +12:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public static void SaveGIF(this Image img, Stream stream, GIFQuality quality)
|
|
|
|
|
{
|
|
|
|
|
if (quality == GIFQuality.Default)
|
|
|
|
|
{
|
|
|
|
|
img.Save(stream, ImageFormat.Gif);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Quantizer quantizer;
|
|
|
|
|
switch (quality)
|
|
|
|
|
{
|
|
|
|
|
case GIFQuality.Grayscale:
|
|
|
|
|
quantizer = new GrayscaleQuantizer();
|
|
|
|
|
break;
|
|
|
|
|
case GIFQuality.Bit4:
|
|
|
|
|
quantizer = new OctreeQuantizer(15, 4);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
case GIFQuality.Bit8:
|
|
|
|
|
quantizer = new OctreeQuantizer(255, 4);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (Bitmap quantized = quantizer.Quantize(img))
|
|
|
|
|
{
|
|
|
|
|
quantized.Save(stream, ImageFormat.Gif);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static long ToUnix(this DateTime dateTime)
|
|
|
|
|
{
|
2015-08-24 07:41:59 +12:00
|
|
|
|
return Helpers.DateTimeToUnix(dateTime);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 05:22:12 +13:00
|
|
|
|
public static void AppendTextToSelection(this TextBoxBase tb, string text)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(text))
|
|
|
|
|
{
|
|
|
|
|
int start = tb.SelectionStart;
|
|
|
|
|
tb.Text = tb.Text.Insert(start, text);
|
|
|
|
|
tb.SelectionStart = start + text.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RadioCheck(this ToolStripMenuItem tsmi)
|
|
|
|
|
{
|
2017-01-16 05:42:31 +13:00
|
|
|
|
ToolStripDropDownItem tsddiParent = tsmi.OwnerItem as ToolStripDropDownItem;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2017-01-16 05:42:31 +13:00
|
|
|
|
foreach (ToolStripMenuItem tsmiChild in tsddiParent.DropDownItems.OfType<ToolStripMenuItem>())
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2017-01-16 05:42:31 +13:00
|
|
|
|
tsmiChild.Checked = tsmiChild == tsmi;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-01 05:11:59 +13:00
|
|
|
|
public static void RadioCheck(this ToolStripButton tsb)
|
|
|
|
|
{
|
|
|
|
|
ToolStrip parent = tsb.GetCurrentParent();
|
|
|
|
|
|
|
|
|
|
foreach (ToolStripButton tsbParent in parent.Items.OfType<ToolStripButton>())
|
|
|
|
|
{
|
|
|
|
|
if (tsbParent != tsb)
|
|
|
|
|
{
|
|
|
|
|
tsbParent.Checked = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tsb.Checked = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-18 02:39:55 +13:00
|
|
|
|
public static void UpdateCheckedAll(this ToolStripMenuItem tsmi, bool check)
|
|
|
|
|
{
|
|
|
|
|
foreach (ToolStripMenuItem tsmiChild in tsmi.DropDownItems.OfType<ToolStripMenuItem>())
|
|
|
|
|
{
|
|
|
|
|
tsmiChild.Checked = check;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public static void InvokeSafe(this Control control, Action action)
|
|
|
|
|
{
|
|
|
|
|
if (control != null && !control.IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
if (control.InvokeRequired)
|
|
|
|
|
{
|
|
|
|
|
control.Invoke(action);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 05:37:54 +13:00
|
|
|
|
public static void ForceActivate(this Form form)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-11-30 20:43:56 +13:00
|
|
|
|
if (!form.IsDisposed)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-11-30 20:43:56 +13:00
|
|
|
|
if (!form.Visible)
|
|
|
|
|
{
|
|
|
|
|
form.Show();
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-11-30 20:43:56 +13:00
|
|
|
|
if (form.WindowState == FormWindowState.Minimized)
|
|
|
|
|
{
|
|
|
|
|
form.WindowState = FormWindowState.Normal;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-11-30 20:43:56 +13:00
|
|
|
|
form.BringToFront();
|
|
|
|
|
form.Activate();
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int WeekOfYear(this DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Icon ToIcon(this Bitmap bmp)
|
|
|
|
|
{
|
|
|
|
|
IntPtr handle = bmp.GetHicon();
|
|
|
|
|
return Icon.FromHandle(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DisposeHandle(this Icon icon)
|
|
|
|
|
{
|
|
|
|
|
if (icon.Handle != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.DestroyIcon(icon.Handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ApplyDefaultPropertyValues(this object self)
|
|
|
|
|
{
|
|
|
|
|
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(self))
|
|
|
|
|
{
|
|
|
|
|
DefaultValueAttribute attr = prop.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
|
|
|
|
|
if (attr != null) prop.SetValue(self, attr.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MoveUp(this ListViewItem lvi)
|
|
|
|
|
{
|
|
|
|
|
ListView lv = lvi.ListView;
|
|
|
|
|
|
|
|
|
|
if (lv.Items.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
int index = lvi.Index;
|
|
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
index = lv.Items.Count - 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
index--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lv.Items.Remove(lvi);
|
|
|
|
|
lv.Items.Insert(index, lvi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lv.Focus();
|
|
|
|
|
lvi.EnsureVisible();
|
|
|
|
|
lvi.Selected = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MoveDown(this ListViewItem lvi)
|
|
|
|
|
{
|
|
|
|
|
ListView lv = lvi.ListView;
|
|
|
|
|
|
|
|
|
|
if (lv.Items.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
int index = lvi.Index;
|
|
|
|
|
|
|
|
|
|
if (index == lv.Items.Count - 1)
|
|
|
|
|
{
|
|
|
|
|
index = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lv.Items.Remove(lvi);
|
|
|
|
|
lv.Items.Insert(index, lvi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lv.Focus();
|
|
|
|
|
lvi.EnsureVisible();
|
|
|
|
|
lvi.Selected = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-24 18:30:32 +12:00
|
|
|
|
public static Bitmap CreateEmptyBitmap(this Image img, int widthOffset = 0, int heightOffset = 0, PixelFormat pixelFormat = PixelFormat.Format32bppArgb)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2013-11-06 11:48:41 +13:00
|
|
|
|
Bitmap bmp = new Bitmap(img.Width + widthOffset, img.Height + heightOffset, pixelFormat);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 10:07:13 +13:00
|
|
|
|
public static Bitmap CreateEmptyBitmap(this Image img, PixelFormat pixelFormat)
|
|
|
|
|
{
|
|
|
|
|
return img.CreateEmptyBitmap(0, 0, pixelFormat);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-09 16:13:49 +13:00
|
|
|
|
public static string GetDescription(this Type type)
|
|
|
|
|
{
|
|
|
|
|
DescriptionAttribute[] attributes = (DescriptionAttribute[])type.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
2015-11-29 01:53:16 +13:00
|
|
|
|
return attributes.Length > 0 ? attributes[0].Description : type.Name;
|
2013-11-09 16:13:49 +13:00
|
|
|
|
}
|
2014-05-13 21:49:34 +12:00
|
|
|
|
|
|
|
|
|
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int count)
|
|
|
|
|
{
|
|
|
|
|
return source.Reverse().Take(count).Reverse();
|
|
|
|
|
}
|
2014-06-19 07:17:22 +12:00
|
|
|
|
|
|
|
|
|
public static void Check(this ToolStripMenuItem tsmi)
|
|
|
|
|
{
|
|
|
|
|
if (tsmi != null)
|
|
|
|
|
{
|
2018-10-18 01:32:03 +13:00
|
|
|
|
foreach (ToolStripItem item in tsmi.GetCurrentParent().Items)
|
2014-06-19 07:17:22 +12:00
|
|
|
|
{
|
2017-10-29 05:41:14 +13:00
|
|
|
|
if (item != null && item is ToolStripMenuItem tsmiItem && tsmiItem.Tag.Equals(tsmi.Tag))
|
2014-06-19 07:17:22 +12:00
|
|
|
|
{
|
2017-10-29 05:41:14 +13:00
|
|
|
|
tsmiItem.Checked = tsmiItem == tsmi;
|
2014-06-19 07:17:22 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-06 05:46:37 +12:00
|
|
|
|
|
|
|
|
|
public static Version Normalize(this Version version)
|
|
|
|
|
{
|
|
|
|
|
return new Version(Math.Max(version.Major, 0), Math.Max(version.Minor, 0), Math.Max(version.Build, 0), Math.Max(version.Revision, 0));
|
|
|
|
|
}
|
2014-12-24 06:44:17 +13:00
|
|
|
|
|
|
|
|
|
public static void Move<T>(this List<T> list, int oldIndex, int newIndex)
|
|
|
|
|
{
|
|
|
|
|
T obj = list[oldIndex];
|
|
|
|
|
list.RemoveAt(oldIndex);
|
|
|
|
|
list.Insert(newIndex, obj);
|
|
|
|
|
}
|
2015-05-27 04:14:09 +12:00
|
|
|
|
|
|
|
|
|
public static void SetWatermark(this TextBox textBox, string watermarkText, bool showCueWhenFocus = false)
|
|
|
|
|
{
|
|
|
|
|
if (textBox != null && textBox.IsHandleCreated && watermarkText != null)
|
|
|
|
|
{
|
2016-10-01 07:21:48 +13:00
|
|
|
|
NativeMethods.SendMessage(textBox.Handle, (int)NativeConstants.EM_SETCUEBANNER, showCueWhenFocus ? 1 : 0, watermarkText);
|
2015-05-27 04:14:09 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-24 06:23:25 +12:00
|
|
|
|
|
|
|
|
|
public static void HideImageMargin(this ToolStripDropDownItem tsddi)
|
|
|
|
|
{
|
|
|
|
|
((ToolStripDropDownMenu)tsddi.DropDown).ShowImageMargin = false;
|
|
|
|
|
}
|
2016-03-01 10:09:22 +13:00
|
|
|
|
|
2018-12-18 02:39:55 +13:00
|
|
|
|
public static void DisableMenuCloseOnClick(this ToolStripDropDownItem tsddi)
|
|
|
|
|
{
|
|
|
|
|
tsddi.DropDown.Closing += (sender, e) => e.Cancel = e.CloseReason == ToolStripDropDownCloseReason.ItemClicked;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 10:09:22 +13:00
|
|
|
|
public static void SetValue(this NumericUpDown nud, decimal number)
|
|
|
|
|
{
|
2019-05-24 07:59:16 +12:00
|
|
|
|
nud.Value = number.Clamp(nud.Minimum, nud.Maximum);
|
2016-03-01 10:09:22 +13:00
|
|
|
|
}
|
2016-04-17 00:15:58 +12:00
|
|
|
|
|
|
|
|
|
public static bool IsValidImage(this PictureBox pb)
|
|
|
|
|
{
|
|
|
|
|
return pb.Image != null && pb.Image != pb.InitialImage && pb.Image != pb.ErrorImage;
|
|
|
|
|
}
|
2016-05-26 12:18:13 +12:00
|
|
|
|
|
|
|
|
|
public static void IgnoreSeparatorClick(this ContextMenuStrip cms)
|
|
|
|
|
{
|
|
|
|
|
bool cancelClose = false;
|
|
|
|
|
|
|
|
|
|
cms.ItemClicked += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
cancelClose = e.ClickedItem is ToolStripSeparator;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cms.Closing += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked && cancelClose)
|
|
|
|
|
{
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-08-02 18:56:26 +12:00
|
|
|
|
|
|
|
|
|
public static Rectangle Combine(this IEnumerable<Rectangle> rects)
|
|
|
|
|
{
|
|
|
|
|
Rectangle result = Rectangle.Empty;
|
|
|
|
|
|
|
|
|
|
foreach (Rectangle rect in rects)
|
|
|
|
|
{
|
|
|
|
|
if (result.IsEmpty)
|
|
|
|
|
{
|
|
|
|
|
result = rect;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = Rectangle.Union(result, rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-08-03 17:59:52 +12:00
|
|
|
|
|
|
|
|
|
public static Rectangle AddPoint(this Rectangle rect, Point point)
|
|
|
|
|
{
|
|
|
|
|
return Rectangle.Union(rect, new Rectangle(point, new Size(1, 1)));
|
|
|
|
|
}
|
2016-08-04 13:13:57 +12:00
|
|
|
|
|
|
|
|
|
public static Rectangle CreateRectangle(this IEnumerable<Point> points)
|
|
|
|
|
{
|
|
|
|
|
Rectangle result = Rectangle.Empty;
|
|
|
|
|
|
|
|
|
|
foreach (Point point in points)
|
|
|
|
|
{
|
|
|
|
|
if (result.IsEmpty)
|
|
|
|
|
{
|
|
|
|
|
result = new Rectangle(point, new Size(1, 1));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = result.AddPoint(point);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-08-25 03:34:02 +12:00
|
|
|
|
|
|
|
|
|
public static Point Center(this Rectangle rect)
|
|
|
|
|
{
|
2018-05-17 01:27:11 +12:00
|
|
|
|
return new Point(rect.X + (rect.Width / 2), rect.Y + (rect.Height / 2));
|
2016-08-25 03:34:02 +12:00
|
|
|
|
}
|
2017-04-21 08:50:31 +12:00
|
|
|
|
|
2017-12-13 12:22:24 +13:00
|
|
|
|
public static Point Restrict(this Point point, Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
point.X = Math.Max(point.X, rect.X);
|
|
|
|
|
point.Y = Math.Max(point.Y, rect.Y);
|
|
|
|
|
point.X = Math.Min(point.X, rect.X + rect.Width - 1);
|
|
|
|
|
point.Y = Math.Min(point.Y, rect.Y + rect.Height - 1);
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 08:50:31 +12:00
|
|
|
|
public static void RefreshItems(this ComboBox cb)
|
|
|
|
|
{
|
|
|
|
|
typeof(ComboBox).InvokeMember("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, cb, new object[] { });
|
|
|
|
|
}
|
2017-04-22 08:42:52 +12:00
|
|
|
|
|
2018-11-21 00:48:31 +13:00
|
|
|
|
public static void RefreshItem(this ListBox lb, int index)
|
2017-09-07 12:22:27 +12:00
|
|
|
|
{
|
2018-11-21 00:48:31 +13:00
|
|
|
|
typeof(ListBox).InvokeMember("RefreshItem", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, lb, new object[] { index });
|
|
|
|
|
}
|
2017-09-07 12:22:27 +12:00
|
|
|
|
|
2018-11-21 00:48:31 +13:00
|
|
|
|
public static void RefreshSelectedItem(this ListBox lb)
|
|
|
|
|
{
|
|
|
|
|
if (lb.SelectedIndex > -1)
|
2017-09-07 12:22:27 +12:00
|
|
|
|
{
|
2018-11-21 00:48:31 +13:00
|
|
|
|
lb.RefreshItem(lb.SelectedIndex);
|
2017-09-07 12:22:27 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 00:48:31 +13:00
|
|
|
|
public static void RefreshItems(this ListBox lb)
|
|
|
|
|
{
|
|
|
|
|
typeof(ListBox).InvokeMember("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, lb, new object[] { });
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-11 11:21:04 +13:00
|
|
|
|
public static void ShowError(this Exception e, bool fullError = true)
|
2017-04-22 08:42:52 +12:00
|
|
|
|
{
|
2017-11-11 11:21:04 +13:00
|
|
|
|
string error = fullError ? e.ToString() : e.Message;
|
|
|
|
|
MessageBox.Show(error, "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2017-04-22 08:42:52 +12:00
|
|
|
|
}
|
2018-08-04 01:54:48 +12:00
|
|
|
|
|
|
|
|
|
public static Task ContinueInCurrentContext(this Task task, Action action)
|
|
|
|
|
{
|
|
|
|
|
TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
|
2018-08-04 02:16:36 +12:00
|
|
|
|
return task.ContinueWith(t => action(), scheduler);
|
2018-08-04 01:54:48 +12:00
|
|
|
|
}
|
2018-11-16 01:18:24 +13:00
|
|
|
|
|
|
|
|
|
public static void DoubleBuffered(this DataGridView dgv, bool value)
|
|
|
|
|
{
|
|
|
|
|
PropertyInfo pi = dgv.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
|
pi.SetValue(dgv, value, null);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|