Files
GeekDesk/MyThread/MouseHookThread.cs

116 lines
3.5 KiB
C#
Raw Normal View History

using GeekDesk.Control.UserControls.Config;
using GeekDesk.Control.Windows;
using GeekDesk.Util;
using GeekDesk.ViewModel;
using Gma.System.MouseKeyHook;
2022-08-29 16:47:33 +08:00
using ShowSeconds;
using System;
using System.Drawing;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
2022-03-25 16:23:14 +08:00
namespace GeekDesk.MyThread
{
public class MouseHookThread
{
2022-08-29 16:47:33 +08:00
private static readonly AppConfig appConfig = MainWindow.appData.AppConfig;
public static Dispatcher dispatcher;
private static UserActivityHook hook;
2022-08-29 16:47:33 +08:00
public static void Hook()
{
2022-08-29 16:47:33 +08:00
//使用dispatcher来单独监听UI线程 防止程序卡顿
dispatcher = DispatcherBuild.Build();
dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
2022-08-29 16:47:33 +08:00
hook = new UserActivityHook();
if (appConfig.MouseMiddleShow)
{
hook.OnMouseWheelUp += OnMouseWheelUp;
}
if (appConfig.SecondsWindow == true)
{
hook.OnMouseLeftDown += OnMouseLeftDown;
hook.OnMouseLeftUp += OnMouseLeftUp;
}
hook.Start(true, false);
}));
}
2022-08-29 16:47:33 +08:00
private static void OnMouseLeftDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
2022-08-29 16:47:33 +08:00
SecondsWindow.SecondsBakColorFun(sender, e);
}
2022-08-29 16:47:33 +08:00
private static void OnMouseLeftUp(object sender, System.Windows.Forms.MouseEventArgs e)
2021-12-13 13:03:50 +08:00
{
2022-08-29 16:47:33 +08:00
SecondsWindow.SecondsHookSetFuc(sender, e);
2021-12-13 13:03:50 +08:00
}
2022-08-29 16:47:33 +08:00
private static void OnMouseWheelUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
2022-08-29 16:47:33 +08:00
MouseWheelShowApp(sender, e);
}
/// <summary>
/// 鼠标中键呼出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2022-08-29 16:47:33 +08:00
private static void MouseWheelShowApp(object sender, System.Windows.Forms.MouseEventArgs e)
{
2022-08-29 16:47:33 +08:00
//中键打开App
if (appConfig.MouseMiddleShow && MotionControl.hotkeyFinished)
{
2022-08-29 16:47:33 +08:00
App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
2022-08-29 16:47:33 +08:00
if (MainWindow.mainWindow.Visibility == Visibility.Collapsed || MainWindow.mainWindow.Opacity == 0)
{
2022-08-29 16:47:33 +08:00
MainWindow.ShowApp();
}
2022-08-29 16:47:33 +08:00
else
{
MainWindow.HideApp();
}
}));
}
}
2022-08-29 16:47:33 +08:00
public static void Dispose()
{
try
{
2022-08-29 16:47:33 +08:00
if (hook != null)
{
2022-08-29 16:47:33 +08:00
if (hook.MouseWheelUpEnable())
{
hook.OnMouseWheelUp -= OnMouseWheelUp;
}
if (hook.MouseLeftDownEnable())
{
hook.OnMouseLeftDown -= OnMouseLeftDown;
}
if (hook.MouseLeftUpEnable())
{
hook.OnMouseLeftUp -= OnMouseLeftUp;
}
hook.Stop();
dispatcher.InvokeShutdown();
hook = null;
dispatcher = null;
}
}
2022-08-29 16:47:33 +08:00
catch (Exception ex) {
LogUtil.WriteErrorLog(ex, "关闭hook出错");
}
}
}
}