private void MouseHookTarget(object sender, CustomMouseEventArgs keyEventArgs)
{
_recordClicked = keyEventArgs.Button == MouseButton.Left && keyEventArgs.State == MouseButtonState.Pressed;
_posCursor = new System.Windows.Point(keyEventArgs.PosX, keyEventArgs.PosY);
if (!IsMouseCaptured || Mouse.Captured == null)
return;
#region Get Handle and Window Rect
var handle = Native.WindowFromPoint(keyEventArgs.PosX, keyEventArgs.PosY);
var scale = this.Scale();
if (_lastHandle != handle)
{
if (_lastHandle != IntPtr.Zero)
Native.DrawFrame(_lastHandle, scale);
_lastHandle = handle;
Native.DrawFrame(handle, scale);
}
Native.RECT rect;
if (!Native.GetWindowRect(handle, out rect)) return;
#endregion
if (keyEventArgs.State == MouseButtonState.Pressed)
return;
#region Mouse Up
Cursor = Cursors.Arrow;
try
{
#region Try to get the process
Process target = null;// Process.GetProcesses().FirstOrDefault(p => p.Handle == handle);
var processes = Process.GetProcesses();
foreach (var proc in processes)
{
try
{
if (proc.MainWindowHandle != IntPtr.Zero && proc.HandleCount > 0 && (proc.Handle == handle || proc.MainWindowHandle == handle))
{
target = proc;
break;
}
}
catch (Exception)
{ }
}
#endregion
if (target != null && target.ProcessName == "ScreenToGif") return;
//Clear up the selected window frame.
Native.DrawFrame(handle, scale);
_lastHandle = IntPtr.Zero;
#region Values
var top = (rect.Top / scale) - Constants.TopOffset;
var left = (rect.Left / scale) - Constants.LeftOffset;
var height = ((rect.Bottom - rect.Top + 1) / scale) + Constants.TopOffset + Constants.BottomOffset;
var width = ((rect.Right - rect.Left + 1) / scale) + Constants.LeftOffset + Constants.RightOffset;
#endregion
#region Validate
//TODO: Validate screensize...
if (top < 0)
top = 0 - 1;
if (left < 0)
left = 0 - 1;
if (SystemInformation.VirtualScreen.Height < height + top)
height = SystemInformation.VirtualScreen.Height - top;
if (SystemInformation.VirtualScreen.Width < width + left)
width = SystemInformation.VirtualScreen.Width - left;
#endregion
Top = top;
Left = left;
Height = height;
Width = width;
}
catch (Exception ex)
{
LogWriter.Log(ex, "Error • Snap To Window");
}
finally
{
ReleaseMouseCapture();
}
#endregion
}