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
|
2024-01-03 12:57:14 +13:00
|
|
|
|
Copyright (c) 2007-2024 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.Drawing;
|
2022-06-30 04:31:59 +12:00
|
|
|
|
using System.Drawing.Imaging;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-12-30 02:32:09 +13:00
|
|
|
|
public class CursorData
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2017-07-21 08:27:03 +12:00
|
|
|
|
public IntPtr Handle { get; private set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public Point Position { get; private set; }
|
2022-06-30 03:07:08 +12:00
|
|
|
|
public Size Size { get; private set; }
|
|
|
|
|
public float SizeMultiplier { get; private set; }
|
2022-06-30 04:31:59 +12:00
|
|
|
|
public bool IsDefaultSize => SizeMultiplier == 1f;
|
2022-06-15 08:46:04 +12:00
|
|
|
|
public Point Hotspot { get; private set; }
|
|
|
|
|
public Point DrawPosition => new Point(Position.X - Hotspot.X, Position.Y - Hotspot.Y);
|
2018-12-30 02:32:09 +13:00
|
|
|
|
public bool IsVisible { get; private set; }
|
2017-08-14 20:06:12 +12:00
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public CursorData()
|
|
|
|
|
{
|
|
|
|
|
UpdateCursorData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateCursorData()
|
|
|
|
|
{
|
2018-12-30 02:32:09 +13:00
|
|
|
|
Handle = IntPtr.Zero;
|
|
|
|
|
Position = Point.Empty;
|
|
|
|
|
IsVisible = false;
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
CursorInfo cursorInfo = new CursorInfo();
|
|
|
|
|
cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
|
|
|
|
|
|
|
|
|
|
if (NativeMethods.GetCursorInfo(out cursorInfo))
|
|
|
|
|
{
|
2018-12-30 02:32:09 +13:00
|
|
|
|
Handle = cursorInfo.hCursor;
|
|
|
|
|
Position = cursorInfo.ptScreenPos;
|
2022-06-30 03:07:08 +12:00
|
|
|
|
Size = Size.Empty;
|
|
|
|
|
SizeMultiplier = GetCursorSizeMultiplier();
|
2016-10-01 07:21:48 +13:00
|
|
|
|
IsVisible = cursorInfo.flags == NativeConstants.CURSOR_SHOWING;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (IsVisible)
|
|
|
|
|
{
|
2018-12-30 02:32:09 +13:00
|
|
|
|
IntPtr iconHandle = NativeMethods.CopyIcon(Handle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-12-30 02:32:09 +13:00
|
|
|
|
if (iconHandle != IntPtr.Zero)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
if (NativeMethods.GetIconInfo(iconHandle, out IconInfo iconInfo))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2022-06-30 04:31:59 +12:00
|
|
|
|
if (IsDefaultSize)
|
|
|
|
|
{
|
|
|
|
|
Hotspot = new Point(iconInfo.xHotspot, iconInfo.yHotspot);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Hotspot = new Point((int)Math.Round(iconInfo.xHotspot * SizeMultiplier), (int)Math.Round(iconInfo.yHotspot * SizeMultiplier));
|
|
|
|
|
}
|
2022-06-30 03:07:08 +12:00
|
|
|
|
|
|
|
|
|
if (iconInfo.hbmColor != IntPtr.Zero)
|
2022-06-15 07:48:50 +12:00
|
|
|
|
{
|
2022-06-30 03:07:08 +12:00
|
|
|
|
NativeMethods.DeleteObject(iconInfo.hbmColor);
|
2022-06-15 07:48:50 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-12-30 02:32:09 +13:00
|
|
|
|
if (iconInfo.hbmMask != IntPtr.Zero)
|
|
|
|
|
{
|
2022-06-30 04:31:59 +12:00
|
|
|
|
if (!IsDefaultSize)
|
2022-06-30 03:07:08 +12:00
|
|
|
|
{
|
2022-06-30 04:31:59 +12:00
|
|
|
|
using (Bitmap bmpMask = Image.FromHbitmap(iconInfo.hbmMask))
|
|
|
|
|
{
|
|
|
|
|
int cursorWidth = bmpMask.Width;
|
|
|
|
|
int cursorHeight = iconInfo.hbmColor != IntPtr.Zero ? bmpMask.Height : bmpMask.Height / 2;
|
|
|
|
|
Size = new Size((int)Math.Round(cursorWidth * SizeMultiplier), (int)Math.Round(cursorHeight * SizeMultiplier));
|
|
|
|
|
}
|
2022-06-30 03:07:08 +12:00
|
|
|
|
}
|
2018-12-30 02:32:09 +13:00
|
|
|
|
|
2022-06-30 03:07:08 +12:00
|
|
|
|
NativeMethods.DeleteObject(iconInfo.hbmMask);
|
2018-12-30 02:32:09 +13:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2018-12-30 02:32:09 +13:00
|
|
|
|
|
|
|
|
|
NativeMethods.DestroyIcon(iconHandle);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-30 03:07:08 +12:00
|
|
|
|
public static float GetCursorSizeMultiplier()
|
2022-06-15 07:20:55 +12:00
|
|
|
|
{
|
2022-06-30 03:07:08 +12:00
|
|
|
|
float sizeMultiplier = 1f;
|
|
|
|
|
|
2022-08-01 00:19:00 +12:00
|
|
|
|
int? cursorSize = RegistryHelpers.GetValueDWord(@"SOFTWARE\Microsoft\Accessibility", "CursorSize");
|
2022-06-30 04:31:59 +12:00
|
|
|
|
|
2022-08-01 00:19:00 +12:00
|
|
|
|
if (cursorSize != null && cursorSize > 1)
|
2022-06-15 07:20:55 +12:00
|
|
|
|
{
|
2022-08-03 03:11:38 +12:00
|
|
|
|
sizeMultiplier = 1f + ((cursorSize.Value - 1) * 0.5f);
|
2022-06-15 07:20:55 +12:00
|
|
|
|
}
|
2022-06-30 03:07:08 +12:00
|
|
|
|
|
|
|
|
|
return sizeMultiplier;
|
2022-06-15 07:20:55 +12:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 08:46:04 +12:00
|
|
|
|
public void DrawCursor(IntPtr hdcDest)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2022-06-15 08:46:04 +12:00
|
|
|
|
DrawCursor(hdcDest, Point.Empty);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 08:46:04 +12:00
|
|
|
|
public void DrawCursor(IntPtr hdcDest, Point offset)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-12-30 02:32:09 +13:00
|
|
|
|
if (IsVisible)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2022-06-15 08:46:04 +12:00
|
|
|
|
Point drawPosition = new Point(DrawPosition.X - offset.X, DrawPosition.Y - offset.Y);
|
|
|
|
|
drawPosition = CaptureHelpers.ScreenToClient(drawPosition);
|
2022-06-15 07:48:50 +12:00
|
|
|
|
|
2022-06-30 03:07:08 +12:00
|
|
|
|
NativeMethods.DrawIconEx(hdcDest, drawPosition.X, drawPosition.Y, Handle, Size.Width, Size.Height, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 08:46:04 +12:00
|
|
|
|
public void DrawCursor(Image img)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2022-06-15 08:46:04 +12:00
|
|
|
|
DrawCursor(img, Point.Empty);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 08:46:04 +12:00
|
|
|
|
public void DrawCursor(Image img, Point offset)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-12-30 02:32:09 +13:00
|
|
|
|
if (IsVisible)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2022-06-15 08:46:04 +12:00
|
|
|
|
using (Graphics g = Graphics.FromImage(img))
|
|
|
|
|
{
|
|
|
|
|
IntPtr hdcDest = g.GetHdc();
|
2017-08-14 20:06:12 +12:00
|
|
|
|
|
2022-06-15 08:46:04 +12:00
|
|
|
|
DrawCursor(hdcDest, offset);
|
|
|
|
|
|
|
|
|
|
g.ReleaseHdc(hdcDest);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-15 10:59:55 +12:00
|
|
|
|
|
|
|
|
|
public Bitmap ToBitmap()
|
|
|
|
|
{
|
2022-06-30 04:31:59 +12:00
|
|
|
|
if (IsDefaultSize || Size.IsEmpty)
|
2022-06-15 10:59:55 +12:00
|
|
|
|
{
|
2022-06-30 04:31:59 +12:00
|
|
|
|
Icon icon = Icon.FromHandle(Handle);
|
|
|
|
|
return icon.ToBitmap();
|
2022-06-15 10:59:55 +12:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-30 04:31:59 +12:00
|
|
|
|
Bitmap bmp = new Bitmap(Size.Width, Size.Height, PixelFormat.Format32bppArgb);
|
2022-06-15 10:59:55 +12:00
|
|
|
|
|
|
|
|
|
using (Graphics g = Graphics.FromImage(bmp))
|
|
|
|
|
{
|
|
|
|
|
IntPtr hdcDest = g.GetHdc();
|
|
|
|
|
|
2022-06-30 04:31:59 +12:00
|
|
|
|
NativeMethods.DrawIconEx(hdcDest, 0, 0, Handle, Size.Width, Size.Height, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
|
2022-06-15 10:59:55 +12:00
|
|
|
|
|
|
|
|
|
g.ReleaseHdc(hdcDest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|