Added IsValid method to CursorData

This commit is contained in:
Jaex 2017-08-14 11:06:12 +03:00
parent 898daf4cc5
commit 59aee065f1
4 changed files with 18 additions and 15 deletions

View file

@ -35,6 +35,8 @@ public class CursorData : IDisposable
public bool IsVisible { get; private set; }
public Point Position { get; private set; }
public bool IsValid => Handle != IntPtr.Zero && IsVisible;
public CursorData()
{
UpdateCursorData();
@ -73,14 +75,14 @@ public void UpdateCursorData()
}
}
public void DrawCursorToImage(Image img)
public void DrawCursor(Image img)
{
DrawCursorToImage(img, Point.Empty);
DrawCursor(img, Point.Empty);
}
public void DrawCursorToImage(Image img, Point cursorOffset)
public void DrawCursor(Image img, Point cursorOffset)
{
if (Handle != IntPtr.Zero)
if (IsValid)
{
Point drawPosition = new Point(Position.X - cursorOffset.X, Position.Y - cursorOffset.Y);
@ -92,16 +94,17 @@ public void DrawCursorToImage(Image img, Point cursorOffset)
}
}
public void DrawCursorToHandle(IntPtr hdcDest)
public void DrawCursor(IntPtr hdcDest)
{
DrawCursorToHandle(hdcDest, Point.Empty);
DrawCursor(hdcDest, Point.Empty);
}
public void DrawCursorToHandle(IntPtr hdcDest, Point cursorOffset)
public void DrawCursor(IntPtr hdcDest, Point cursorOffset)
{
if (Handle != IntPtr.Zero)
if (IsValid)
{
Point drawPosition = new Point(Position.X - cursorOffset.X, Position.Y - cursorOffset.Y);
NativeMethods.DrawIconEx(hdcDest, drawPosition.X, drawPosition.Y, Handle, 0, 0, 0, IntPtr.Zero, NativeConstants.DI_NORMAL);
}
}

View file

@ -135,7 +135,7 @@ private Image CaptureRectangleNative(IntPtr handle, Rectangle rect, bool capture
{
using (CursorData cursorData = new CursorData())
{
cursorData.DrawCursorToHandle(hdcDest, cursorOffset);
cursorData.DrawCursor(hdcDest, cursorOffset);
}
}
catch (Exception e)

View file

@ -47,7 +47,7 @@ public Image CaptureWindowTransparent(IntPtr handle)
}
Bitmap whiteBackground = null, blackBackground = null, whiteBackground2 = null;
CursorData cursor = null;
CursorData cursorData = null;
bool isTransparent = false, isTaskbarHide = false;
try
@ -61,7 +61,7 @@ public Image CaptureWindowTransparent(IntPtr handle)
{
try
{
cursor = new CursorData();
cursorData = new CursorData();
}
catch (Exception e)
{
@ -119,10 +119,10 @@ public Image CaptureWindowTransparent(IntPtr handle)
transparentImage = whiteBackground2;
}
if (cursor != null && cursor.IsVisible)
if (cursorData != null && cursorData.IsValid)
{
Point cursorOffset = CaptureHelpers.ScreenToClient(rect.Location);
cursor.DrawCursorToImage(transparentImage, cursorOffset);
cursorData.DrawCursor(transparentImage, cursorOffset);
}
if (isTransparent)
@ -147,7 +147,7 @@ public Image CaptureWindowTransparent(IntPtr handle)
if (whiteBackground != null) whiteBackground.Dispose();
if (blackBackground != null) blackBackground.Dispose();
if (isTransparent && whiteBackground2 != null) whiteBackground2.Dispose();
if (cursor != null) cursor.Dispose();
if (cursorData != null) cursorData.Dispose();
}
}

View file

@ -94,7 +94,7 @@ protected ImageInfo ExecuteRegionCapture(TaskSettings taskSettings)
form.Prepare(img);
if (cursorData != null && cursorData.IsVisible)
if (cursorData != null && cursorData.IsValid)
{
form.AddCursor(cursorData.Handle, cursorData.Position);
}