mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
#5055: If default size then use .NET method of converting cursor to bitmap
This commit is contained in:
parent
a140c6eac5
commit
77bf60fda0
1 changed files with 27 additions and 16 deletions
|
@ -25,6 +25,7 @@ You should have received a copy of the GNU General Public License
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace ShareX.HelpersLib
|
namespace ShareX.HelpersLib
|
||||||
|
@ -35,6 +36,7 @@ public class CursorData
|
||||||
public Point Position { get; private set; }
|
public Point Position { get; private set; }
|
||||||
public Size Size { get; private set; }
|
public Size Size { get; private set; }
|
||||||
public float SizeMultiplier { get; private set; }
|
public float SizeMultiplier { get; private set; }
|
||||||
|
public bool IsDefaultSize => SizeMultiplier == 1f;
|
||||||
public Point Hotspot { get; private set; }
|
public Point Hotspot { get; private set; }
|
||||||
public Point DrawPosition => new Point(Position.X - Hotspot.X, Position.Y - Hotspot.Y);
|
public Point DrawPosition => new Point(Position.X - Hotspot.X, Position.Y - Hotspot.Y);
|
||||||
public bool IsVisible { get; private set; }
|
public bool IsVisible { get; private set; }
|
||||||
|
@ -69,7 +71,14 @@ public void UpdateCursorData()
|
||||||
{
|
{
|
||||||
if (NativeMethods.GetIconInfo(iconHandle, out IconInfo iconInfo))
|
if (NativeMethods.GetIconInfo(iconHandle, out IconInfo iconInfo))
|
||||||
{
|
{
|
||||||
Hotspot = new Point((int)Math.Round(iconInfo.xHotspot * SizeMultiplier), (int)Math.Round(iconInfo.yHotspot * SizeMultiplier));
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
if (iconInfo.hbmColor != IntPtr.Zero)
|
if (iconInfo.hbmColor != IntPtr.Zero)
|
||||||
{
|
{
|
||||||
|
@ -78,11 +87,14 @@ public void UpdateCursorData()
|
||||||
|
|
||||||
if (iconInfo.hbmMask != IntPtr.Zero)
|
if (iconInfo.hbmMask != IntPtr.Zero)
|
||||||
{
|
{
|
||||||
using (Bitmap bmpMask = Image.FromHbitmap(iconInfo.hbmMask))
|
if (!IsDefaultSize)
|
||||||
{
|
{
|
||||||
int cursorWidth = bmpMask.Width;
|
using (Bitmap bmpMask = Image.FromHbitmap(iconInfo.hbmMask))
|
||||||
int cursorHeight = iconInfo.hbmColor != IntPtr.Zero ? bmpMask.Height : bmpMask.Height / 2;
|
{
|
||||||
Size = new Size((int)Math.Round(cursorWidth * SizeMultiplier), (int)Math.Round(cursorHeight * SizeMultiplier));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeMethods.DeleteObject(iconInfo.hbmMask);
|
NativeMethods.DeleteObject(iconInfo.hbmMask);
|
||||||
|
@ -102,7 +114,11 @@ public static float GetCursorSizeMultiplier()
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int cursorSize = RegistryHelpers.GetValueDWord(@"SOFTWARE\Microsoft\Accessibility", "CursorSize");
|
int cursorSize = RegistryHelpers.GetValueDWord(@"SOFTWARE\Microsoft\Accessibility", "CursorSize");
|
||||||
sizeMultiplier = 1f + ((cursorSize - 1) * 0.5f);
|
|
||||||
|
if (cursorSize > 1)
|
||||||
|
{
|
||||||
|
sizeMultiplier = 1f + ((cursorSize - 1) * 0.5f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
@ -149,24 +165,19 @@ public void DrawCursor(Image img, Point offset)
|
||||||
|
|
||||||
public Bitmap ToBitmap()
|
public Bitmap ToBitmap()
|
||||||
{
|
{
|
||||||
Size cursorSize;
|
if (IsDefaultSize || Size.IsEmpty)
|
||||||
|
|
||||||
if (Size.IsEmpty)
|
|
||||||
{
|
{
|
||||||
cursorSize = new Size(32, 32);
|
Icon icon = Icon.FromHandle(Handle);
|
||||||
}
|
return icon.ToBitmap();
|
||||||
else
|
|
||||||
{
|
|
||||||
cursorSize = Size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bmp = new Bitmap(cursorSize.Width, cursorSize.Height);
|
Bitmap bmp = new Bitmap(Size.Width, Size.Height, PixelFormat.Format32bppArgb);
|
||||||
|
|
||||||
using (Graphics g = Graphics.FromImage(bmp))
|
using (Graphics g = Graphics.FromImage(bmp))
|
||||||
{
|
{
|
||||||
IntPtr hdcDest = g.GetHdc();
|
IntPtr hdcDest = g.GetHdc();
|
||||||
|
|
||||||
NativeMethods.DrawIconEx(hdcDest, 0, 0, Handle, cursorSize.Width, cursorSize.Height, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
|
NativeMethods.DrawIconEx(hdcDest, 0, 0, Handle, Size.Width, Size.Height, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
|
||||||
|
|
||||||
g.ReleaseHdc(hdcDest);
|
g.ReleaseHdc(hdcDest);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue