ShareX/ShareX.HelpersLib/CursorData.cs

121 lines
4 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; }
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;
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
{
2018-12-30 02:32:09 +13:00
Position = new Point(Position.X - iconInfo.xHotspot, Position.Y - iconInfo.yHotspot);
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
}
}
}
}
2017-08-14 20:06:12 +12:00
public void DrawCursor(Image img)
2013-11-03 23:53:49 +13:00
{
2017-08-14 20:06:12 +12:00
DrawCursor(img, Point.Empty);
2013-11-03 23:53:49 +13:00
}
2018-12-30 02:32:09 +13: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
{
2018-12-30 02:32:09 +13:00
Point drawPosition = new Point(Position.X - offset.X, Position.Y - offset.Y);
drawPosition = CaptureHelpers.ScreenToClient(drawPosition);
2013-11-03 23:53:49 +13:00
using (Graphics g = Graphics.FromImage(img))
using (Icon icon = Icon.FromHandle(Handle))
2013-11-03 23:53:49 +13:00
{
g.DrawIcon(icon, drawPosition.X, drawPosition.Y);
}
}
}
2017-08-14 20:06:12 +12:00
public void DrawCursor(IntPtr hdcDest)
2013-11-03 23:53:49 +13:00
{
2017-08-14 20:06:12 +12:00
DrawCursor(hdcDest, Point.Empty);
2013-11-03 23:53:49 +13:00
}
2018-12-30 02:32:09 +13: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
{
2018-12-30 02:32:09 +13:00
Point drawPosition = new Point(Position.X - offset.X, Position.Y - offset.Y);
drawPosition = CaptureHelpers.ScreenToClient(drawPosition);
2017-08-14 20:06:12 +12:00
NativeMethods.DrawIconEx(hdcDest, drawPosition.X, drawPosition.Y, Handle, 0, 0, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
2013-11-03 23:53:49 +13:00
}
}
}
}