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.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
|
@ -35,6 +36,7 @@ public class CursorData
|
|||
public Point Position { get; private set; }
|
||||
public Size Size { get; private set; }
|
||||
public float SizeMultiplier { get; private set; }
|
||||
public bool IsDefaultSize => SizeMultiplier == 1f;
|
||||
public Point Hotspot { get; private set; }
|
||||
public Point DrawPosition => new Point(Position.X - Hotspot.X, Position.Y - Hotspot.Y);
|
||||
public bool IsVisible { get; private set; }
|
||||
|
@ -68,8 +70,15 @@ public void UpdateCursorData()
|
|||
if (iconHandle != IntPtr.Zero)
|
||||
{
|
||||
if (NativeMethods.GetIconInfo(iconHandle, out IconInfo iconInfo))
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
@ -77,6 +86,8 @@ public void UpdateCursorData()
|
|||
}
|
||||
|
||||
if (iconInfo.hbmMask != IntPtr.Zero)
|
||||
{
|
||||
if (!IsDefaultSize)
|
||||
{
|
||||
using (Bitmap bmpMask = Image.FromHbitmap(iconInfo.hbmMask))
|
||||
{
|
||||
|
@ -84,6 +95,7 @@ public void UpdateCursorData()
|
|||
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);
|
||||
}
|
||||
|
@ -102,8 +114,12 @@ public static float GetCursorSizeMultiplier()
|
|||
try
|
||||
{
|
||||
int cursorSize = RegistryHelpers.GetValueDWord(@"SOFTWARE\Microsoft\Accessibility", "CursorSize");
|
||||
|
||||
if (cursorSize > 1)
|
||||
{
|
||||
sizeMultiplier = 1f + ((cursorSize - 1) * 0.5f);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
@ -149,24 +165,19 @@ public void DrawCursor(Image img, Point offset)
|
|||
|
||||
public Bitmap ToBitmap()
|
||||
{
|
||||
Size cursorSize;
|
||||
|
||||
if (Size.IsEmpty)
|
||||
if (IsDefaultSize || Size.IsEmpty)
|
||||
{
|
||||
cursorSize = new Size(32, 32);
|
||||
}
|
||||
else
|
||||
{
|
||||
cursorSize = Size;
|
||||
Icon icon = Icon.FromHandle(Handle);
|
||||
return icon.ToBitmap();
|
||||
}
|
||||
|
||||
Bitmap bmp = new Bitmap(cursorSize.Width, cursorSize.Height);
|
||||
Bitmap bmp = new Bitmap(Size.Width, Size.Height, PixelFormat.Format32bppArgb);
|
||||
|
||||
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);
|
||||
NativeMethods.DrawIconEx(hdcDest, 0, 0, Handle, Size.Width, Size.Height, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
|
||||
|
||||
g.ReleaseHdc(hdcDest);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue