//private bool _stopPlaying = false;
private CancellationTokenSource _stopPlayingCts = null;
private void BtnPlay_OnClick(object sender, RoutedEventArgs e)
{
try
{
Playing();
}
catch (Exception)
{
}
}
//注:这里是一个异步操作。如果没有停止第一次的加载播放,再加载播放,他就会叠加操作。
private async void Playing()
{
//_stopPlaying = false;
// 如果 _stopPlayingCts 已经被创建,则先释放它
if (_stopPlayingCts != null)
{
_stopPlayingCts.Dispose();
_stopPlayingCts = null;
}
_stopPlayingCts = new CancellationTokenSource();
_mask.Topmost = false;
_mask.Topmost = true;
//while (_stopPlaying == false && _currentRecords.Count != 0)
while (!_stopPlayingCts.IsCancellationRequested && _currentRecords.Count != 0)
{
_currentRecord = _currentRecords[0];
foreach (var t in _currentRecord.ActionList)
{
if (_stopPlayingCts.IsCancellationRequested) return;
await Util.DelayAsync((int)t.Duration.TotalMilliseconds, _stopPlayingCts.Token);
if (_stopPlayingCts.IsCancellationRequested) return;
// 模拟移动
var point = Win32Helper.TransformPxToDx(t.Point);
if (point != null)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_ABSOLUTE | MouseSimulator.MOUSEEVENTF_MOVE, point.Value.X, point.Value.Y, 0, 0);
}
try
{
switch (t.ActionType)
{
case ActionTypes.Up:
if (t.MouseButton == MouseButtons.Left)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
else if (t.MouseButton == MouseButtons.Right)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
else if (t.MouseButton == MouseButtons.Middle)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
break;
case ActionTypes.Down:
if (t.MouseButton == MouseButtons.Left)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
else if (t.MouseButton == MouseButtons.Right)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
else if (t.MouseButton == MouseButtons.Middle)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
break;
case ActionTypes.Click:
if (t.MouseButton == MouseButtons.Left)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
else if (t.MouseButton == MouseButtons.Right)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
else if (t.MouseButton == MouseButtons.Middle)
{
MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
_mask.ShowClick(_mask.ClickIndicator, t.Point);
}
break;
case ActionTypes.DClick:
// TODO 暂未处理
break;
}
}
catch (Exception)
{
}
}
_currentRecords.Remove(_currentRecord);
}
}
————————————————————————————————
private void StopPlaying()
{
//_stopPlaying = true;
if (_stopPlayingCts != null && !_stopPlayingCts.IsCancellationRequested)
_stopPlayingCts.Cancel();
_mask.Top = -100000;
}
——————————————————————————————
private void OnClosing(object sender, CancelEventArgs args)
{
_mouseHook.Stop();
_mask.Close();
if (_stopPlayingCts != null)
_stopPlayingCts.Dispose();
——————————————————————————
///
/// 异步延时
///
///
///
public static async Task DelayAsync(int ms, CancellationToken token)
{
try
{
await Task.Delay(ms, token);
}
catch (TaskCanceledException)
{
// 在这里处理取消操作或者重新抛出异常
// 例如:执行清理操作、记录日志、重新抛出异常等
// 如果不需要处理,可以空着,让异常传播到调用方
}
}
//private bool _stopPlaying = false;
private CancellationTokenSource _stopPlayingCts = null;
private void BtnPlay_OnClick(object sender, RoutedEventArgs e)
{
try
{
Playing();
}
catch (Exception)
{
}
}
//注:这里是一个异步操作。如果没有停止第一次的加载播放,再加载播放,他就会叠加操作。
private async void Playing()
{
//_stopPlaying = false;
}
————————————————————————————————
private void StopPlaying()
{
//_stopPlaying = true;
if (_stopPlayingCts != null && !_stopPlayingCts.IsCancellationRequested)
_stopPlayingCts.Cancel();
_mask.Top = -100000;
}
——————————————————————————————
private void OnClosing(object sender, CancelEventArgs args)
{
_mouseHook.Stop();
_mask.Close();
if (_stopPlayingCts != null)
_stopPlayingCts.Dispose();
——————————————————————————
///
/// 异步延时
///
///
///
public static async Task DelayAsync(int ms, CancellationToken token)
{
try
{
await Task.Delay(ms, token);
}
catch (TaskCanceledException)
{
// 在这里处理取消操作或者重新抛出异常
// 例如:执行清理操作、记录日志、重新抛出异常等
// 如果不需要处理,可以空着,让异常传播到调用方
}
}