ShareX/ShareX.HelpersLib/CursorData.cs

173 lines
5.6 KiB
C#
Raw Normal View History

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;
using System.Drawing;
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
{
public IntPtr Handle { get; private set; }
2013-11-03 23:53:49 +13:00
public Point Position { get; private set; }
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);
2022-06-15 12:33:59 +12:00
public Size BaseSize { get; private set; }
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-15 12:33:59 +12:00
BaseSize = GetCursorBaseSize();
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-15 12:33:59 +12:00
if (BaseSize.IsEmpty)
{
2022-06-15 08:46:04 +12:00
Hotspot = new Point(iconInfo.xHotspot, iconInfo.yHotspot);
}
else
{
2022-06-15 12:33:59 +12:00
float multiplier = BaseSize.Width / 32f;
2022-06-15 09:33:32 +12:00
Hotspot = new Point((int)Math.Round(iconInfo.xHotspot * multiplier), (int)Math.Round(iconInfo.yHotspot * multiplier));
}
2013-11-03 23:53:49 +13:00
2018-12-30 02:32:09 +13:00
if (iconInfo.hbmMask != IntPtr.Zero)
{
NativeMethods.DeleteObject(iconInfo.hbmMask);
}
if (iconInfo.hbmColor != IntPtr.Zero)
{
NativeMethods.DeleteObject(iconInfo.hbmColor);
}
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-15 12:33:59 +12:00
public static Size GetCursorBaseSize()
{
try
{
int cursorBaseSize = RegistryHelpers.GetValueDWord(@"Control Panel\Cursors", "CursorBaseSize");
return new Size(cursorBaseSize, cursorBaseSize);
}
catch
{
return Size.Empty;
}
}
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 12:33:59 +12:00
NativeMethods.DrawIconEx(hdcDest, drawPosition.X, drawPosition.Y, Handle, BaseSize.Width, BaseSize.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
}
}
public Bitmap ToBitmap()
{
Size cursorSize;
2022-06-15 12:33:59 +12:00
if (BaseSize.IsEmpty)
{
cursorSize = new Size(32, 32);
}
else
{
2022-06-15 12:33:59 +12:00
cursorSize = BaseSize;
}
Bitmap bmp = new Bitmap(cursorSize.Width, cursorSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
IntPtr hdcDest = g.GetHdc();
NativeMethods.DrawIconEx(hdcDest, 0, 0, Handle, cursorSize.Width, cursorSize.Height, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
g.ReleaseHdc(hdcDest);
}
return bmp;
}
2013-11-03 23:53:49 +13:00
}
}