WPF:鼠标长时间无操作,窗口隐藏

时间:2022-01-25 18:38:59

//设置鼠标长时间无操作计时器
private System.Timers.Timer MouseTimerTick = new System.Timers.Timer(10000);

private void Window_Activated(object sender, EventArgs e)
{
  this.Dispatcher.Invoke(new Action(() =>
  {
    //显示
  }));
  MouseTimerTick.Stop();
}

private void Window_Deactivated(object sender, EventArgs e)
{
  MouseTimerTick.Start();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  MouseTimerTick.Elapsed += new System.Timers.ElapsedEventHandler(TimerTick);
  MouseTimerTick.AutoReset = true;
  MouseTimerTick.Enabled = true;
}
private void TimerTick(object source, System.Timers.ElapsedEventArgs e)
{
  this.Dispatcher.Invoke(new Action(() =>
  {
    //隐藏
  }));
}