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)
|
|
|
|
|
|
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;
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-11-28 22:55:42 +13:00
|
|
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
foreach (T item in source)
|
|
|
|
|
{
|
|
|
|
|
action(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
2021-06-10 10:14:01 +12:00
|
|
|
|
return default;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T ReturnIfValidIndex<T>(this List<T> list, int index)
|
|
|
|
|
{
|
|
|
|
|
if (list.IsValidIndex(index)) return list[index];
|
2021-06-10 10:14:01 +12:00
|
|
|
|
return default;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Last<T>(this T[] array, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (array.Length > index) return array[array.Length - index - 1];
|
2021-06-10 10:14:01 +12:00
|
|
|
|
return default;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Last<T>(this List<T> list, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (list.Count > index) return list[list.Count - index - 1];
|
2021-06-10 10:14:01 +12:00
|
|
|
|
return default;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static bool IsValid(this RectangleF rect)
|
2016-05-05 00:53:18 +12:00
|
|
|
|
{
|
2022-02-20 19:30:39 +13:00
|
|
|
|
return rect.Width > 0 && rect.Height > 0;
|
2016-05-05 00:53:18 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static PointF Add(this PointF point, float offsetX, float offsetY)
|
|
|
|
|
{
|
|
|
|
|
return new PointF(point.X + offsetX, point.Y + offsetY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PointF Add(this PointF point, PointF offset)
|
|
|
|
|
{
|
|
|
|
|
return new PointF(point.X + offset.X, point.Y + offset.Y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PointF Scale(this Point point, float scaleFactor)
|
2022-02-01 14:01:18 +13:00
|
|
|
|
{
|
2022-02-20 19:30:39 +13:00
|
|
|
|
return new PointF(point.X * scaleFactor, point.Y * scaleFactor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PointF Scale(this PointF point, float scaleFactor)
|
|
|
|
|
{
|
|
|
|
|
return new PointF(point.X * scaleFactor, point.Y * scaleFactor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Point Round(this PointF point)
|
|
|
|
|
{
|
|
|
|
|
return Point.Round(point);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Offset(this PointF point, PointF offset)
|
|
|
|
|
{
|
|
|
|
|
point.X += offset.X;
|
|
|
|
|
point.Y += offset.Y;
|
2022-02-01 14:01:18 +13:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static RectangleF Offset(this RectangleF rect, float offset)
|
2022-02-01 14:01:18 +13:00
|
|
|
|
{
|
2022-02-20 19:30:39 +13:00
|
|
|
|
return new RectangleF(rect.X - offset, rect.Y - offset, rect.Width + (offset * 2), rect.Height + (offset * 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static RectangleF Scale(this RectangleF rect, float scaleFactor)
|
|
|
|
|
{
|
2022-02-24 00:15:45 +13:00
|
|
|
|
return new RectangleF(rect.X * scaleFactor, rect.Y * scaleFactor, rect.Width * scaleFactor, rect.Height * scaleFactor);
|
2022-02-20 19:30:39 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Rectangle Round(this RectangleF rect)
|
|
|
|
|
{
|
|
|
|
|
return Rectangle.Round(rect);
|
2022-02-01 14:01:18 +13:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static RectangleF LocationOffset(this RectangleF rect, float x, float y)
|
|
|
|
|
{
|
|
|
|
|
return new RectangleF(rect.X + x, rect.Y + y, rect.Width, rect.Height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static RectangleF LocationOffset(this RectangleF rect, PointF offset)
|
|
|
|
|
{
|
|
|
|
|
return rect.LocationOffset(offset.X, offset.Y);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static RectangleF SizeOffset(this RectangleF rect, float width, float height)
|
|
|
|
|
{
|
|
|
|
|
return new RectangleF(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);
|
|
|
|
|
}
|
2022-02-24 00:15:45 +13:00
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static RectangleF SizeOffset(this RectangleF rect, float offset)
|
|
|
|
|
{
|
|
|
|
|
return rect.SizeOffset(offset, offset);
|
|
|
|
|
}
|
2014-08-28 12:53:38 +12:00
|
|
|
|
|
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;
|
2020-10-21 22:50:24 +13:00
|
|
|
|
tsmiPaste.Enabled = !rtb.ReadOnly && ClipboardHelpers.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) =>
|
|
|
|
|
{
|
2021-12-18 10:36:12 +13:00
|
|
|
|
if (e.KeyData == (Keys.Control | Keys.A))
|
2018-01-04 10:39:10 +13:00
|
|
|
|
{
|
|
|
|
|
tb.SelectAll();
|
|
|
|
|
e.SuppressKeyPress = true;
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
if (prop.Attributes[typeof(DefaultValueAttribute)] is DefaultValueAttribute attr)
|
|
|
|
|
{
|
|
|
|
|
prop.SetValue(self, attr.Value);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-09-17 09:32:41 +12:00
|
|
|
|
tsddi.DropDown.Closing -= DisableMenuCloseOnClick_DropDown_Closing;
|
|
|
|
|
tsddi.DropDown.Closing += DisableMenuCloseOnClick_DropDown_Closing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DisableMenuCloseOnClick_DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
e.Cancel = e.CloseReason == ToolStripDropDownCloseReason.ItemClicked;
|
2018-12-18 02:39:55 +13:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2019-11-20 02:52:34 +13:00
|
|
|
|
public static void SetValue(this TrackBar tb, int number)
|
|
|
|
|
{
|
|
|
|
|
tb.Value = number.Clamp(tb.Minimum, tb.Maximum);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static RectangleF Combine(this IEnumerable<RectangleF> rects)
|
2016-08-03 17:59:52 +12:00
|
|
|
|
{
|
2022-02-20 19:30:39 +13:00
|
|
|
|
RectangleF result = RectangleF.Empty;
|
|
|
|
|
|
|
|
|
|
foreach (RectangleF rect in rects)
|
|
|
|
|
{
|
|
|
|
|
if (result.IsEmpty)
|
|
|
|
|
{
|
|
|
|
|
result = rect;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = RectangleF.Union(result, rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2016-08-03 17:59:52 +12:00
|
|
|
|
}
|
2016-08-04 13:13:57 +12:00
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static RectangleF AddPoint(this RectangleF rect, PointF point)
|
2016-08-04 13:13:57 +12:00
|
|
|
|
{
|
2022-02-20 19:30:39 +13:00
|
|
|
|
return RectangleF.Union(rect, new RectangleF(point, new SizeF(1, 1)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static RectangleF CreateRectangle(this IEnumerable<PointF> points)
|
|
|
|
|
{
|
|
|
|
|
RectangleF result = Rectangle.Empty;
|
2016-08-04 13:13:57 +12:00
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
foreach (PointF point in points)
|
2016-08-04 13:13:57 +12:00
|
|
|
|
{
|
|
|
|
|
if (result.IsEmpty)
|
|
|
|
|
{
|
2022-02-20 19:30:39 +13:00
|
|
|
|
result = new RectangleF(point, new Size(1, 1));
|
2016-08-04 13:13:57 +12:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static PointF Center(this RectangleF rect)
|
|
|
|
|
{
|
|
|
|
|
return new PointF(rect.X + (rect.Width / 2), rect.Y + (rect.Height / 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Area(this RectangleF rect)
|
2020-07-06 10:37:56 +12:00
|
|
|
|
{
|
|
|
|
|
return rect.Width * rect.Height;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static float Perimeter(this RectangleF rect)
|
2020-07-06 10:37:56 +12:00
|
|
|
|
{
|
|
|
|
|
return 2 * (rect.Width + rect.Height);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 19:30:39 +13:00
|
|
|
|
public static PointF Restrict(this PointF point, RectangleF rect)
|
2017-12-13 12:22:24 +13:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2021-04-26 05:49:40 +12:00
|
|
|
|
public static void AutoSizeDropDown(this ComboBox cb)
|
|
|
|
|
{
|
|
|
|
|
int maxWidth = 0;
|
|
|
|
|
int verticalScrollBarWidth = cb.Items.Count > cb.MaxDropDownItems ? SystemInformation.VerticalScrollBarWidth : 0;
|
|
|
|
|
foreach (object item in cb.Items)
|
|
|
|
|
{
|
|
|
|
|
int tempWidth = TextRenderer.MeasureText(cb.GetItemText(item), cb.Font).Width + verticalScrollBarWidth;
|
|
|
|
|
if (tempWidth > maxWidth)
|
|
|
|
|
{
|
|
|
|
|
maxWidth = tempWidth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cb.DropDownWidth = maxWidth;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2019-06-17 13:14:04 +12:00
|
|
|
|
|
|
|
|
|
public static void SetFontRegular(this RichTextBox rtb)
|
|
|
|
|
{
|
|
|
|
|
rtb.SelectionFont = new Font(rtb.Font, FontStyle.Regular);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetFontBold(this RichTextBox rtb)
|
|
|
|
|
{
|
|
|
|
|
rtb.SelectionFont = new Font(rtb.Font, FontStyle.Bold);
|
|
|
|
|
}
|
2019-06-29 10:35:34 +12:00
|
|
|
|
|
2021-12-28 10:52:36 +13:00
|
|
|
|
public static void AppendText(this RichTextBox rtb, string text, FontStyle fontStyle, float fontSize = 0)
|
|
|
|
|
{
|
|
|
|
|
Font font;
|
|
|
|
|
|
|
|
|
|
if (fontSize > 0)
|
|
|
|
|
{
|
|
|
|
|
font = new Font(rtb.Font.FontFamily, fontSize, fontStyle);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
font = new Font(rtb.Font, fontStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rtb.SelectionFont = font;
|
|
|
|
|
rtb.AppendText(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AppendLine(this RichTextBox rtb, string text = "")
|
|
|
|
|
{
|
|
|
|
|
rtb.AppendText(text + Environment.NewLine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AppendLine(this RichTextBox rtb, string text, FontStyle fontStyle, float fontSize = 0)
|
|
|
|
|
{
|
|
|
|
|
rtb.AppendText(text + Environment.NewLine, fontStyle, fontSize);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-09 04:07:17 +13:00
|
|
|
|
public static void SupportCustomTheme(this ListView lv)
|
2019-06-29 10:35:34 +12:00
|
|
|
|
{
|
|
|
|
|
if (!lv.OwnerDraw)
|
|
|
|
|
{
|
|
|
|
|
lv.OwnerDraw = true;
|
|
|
|
|
|
|
|
|
|
lv.DrawItem += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
e.DrawDefault = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
lv.DrawSubItem += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
e.DrawDefault = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
lv.DrawColumnHeader += (sender, e) =>
|
|
|
|
|
{
|
2020-02-05 21:13:55 +13:00
|
|
|
|
if (ShareXResources.UseCustomTheme)
|
2019-06-29 10:35:34 +12:00
|
|
|
|
{
|
2019-09-06 20:18:30 +12:00
|
|
|
|
using (Brush brush = new SolidBrush(ShareXResources.Theme.BackgroundColor))
|
2019-06-29 10:35:34 +12:00
|
|
|
|
{
|
|
|
|
|
e.Graphics.FillRectangle(brush, e.Bounds);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 20:18:30 +12:00
|
|
|
|
TextRenderer.DrawText(e.Graphics, e.Header.Text, e.Font, e.Bounds.LocationOffset(2, 0).SizeOffset(-4, 0), ShareXResources.Theme.TextColor,
|
2019-06-29 10:35:34 +12:00
|
|
|
|
TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
|
|
|
|
|
|
|
|
|
|
if (e.Bounds.Right < lv.ClientRectangle.Right)
|
|
|
|
|
{
|
2019-09-25 02:01:00 +12:00
|
|
|
|
using (Pen pen = new Pen(ShareXResources.Theme.SeparatorDarkColor))
|
|
|
|
|
using (Pen pen2 = new Pen(ShareXResources.Theme.SeparatorLightColor))
|
2019-06-29 10:35:34 +12:00
|
|
|
|
{
|
|
|
|
|
e.Graphics.DrawLine(pen, e.Bounds.Right - 2, e.Bounds.Top, e.Bounds.Right - 2, e.Bounds.Bottom - 1);
|
|
|
|
|
e.Graphics.DrawLine(pen2, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
e.DrawDefault = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-06 22:21:27 +13:00
|
|
|
|
|
|
|
|
|
public static List<T> Range<T>(this List<T> source, int start, int end)
|
|
|
|
|
{
|
2019-10-06 23:41:50 +13:00
|
|
|
|
List<T> list = new List<T>();
|
|
|
|
|
|
2019-10-06 22:21:27 +13:00
|
|
|
|
if (start > end)
|
|
|
|
|
{
|
2019-10-06 23:41:50 +13:00
|
|
|
|
for (int i = start; i >= end; i--)
|
|
|
|
|
{
|
|
|
|
|
list.Add(source[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int i = start; i <= end; i++)
|
|
|
|
|
{
|
|
|
|
|
list.Add(source[i]);
|
|
|
|
|
}
|
2019-10-06 22:21:27 +13:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 23:41:50 +13:00
|
|
|
|
return list;
|
2019-10-06 22:21:27 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<T> Range<T>(this List<T> source, T start, T end)
|
|
|
|
|
{
|
|
|
|
|
int startIndex = source.IndexOf(start);
|
|
|
|
|
if (startIndex == -1) return new List<T>();
|
|
|
|
|
|
|
|
|
|
int endIndex = source.IndexOf(end);
|
|
|
|
|
if (endIndex == -1) return new List<T>();
|
|
|
|
|
|
|
|
|
|
return Range(source, startIndex, endIndex);
|
|
|
|
|
}
|
2020-01-04 04:09:21 +13:00
|
|
|
|
|
2020-03-11 19:43:24 +13:00
|
|
|
|
public static T CloneSafe<T>(this T obj) where T : class, ICloneable
|
2020-01-04 04:09:21 +13:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-11 19:43:24 +13:00
|
|
|
|
if (obj != null)
|
2020-01-04 04:09:21 +13:00
|
|
|
|
{
|
2020-03-11 19:43:24 +13:00
|
|
|
|
return obj.Clone() as T;
|
2020-01-04 04:09:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-02-09 05:20:27 +13:00
|
|
|
|
|
|
|
|
|
public static IEnumerable<TreeNode> All(this TreeNodeCollection nodes)
|
|
|
|
|
{
|
|
|
|
|
foreach (TreeNode node in nodes)
|
|
|
|
|
{
|
|
|
|
|
yield return node;
|
|
|
|
|
|
|
|
|
|
foreach (TreeNode child in node.Nodes.All())
|
|
|
|
|
{
|
|
|
|
|
yield return child;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-30 10:52:20 +13:00
|
|
|
|
|
|
|
|
|
public static bool IsTransparent(this Color color)
|
|
|
|
|
{
|
|
|
|
|
return color.A < 255;
|
|
|
|
|
}
|
2021-01-17 06:10:06 +13:00
|
|
|
|
|
|
|
|
|
public static string ToStringProper(this Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
return $"X: {rect.X}, Y: {rect.Y}, Width: {rect.Width}, Height: {rect.Height}";
|
|
|
|
|
}
|
2021-11-15 05:51:46 +13:00
|
|
|
|
|
|
|
|
|
public static void ChangeFontStyle(this Control control, FontStyle fontStyle)
|
|
|
|
|
{
|
|
|
|
|
control.Font = new Font(control.Font, fontStyle);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|