diff --git a/App.config b/App.config index 448be3e..c6dd0cd 100644 --- a/App.config +++ b/App.config @@ -8,9 +8,9 @@ - - - + + + diff --git a/Control/UserControls/Config/MotionControl.xaml.cs b/Control/UserControls/Config/MotionControl.xaml.cs index af95337..1e2a332 100644 --- a/Control/UserControls/Config/MotionControl.xaml.cs +++ b/Control/UserControls/Config/MotionControl.xaml.cs @@ -315,14 +315,17 @@ namespace GeekDesk.Control.UserControls.Config /// private void MouseMiddle_Changed(object sender, RoutedEventArgs e) { - if (appConfig.MouseMiddleShow) - { - MouseHookThread.MiddleHook(); - } - else - { - MouseHookThread.DisposeMiddle(); - } + //if (appConfig.MouseMiddleShow) + //{ + // MouseHookThread.MiddleHook(); + //} + //else + //{ + // MouseHookThread.DisposeMiddle(); + //} + + MouseHookThread.Dispose(); + MouseHookThread.Hook(); } /// diff --git a/Control/UserControls/Config/OtherControl.xaml.cs b/Control/UserControls/Config/OtherControl.xaml.cs index c65d507..eb6adfd 100644 --- a/Control/UserControls/Config/OtherControl.xaml.cs +++ b/Control/UserControls/Config/OtherControl.xaml.cs @@ -121,15 +121,21 @@ namespace GeekDesk.Control.UserControls.Config private void ShowSeconds_Click(object sender, RoutedEventArgs e) { - if (MainWindow.appData.AppConfig.SecondsWindow == true) - { - //StartSecondsWindow(); - SecondsWindow.ShowWindow(); - } - else - { - SecondsWindow.Dispose(); - } + //if (MainWindow.appData.AppConfig.SecondsWindow == true) + //{ + // //StartSecondsWindow(); + // //SecondsWindow.ShowWindow(); + // MouseHookThread.Dispose(); + // MouseHookThread.Hook(); + //} + //else + //{ + // MouseHookThread.Dispose(); + // MouseHookThread.Hook(); + //} + + MouseHookThread.Dispose(); + MouseHookThread.Hook(); } public static void StopSecondsWindow() diff --git a/Control/UserControls/PannelCard/RightCardControl.xaml.cs b/Control/UserControls/PannelCard/RightCardControl.xaml.cs index 38a352f..e2b568b 100644 --- a/Control/UserControls/PannelCard/RightCardControl.xaml.cs +++ b/Control/UserControls/PannelCard/RightCardControl.xaml.cs @@ -178,8 +178,15 @@ namespace GeekDesk.Control.UserControls.PannelCard try { - using (Process p = new Process()) - { + Process p = new Process(); + //using () + //{ + p.StartInfo.UseShellExecute = false; + p.StartInfo.RedirectStandardInput = true; + p.StartInfo.RedirectStandardOutput = true; + p.StartInfo.RedirectStandardError = true; + p.StartInfo.CreateNoWindow = true; + string startArg = icon.StartArg; if (startArg != null && Constants.SYSTEM_ICONS.ContainsKey(startArg)) @@ -221,11 +228,7 @@ namespace GeekDesk.Control.UserControls.PannelCard switch (type) { case IconStartType.ADMIN_STARTUP: - //p.StartInfo.Arguments = "1";//启动参数 p.StartInfo.Verb = "runas"; - //p.StartInfo.CreateNoWindow = false; //设置显示窗口 - p.StartInfo.UseShellExecute = true;//不使用操作系统外壳程序启动进程 - //p.StartInfo.ErrorDialog = false; if (appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL) { //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口 @@ -243,7 +246,7 @@ namespace GeekDesk.Control.UserControls.PannelCard } break;// c#好像不能case穿透 - case IconStartType.DEFAULT_STARTUP: + case IconStartType.DEFAULT_STARTUP: if (appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL) { //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口 @@ -285,13 +288,15 @@ namespace GeekDesk.Control.UserControls.PannelCard } } p.Start(); + p.Close(); + p.Dispose(); if (useRelativePath) { //如果使用相对路径启动成功 那么重新设置程序绝对路径 icon.Path = path; } } - } + //} icon.Count++; //隐藏搜索框 @@ -311,7 +316,7 @@ namespace GeekDesk.Control.UserControls.PannelCard HandyControl.Controls.Growl.WarningGlobal("程序启动失败(可能为不支持的启动方式)!"); LogUtil.WriteErrorLog(e, "程序启动失败:path=" + icon.Path + ",type=" + type); } - } + } } @@ -459,20 +464,20 @@ namespace GeekDesk.Control.UserControls.PannelCard RunTimeStatus.MOUSE_ENTER_ICON = true; if (!RunTimeStatus.ICONLIST_MOUSE_WHEEL) { - new Thread(() => + ThreadPool.QueueUserWorkItem(state => { this.Dispatcher.BeginInvoke(new Action(() => { IconInfo info = (sender as Panel).Tag as IconInfo; MyPoptipContent.Text = info.Content; MyPoptip.VerticalOffset = 30; - Thread.Sleep(100); + Thread.Sleep(50); if (!RunTimeStatus.ICONLIST_MOUSE_WHEEL) { MyPoptip.IsOpen = true; } })); - }).Start(); + }); } @@ -481,30 +486,28 @@ namespace GeekDesk.Control.UserControls.PannelCard double height = appData.AppConfig.ImageHeight; width += width * 0.15; height += height * 0.15; - Thread t = new Thread(() => + + ThreadPool.QueueUserWorkItem(state => { this.Dispatcher.BeginInvoke(new Action(() => { ImgStoryBoard(sender, (int)width, (int)height, 1, true); })); }); - t.IsBackground = true; - t.Start(); } private void MenuIcon_MouseLeave(object sender, MouseEventArgs e) { RunTimeStatus.MOUSE_ENTER_ICON = false; MyPoptip.IsOpen = false; - Thread t = new Thread(() => + + ThreadPool.QueueUserWorkItem(state => { - this.Dispatcher.BeginInvoke(new Action(() => - { - ImgStoryBoard(sender, appData.AppConfig.ImageWidth, appData.AppConfig.ImageHeight, 260); - })); + this.Dispatcher.BeginInvoke(new Action(() => + { + ImgStoryBoard(sender, appData.AppConfig.ImageWidth, appData.AppConfig.ImageHeight, 260); + })); }); - t.IsBackground = true; - t.Start(); } @@ -909,7 +912,6 @@ namespace GeekDesk.Control.UserControls.PannelCard { index = 0; } - Console.WriteLine("进入"); MainWindow.mainWindow.LeftCard.MenuListBox.SelectedIndex = index; scrollViewer.ScrollToVerticalOffset(0); } diff --git a/Control/Windows/PixelColorPickerWindow.xaml b/Control/Windows/PixelColorPickerWindow.xaml index c6769af..7a003f7 100644 --- a/Control/Windows/PixelColorPickerWindow.xaml +++ b/Control/Windows/PixelColorPickerWindow.xaml @@ -8,6 +8,7 @@ Background="Black" PreviewMouseMove="Window_PreviewMouseMove" MouseLeftButtonDown="Window_MouseLeftButtonDown" + MouseRightButtonDown="Window_MouseRightButtonDown" MouseWheel="Window_MouseWheel" > diff --git a/Control/Windows/PixelColorPickerWindow.xaml.cs b/Control/Windows/PixelColorPickerWindow.xaml.cs index a1917aa..28a0cfb 100644 --- a/Control/Windows/PixelColorPickerWindow.xaml.cs +++ b/Control/Windows/PixelColorPickerWindow.xaml.cs @@ -39,11 +39,11 @@ namespace GeekDesk.Control.Windows { InitializeComponent(); this.colorPicker = colorPicker; - try - { - SetProcessDPIAware(); - } - catch (Exception e) { } + //try + //{ + // SetProcessDPIAware(); + //} + //catch (Exception e) { } ColorPickerWindow_Init(); } @@ -206,5 +206,16 @@ namespace GeekDesk.Control.Windows SetPixelAbout(e); } + /// + /// 右键按下 + /// + /// + /// + private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e) + { + GlobalColorPickerWindow.ShowOrHide(); + //关闭 + this.Close(); + } } } diff --git a/GeekDesk.csproj b/GeekDesk.csproj index 6e1e2b7..36adf2d 100644 --- a/GeekDesk.csproj +++ b/GeekDesk.csproj @@ -76,6 +76,9 @@ false + + app.manifest + packages\CommonServiceLocator.2.0.6\lib\net45\CommonServiceLocator.dll @@ -86,9 +89,6 @@ packages\HandyControl.3.3.0\lib\net472\HandyControl.dll - - packages\KeyMouseHook.1.0.6\lib\net40\KeyMouseHook.dll - packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll @@ -295,6 +295,7 @@ + @@ -304,6 +305,7 @@ + @@ -463,6 +465,7 @@ Resources.Designer.cs + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 4ae4b84..5177737 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -174,7 +174,6 @@ namespace GeekDesk this.Height = appData.AppConfig.WindowHeight; } - /// /// 窗口加载完毕 执行方法 /// @@ -182,6 +181,7 @@ namespace GeekDesk /// void Window_Loaded(object sender, RoutedEventArgs e) { + SecondsWindow.ShowWindow(); BGSettingUtil.BGSetting(); if (!appData.AppConfig.StartedShowPanel) @@ -216,17 +216,12 @@ namespace GeekDesk RegisterUtil.SetSelfStarting(appData.AppConfig.SelfStartUp, Constants.MY_NAME); } - //注册鼠标中键监听事件 - if (appData.AppConfig.MouseMiddleShow) + //注册鼠标监听事件 + if (appData.AppConfig.MouseMiddleShow || appData.AppConfig.SecondsWindow==true) { - MouseHookThread.MiddleHook(); - } - - //启动显秒程序 - if (appData.AppConfig.SecondsWindow == true) - { - SecondsWindow.ShowWindow(); + MouseHookThread.Hook(); } + //更新线程开启 检测更新 UpdateThread.Update(); @@ -466,6 +461,11 @@ namespace GeekDesk else { appData.AppConfig.IsShow = null; + //防止永远不显示界面 + if (mainWindow.Opacity < 1) + { + mainWindow.Opacity = 1; + } } @@ -677,11 +677,10 @@ namespace GeekDesk /// private void ExitApp(object sender, RoutedEventArgs e) { - if (appData.AppConfig.MouseMiddleShow) + if (appData.AppConfig.MouseMiddleShow || appData.AppConfig.SecondsWindow==true) { - MouseHookThread.DisposeMiddle(); + MouseHookThread.Dispose(); } - SecondsWindow.Dispose(); Application.Current.Shutdown(); } /// @@ -691,13 +690,11 @@ namespace GeekDesk /// private void ReStartApp(object sender, RoutedEventArgs e) { - if (appData.AppConfig.MouseMiddleShow) + if (appData.AppConfig.MouseMiddleShow || appData.AppConfig.SecondsWindow == true) { - MouseHookThread.DisposeMiddle(); + MouseHookThread.Dispose(); } - SecondsWindow.Dispose(); - Process p = new Process(); p.StartInfo.FileName = Constants.APP_DIR + "GeekDesk.exe"; p.StartInfo.WorkingDirectory = Constants.APP_DIR; diff --git a/MyThread/MouseHookThread.cs b/MyThread/MouseHookThread.cs index 2d0b1b2..17b35f5 100644 --- a/MyThread/MouseHookThread.cs +++ b/MyThread/MouseHookThread.cs @@ -3,6 +3,7 @@ using GeekDesk.Control.Windows; using GeekDesk.Util; using GeekDesk.ViewModel; using Gma.System.MouseKeyHook; +using ShowSeconds; using System; using System.Drawing; using System.Threading; @@ -13,89 +14,100 @@ namespace GeekDesk.MyThread { public class MouseHookThread { - private static AppConfig appConfig = MainWindow.appData.AppConfig; - private static IKeyboardMouseEvents middleHook = null; - private static Dispatcher middleDP; + private static readonly AppConfig appConfig = MainWindow.appData.AppConfig; + public static Dispatcher dispatcher; + private static UserActivityHook hook; - public static void MiddleHook() + public static void Hook() { - //使用dispatcher来单独监听UI线程 防止程序卡顿 - middleDP = DispatcherBuild.Build(); - middleHook = Hook.GlobalEvents(); - middleDP.Invoke((Action)(() => + //使用dispatcher来单独监听UI线程 防止程序卡顿 + dispatcher = DispatcherBuild.Build(); + dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { - middleHook.MouseUpExt += MiddleHookFun; + hook = new UserActivityHook(); + + if (appConfig.MouseMiddleShow) + { + hook.OnMouseWheelUp += OnMouseWheelUp; + } + + if (appConfig.SecondsWindow == true) + { + hook.OnMouseLeftDown += OnMouseLeftDown; + hook.OnMouseLeftUp += OnMouseLeftUp; + } + + hook.Start(true, false); })); } - - private static Color GetBottomBeforeColor() + private static void OnMouseLeftDown(object sender, System.Windows.Forms.MouseEventArgs e) { - return GetColor(1760, 985); + SecondsWindow.SecondsBakColorFun(sender, e); } - private static Color GetTopBeforeColor() + private static void OnMouseLeftUp(object sender, System.Windows.Forms.MouseEventArgs e) { - return GetColor(1751, 693); + SecondsWindow.SecondsHookSetFuc(sender, e); } - private static Color GetColor(int w2, int h2) + private static void OnMouseWheelUp(object sender, System.Windows.Forms.MouseEventArgs e) { - double w = 1920; - double h = 1080; - double width = SystemParameters.PrimaryScreenWidth; - double height = SystemParameters.PrimaryScreenHeight; - System.Drawing.Point p = new System.Drawing.Point((int)(w2 / w * width), (int)(h2 / h * height)); - return ScreenUtil.GetColorAt(p); + MouseWheelShowApp(sender, e); } - - /// /// 鼠标中键呼出 /// /// /// - private static void MiddleHookFun(object sender, System.Windows.Forms.MouseEventArgs e) + private static void MouseWheelShowApp(object sender, System.Windows.Forms.MouseEventArgs e) { - if (e.Button == System.Windows.Forms.MouseButtons.Middle) + //中键打开App + if (appConfig.MouseMiddleShow && MotionControl.hotkeyFinished) { - //中键打开App - if (appConfig.MouseMiddleShow) + App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { - if (MotionControl.hotkeyFinished) + if (MainWindow.mainWindow.Visibility == Visibility.Collapsed || MainWindow.mainWindow.Opacity == 0) { - App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() => - { - if (MainWindow.mainWindow.Visibility == Visibility.Collapsed || MainWindow.mainWindow.Opacity == 0) - { - MainWindow.ShowApp(); - } - else - { - MainWindow.HideApp(); - } - })); + MainWindow.ShowApp(); } - } + else + { + MainWindow.HideApp(); + } + })); } } - public static void DisposeMiddle() + public static void Dispose() { try { - if (middleHook != null) + if (hook != null) { - middleHook.MouseUpExt -= MiddleHookFun; - middleHook.Dispose(); - middleDP.InvokeShutdown(); + 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; } - } - catch (Exception ex) { } - + catch (Exception ex) { + LogUtil.WriteErrorLog(ex, "关闭hook出错"); + } } diff --git a/Plugins/ShowSeconds/SecondsWindow.xaml.cs b/Plugins/ShowSeconds/SecondsWindow.xaml.cs index 47047d3..56ac958 100644 --- a/Plugins/ShowSeconds/SecondsWindow.xaml.cs +++ b/Plugins/ShowSeconds/SecondsWindow.xaml.cs @@ -1,7 +1,6 @@ using GeekDesk; using GeekDesk.MyThread; using GeekDesk.Util; -using Gma.System.MouseKeyHook; using ShowSeconds.ViewModel; using System; using System.Collections; @@ -33,8 +32,8 @@ namespace ShowSeconds public partial class SecondsWindow : Window { - private Color beforeColor; - private Color topBeforeColor; + private static Color beforeColor; + private static Color topBeforeColor; //dark theam private readonly static System.Windows.Media.SolidColorBrush darkBG @@ -62,15 +61,11 @@ namespace ShowSeconds Color = System.Windows.Media.Color.FromRgb(65, 63, 61), }; - private bool expandClock = true; //是否展开时钟 - private System.Windows.Forms.Timer timer; - - Dispatcher secondsDP = DispatcherBuild.Build(); - IKeyboardMouseEvents secondsHook = Hook.GlobalEvents(); - - private double lProportion = 0.82; - private double tProportion = 0.03; - private int sleepTime = 800; + private static bool expandClock = true; //是否展开时钟 + private static System.Windows.Forms.Timer timer; + private static double lProportion = 0.82; + private static double tProportion = 0.03; + private static int sleepTime = 800; public SecondsWindow() { SecondsDataContext dc = new SecondsDataContext @@ -101,20 +96,148 @@ namespace ShowSeconds BGBorder.Visibility = Visibility.Collapsed; } - private void Window_Loaded(object sender, RoutedEventArgs e) { timer = new System.Windows.Forms.Timer(); timer.Interval = 1000; - timer.Tick += Timer_Tick; - secondsDP.Invoke((Action)(() => - { - secondsHook.MouseDownExt += SecondsBakColorFun; - secondsHook.MouseUpExt += SecondsHookSetFuc; - })); + timer.Tick += Timer_Tick; HideWindowUtil.HideAltTab(this); } + public static void SecondsHookSetFuc(object sender, System.Windows.Forms.MouseEventArgs e) + { + ThreadPool.QueueUserWorkItem(state => + { + window.Dispatcher.Invoke(() => + { + if (ScreenUtil.IsPrimaryFullScreen()) return; + + App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() => + { + int x = e.X; + int y = e.Y; + + //获取实际坐标 windows可能会有缩放 + IntPtr hdc = GetDC(IntPtr.Zero); + double scale = ScreenUtil.GetScreenScalingFactor(); + + x = (int)(x / scale); + y = (int)(y / scale); + + double w = 1920; + double h = 1080; + double width = SystemParameters.PrimaryScreenWidth; + double height = SystemParameters.PrimaryScreenHeight; + + if (x > 1843 / w * width + && x < 1907 / w * width + && y > 1037 / h * height + && y < 1074 / h * height) + { + Color c; + int count = sleepTime; + do + { + c = GetBottomBeforeColor(); + if (c.A != beforeColor.A + || c.R != beforeColor.R + || c.G != beforeColor.G + || c.B != beforeColor.B) + { + break; + } + Thread.Sleep(50); + count -= 50; + } while (count > 0); + + if (c.A != beforeColor.A + || c.R != beforeColor.R + || c.G != beforeColor.G + || c.B != beforeColor.B) + { + //判断是否展开时钟 + Color ct = GetTopBeforeColor(); + if (ct.A != topBeforeColor.A + || ct.R != topBeforeColor.R + || ct.G != topBeforeColor.G + || ct.B != topBeforeColor.B) + { + expandClock = true; + } + else + { + expandClock = false; + } + + if (!window.BGBorder.IsVisible) + { + Color theamColor = GetColor(1919, 1079); + if (CalculateLight(theamColor) > 255 / 2) + { + //light + window.BGBorder.Background = lightBG; + window.SecondsText.Foreground = lightFont; + } + else + { + // dark + window.BGBorder.Background = darkBG; + window.SecondsText.Foreground = darkFont; + } + + SecondsDataContext dc = window.DataContext as SecondsDataContext; + dc.Seconds = (DateTime.Now.Hour).ToString() + ":" + + FormatMS(DateTime.Now.Minute) + ":" + + FormatMS(DateTime.Now.Second); + + int sx = (int)(width * lProportion); + int sMarginBottom = (int)(height * tProportion); + window.Left = sx - window.Width; + window.Top = SystemParameters.WorkArea.Height - window.Height; + window.BGBorder.Visibility = Visibility.Visible; + timer.Start(); + } + else + { + window.BGBorder.Visibility = Visibility.Collapsed; + timer.Stop(); + } + } + } + else if ((expandClock && (x < 1574 / w * width + || x > 1906 / w * width + || y < 598 / h * height + || y > 1020 / h * height) + ) + || !expandClock && (x < 1574 / w * width + || x > 1906 / w * width + || y < 950 / h * height + || y > 1020 / h * height) + ) + { + window.BGBorder.Visibility = Visibility.Collapsed; + timer.Stop(); + } + })); + }); + }); + + } + + public static void SecondsBakColorFun(object sender, System.Windows.Forms.MouseEventArgs e) + { + ThreadPool.QueueUserWorkItem(state => + { + window.Dispatcher.Invoke(() => + { + beforeColor = GetBottomBeforeColor(); + topBeforeColor = GetTopBeforeColor(); + }); + + }); + } + + private void Timer_Tick(object sender, EventArgs e) { string str = (DateTime.Now.Hour).ToString() + ":" + @@ -127,8 +250,6 @@ namespace ShowSeconds this.DataContext = dc; } - - private static string FormatMS(int ms) { if (ms < 10) @@ -141,117 +262,6 @@ namespace ShowSeconds } } - private void SecondsHookSetFuc(object sender, MouseEventExtArgs e) - { - if (e.Button == System.Windows.Forms.MouseButtons.Left) - { - if (ScreenUtil.IsPrimaryFullScreen()) return; - - App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() => - { - int x = e.X; - int y = e.Y; - - //获取实际坐标 windows可能会有缩放 - IntPtr hdc = GetDC(IntPtr.Zero); - double scale = ScreenUtil.GetScreenScalingFactor(); - - x = (int)(x / scale); - y = (int)(y / scale); - - double w = 1920; - double h = 1080; - double width = SystemParameters.PrimaryScreenWidth; - double height = SystemParameters.PrimaryScreenHeight; - - if (x > 1843 / w * width - && x < 1907 / w * width - && y > 1037 / h * height - && y < 1074 / h * height) - { - Thread.Sleep(sleepTime); - Color c = GetBottomBeforeColor(); - if (c.A != beforeColor.A - || c.R != beforeColor.R - || c.G != beforeColor.G - || c.B != beforeColor.B) - { - //判断是否展开时钟 - Color ct = GetTopBeforeColor(); - if (ct.A != topBeforeColor.A - || ct.R != topBeforeColor.R - || ct.G != topBeforeColor.G - || ct.B != topBeforeColor.B) - { - expandClock = true; - } - else - { - expandClock = false; - } - - if (!BGBorder.IsVisible) - { - Color theamColor = GetColor(1919, 1079); - if (CalculateLight(theamColor) > 255 / 2) - { - //light - BGBorder.Background = lightBG; - SecondsText.Foreground = lightFont; - } - else - { - // dark - BGBorder.Background = darkBG; - SecondsText.Foreground = darkFont; - } - - SecondsDataContext dc = this.DataContext as SecondsDataContext; - dc.Seconds = (DateTime.Now.Hour).ToString() + ":" + - FormatMS(DateTime.Now.Minute) + ":" + - FormatMS(DateTime.Now.Second); - - int sx = (int)(width * lProportion); - int sMarginBottom = (int)(height * tProportion); - Left = sx - Width; - Top = SystemParameters.WorkArea.Height - Height; - BGBorder.Visibility = Visibility.Visible; - timer.Start(); - } - else - { - BGBorder.Visibility= Visibility.Collapsed; - timer.Stop(); - } - } - } - else if ((expandClock && (x < 1574 / w * width - || x > 1906 / w * width - || y < 598 / h * height - || y > 1020 / h * height) - ) - || !expandClock && (x < 1574 / w * width - || x > 1906 / w * width - || y < 950 / h * height - || y > 1020 / h * height) - ) - { - BGBorder.Visibility = Visibility.Collapsed; - timer.Stop(); - } - })); - } - } - - private void SecondsBakColorFun(object sender, MouseEventExtArgs e) - { - if (e.Button == System.Windows.Forms.MouseButtons.Left) - { - beforeColor = GetBottomBeforeColor(); - topBeforeColor = GetTopBeforeColor(); - } - } - private static Color GetBottomBeforeColor() { return GetColor(1760, 985); @@ -270,8 +280,6 @@ namespace ShowSeconds double height = SystemParameters.PrimaryScreenHeight; double scale = ScreenUtil.GetScreenScalingFactor(); - Console.WriteLine("bef:" + w2 / w * width); - Console.WriteLine("af:" + w2 / w * width * scale); System.Drawing.Point p = new System.Drawing.Point((int)(w2 / w * width * scale), (int)(h2 / h * height * scale)); return ScreenUtil.GetColorAt(p); } @@ -295,7 +303,6 @@ namespace ShowSeconds if ("Shutdown".Equals(cds.msg)) { - Dispose(); Application.Current.Shutdown(); } } @@ -303,22 +310,6 @@ namespace ShowSeconds } - public static void Dispose() - { - try - { - if (window.secondsHook != null) - { - window.secondsHook.MouseDownExt -= window.SecondsBakColorFun; - window.secondsHook.MouseUpExt -= window.SecondsHookSetFuc; - window.secondsHook.Dispose(); - window.secondsDP.InvokeShutdown(); - } - window.Close(); - } - catch (Exception ex) { } - } - private static int CalculateLight(Color color) { int[] colorArr = new int[] {color.R, color.G, color.B}; diff --git a/Task/ToDoTask.cs b/Task/ToDoTask.cs index d5c2b50..17a197b 100644 --- a/Task/ToDoTask.cs +++ b/Task/ToDoTask.cs @@ -46,7 +46,7 @@ namespace GeekDesk.Task } } } - ClearMemory(); + //ClearMemory(); })); } diff --git a/Util/MouseHook.cs b/Util/MouseHook.cs new file mode 100644 index 0000000..9822972 --- /dev/null +++ b/Util/MouseHook.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace GeekDesk.Util +{ + public class MouseHook + { + + private Point point; + private Point Point + { + get { return point; } + set + { + if (point != value) + { + point = value; + if (MouseMoveEvent != null) + { + var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0); + MouseMoveEvent(this, e); + } + } + } + } + private int hHook; + private const int WM_MOUSEMOVE = 0x200; + private const int WM_LBUTTONDOWN = 0x201; + private const int WM_RBUTTONDOWN = 0x204; + private const int WM_MBUTTONDOWN = 0x207; + private const int WM_LBUTTONUP = 0x202; + private const int WM_RBUTTONUP = 0x205; + private const int WM_MBUTTONUP = 0x208; + private const int WM_LBUTTONDBLCLK = 0x203; + private const int WM_RBUTTONDBLCLK = 0x206; + private const int WM_MBUTTONDBLCLK = 0x209; + public const int WH_MOUSE_LL = 14; + public Win32Api.HookProc hProc; + public MouseHook() + { + this.Point = new Point(); + } + public int SetHook() + { + hProc = new Win32Api.HookProc(MouseHookProc); + hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); + return hHook; + } + public void UnHook() + { + Win32Api.UnhookWindowsHookEx(hHook); + } + private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) + { + Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct)); + if (nCode < 0) + { + return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); + } + else + { + if (MouseClickEvent != null) + { + MouseButtons button = MouseButtons.None; + int clickCount = 0; + switch ((Int32)wParam) + { + case WM_LBUTTONDOWN: + button = MouseButtons.Left; + clickCount = 1; + MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); + break; + case WM_RBUTTONDOWN: + button = MouseButtons.Right; + clickCount = 1; + MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); + break; + case WM_MBUTTONDOWN: + button = MouseButtons.Middle; + clickCount = 1; + MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); + break; + case WM_LBUTTONUP: + button = MouseButtons.Left; + clickCount = 1; + MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); + break; + case WM_RBUTTONUP: + button = MouseButtons.Right; + clickCount = 1; + MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); + break; + case WM_MBUTTONUP: + button = MouseButtons.Middle; + clickCount = 1; + MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); + break; + } + + var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0); + MouseClickEvent(this, e); + } + this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y); + return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); + } + } + + public delegate void MouseMoveHandler(object sender, MouseEventArgs e); + public event MouseMoveHandler MouseMoveEvent; + + public delegate void MouseClickHandler(object sender, MouseEventArgs e); + public event MouseClickHandler MouseClickEvent; + + public delegate void MouseDownHandler(object sender, MouseEventArgs e); + public event MouseDownHandler MouseDownEvent; + + public delegate void MouseUpHandler(object sender, MouseEventArgs e); + public event MouseUpHandler MouseUpEvent; + + + public class Win32Api + { + [StructLayout(LayoutKind.Sequential)] + public class POINT + { + public int x; + public int y; + } + [StructLayout(LayoutKind.Sequential)] + public class MouseHookStruct + { + public POINT pt; + public int hwnd; + public int wHitTestCode; + public int dwExtraInfo; + } + public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] + public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] + public static extern bool UnhookWindowsHookEx(int idHook); + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] + public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam); + } + + } + + + +} diff --git a/Util/UserActivityHook.cs b/Util/UserActivityHook.cs new file mode 100644 index 0000000..297b2dd --- /dev/null +++ b/Util/UserActivityHook.cs @@ -0,0 +1,837 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection; +using System.Threading; +using System.Windows.Forms; +using System.ComponentModel; + +namespace GeekDesk.Util +{ + /// + /// This class allows you to tap keyboard and mouse and / or to detect their activity even when an + /// application runes in background or does not have any user interface at all. This class raises + /// common .NET events with KeyEventArgs and MouseEventArgs so you can easily retrive any information you need. + /// + public class UserActivityHook + { + #region Windows structure definitions + + /// + /// The POINT structure defines the x- and y- coordinates of a point. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_0tiq.asp + /// + [StructLayout(LayoutKind.Sequential)] + private class POINT + { + /// + /// Specifies the x-coordinate of the point. + /// + public int x; + /// + /// Specifies the y-coordinate of the point. + /// + public int y; + } + + /// + /// The MOUSEHOOKSTRUCT structure contains information about a mouse event passed to a WH_MOUSE hook procedure, MouseProc. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp + /// + [StructLayout(LayoutKind.Sequential)] + private class MouseHookStruct + { + /// + /// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates. + /// + public POINT pt; + /// + /// Handle to the window that will receive the mouse message corresponding to the mouse event. + /// + public int hwnd; + /// + /// Specifies the hit-test value. For a list of hit-test values, see the description of the WM_NCHITTEST message. + /// + public int wHitTestCode; + /// + /// Specifies extra information associated with the message. + /// + public int dwExtraInfo; + } + + /// + /// The MSLLHOOKSTRUCT structure contains information about a low-level keyboard input event. + /// + [StructLayout(LayoutKind.Sequential)] + private class MouseLLHookStruct + { + /// + /// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates. + /// + public POINT pt; + /// + /// If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta. + /// The low-order word is reserved. A positive value indicates that the wheel was rotated forward, + /// away from the user; a negative value indicates that the wheel was rotated backward, toward the user. + /// One wheel click is defined as WHEEL_DELTA, which is 120. + ///If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, + /// or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, + /// and the low-order word is reserved. This value can be one or more of the following values. Otherwise, mouseData is not used. + ///XBUTTON1 + ///The first X button was pressed or released. + ///XBUTTON2 + ///The second X button was pressed or released. + /// + public int mouseData; + /// + /// Specifies the event-injected flag. An application can use the following value to test the mouse flags. Value Purpose + ///LLMHF_INJECTED Test the event-injected flag. + ///0 + ///Specifies whether the event was injected. The value is 1 if the event was injected; otherwise, it is 0. + ///1-15 + ///Reserved. + /// + public int flags; + /// + /// Specifies the time stamp for this message. + /// + public int time; + /// + /// Specifies extra information associated with the message. + /// + public int dwExtraInfo; + } + + + /// + /// The KBDLLHOOKSTRUCT structure contains information about a low-level keyboard input event. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp + /// + [StructLayout(LayoutKind.Sequential)] + private class KeyboardHookStruct + { + /// + /// Specifies a virtual-key code. The code must be a value in the range 1 to 254. + /// + public int vkCode; + /// + /// Specifies a hardware scan code for the key. + /// + public int scanCode; + /// + /// Specifies the extended-key flag, event-injected flag, context code, and transition-state flag. + /// + public int flags; + /// + /// Specifies the time stamp for this message. + /// + public int time; + /// + /// Specifies extra information associated with the message. + /// + public int dwExtraInfo; + } + #endregion + + #region Windows function imports + /// + /// The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. + /// You would install a hook procedure to monitor the system for certain types of events. These events + /// are associated either with a specific thread or with all threads in the same desktop as the calling thread. + /// + /// + /// [in] Specifies the type of hook procedure to be installed. This parameter can be one of the following values. + /// + /// + /// [in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a + /// thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link + /// library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process. + /// + /// + /// [in] Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. + /// The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by + /// the current process and if the hook procedure is within the code associated with the current process. + /// + /// + /// [in] Specifies the identifier of the thread with which the hook procedure is to be associated. + /// If this parameter is zero, the hook procedure is associated with all existing threads running in the + /// same desktop as the calling thread. + /// + /// + /// If the function succeeds, the return value is the handle to the hook procedure. + /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp + /// + [DllImport("user32.dll", CharSet = CharSet.Auto, + CallingConvention = CallingConvention.StdCall, SetLastError = true)] + private static extern int SetWindowsHookEx( + int idHook, + HookProc lpfn, + IntPtr hMod, + int dwThreadId); + + /// + /// The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindowsHookEx function. + /// + /// + /// [in] Handle to the hook to be removed. This parameter is a hook handle obtained by a previous call to SetWindowsHookEx. + /// + /// + /// If the function succeeds, the return value is nonzero. + /// If the function fails, the return value is zero. To get extended error information, call GetLastError. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp + /// + [DllImport("user32.dll", CharSet = CharSet.Auto, + CallingConvention = CallingConvention.StdCall, SetLastError = true)] + private static extern int UnhookWindowsHookEx(int idHook); + + /// + /// The CallNextHookEx function passes the hook information to the next hook procedure in the current hook chain. + /// A hook procedure can call this function either before or after processing the hook information. + /// + /// Ignored. + /// + /// [in] Specifies the hook code passed to the current hook procedure. + /// The next hook procedure uses this code to determine how to process the hook information. + /// + /// + /// [in] Specifies the wParam value passed to the current hook procedure. + /// The meaning of this parameter depends on the type of hook associated with the current hook chain. + /// + /// + /// [in] Specifies the lParam value passed to the current hook procedure. + /// The meaning of this parameter depends on the type of hook associated with the current hook chain. + /// + /// + /// This value is returned by the next hook procedure in the chain. + /// The current hook procedure must also return this value. The meaning of the return value depends on the hook type. + /// For more information, see the descriptions of the individual hook procedures. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp + /// + [DllImport("user32.dll", CharSet = CharSet.Auto, + CallingConvention = CallingConvention.StdCall)] + private static extern int CallNextHookEx( + int idHook, + int nCode, + int wParam, + IntPtr lParam); + + /// + /// The CallWndProc hook procedure is an application-defined or library-defined callback + /// function used with the SetWindowsHookEx function. The HOOKPROC type defines a pointer + /// to this callback function. CallWndProc is a placeholder for the application-defined + /// or library-defined function name. + /// + /// + /// [in] Specifies whether the hook procedure must process the message. + /// If nCode is HC_ACTION, the hook procedure must process the message. + /// If nCode is less than zero, the hook procedure must pass the message to the + /// CallNextHookEx function without further processing and must return the + /// value returned by CallNextHookEx. + /// + /// + /// [in] Specifies whether the message was sent by the current thread. + /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. + /// + /// + /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. + /// + /// + /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. + /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx + /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC + /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook + /// procedure does not call CallNextHookEx, the return value should be zero. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/callwndproc.asp + /// + private delegate int HookProc(int nCode, int wParam, IntPtr lParam); + + /// + /// The ToAscii function translates the specified virtual-key code and keyboard + /// state to the corresponding character or characters. The function translates the code + /// using the input language and physical keyboard layout identified by the keyboard layout handle. + /// + /// + /// [in] Specifies the virtual-key code to be translated. + /// + /// + /// [in] Specifies the hardware scan code of the key to be translated. + /// The high-order bit of this value is set if the key is up (not pressed). + /// + /// + /// [in] Pointer to a 256-byte array that contains the current keyboard state. + /// Each element (byte) in the array contains the state of one key. + /// If the high-order bit of a byte is set, the key is down (pressed). + /// The low bit, if set, indicates that the key is toggled on. In this function, + /// only the toggle bit of the CAPS LOCK key is relevant. The toggle state + /// of the NUM LOCK and SCROLL LOCK keys is ignored. + /// + /// + /// [out] Pointer to the buffer that receives the translated character or characters. + /// + /// + /// [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise. + /// + /// + /// If the specified key is a dead key, the return value is negative. Otherwise, it is one of the following values. + /// Value Meaning + /// 0 The specified virtual key has no translation for the current state of the keyboard. + /// 1 One character was copied to the buffer. + /// 2 Two characters were copied to the buffer. This usually happens when a dead-key character + /// (accent or diacritic) stored in the keyboard layout cannot be composed with the specified + /// virtual key to form a single character. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/toascii.asp + /// + [DllImport("user32")] + private static extern int ToAscii( + int uVirtKey, + int uScanCode, + byte[] lpbKeyState, + byte[] lpwTransKey, + int fuState); + + /// + /// The GetKeyboardState function copies the status of the 256 virtual keys to the + /// specified buffer. + /// + /// + /// [in] Pointer to a 256-byte array that contains keyboard key states. + /// + /// + /// If the function succeeds, the return value is nonzero. + /// If the function fails, the return value is zero. To get extended error information, call GetLastError. + /// + /// + /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/toascii.asp + /// + [DllImport("user32")] + private static extern int GetKeyboardState(byte[] pbKeyState); + + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] + private static extern short GetKeyState(int vKey); + + #endregion + + #region Windows constants + + //values from Winuser.h in Microsoft SDK. + /// + /// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events. + /// + private const int WH_MOUSE_LL = 14; + /// + /// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events. + /// + private const int WH_KEYBOARD_LL = 13; + + /// + /// Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook procedure. + /// + private const int WH_MOUSE = 7; + /// + /// Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure. + /// + private const int WH_KEYBOARD = 2; + + /// + /// The WM_MOUSEMOVE message is posted to a window when the cursor moves. + /// + private const int WM_MOUSEMOVE = 0x200; + /// + /// The WM_LBUTTONDOWN message is posted when the user presses the left mouse button + /// + private const int WM_LBUTTONDOWN = 0x201; + /// + /// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button + /// + private const int WM_RBUTTONDOWN = 0x204; + /// + /// The WM_MBUTTONDOWN message is posted when the user presses the middle mouse button + /// + private const int WM_MBUTTONDOWN = 0x207; + /// + /// The WM_LBUTTONUP message is posted when the user releases the left mouse button + /// + private const int WM_LBUTTONUP = 0x202; + /// + /// The WM_RBUTTONUP message is posted when the user releases the right mouse button + /// + private const int WM_RBUTTONUP = 0x205; + /// + /// The WM_MBUTTONUP message is posted when the user releases the middle mouse button + /// + private const int WM_MBUTTONUP = 0x208; + /// + /// The WM_LBUTTONDBLCLK message is posted when the user double-clicks the left mouse button + /// + private const int WM_LBUTTONDBLCLK = 0x203; + /// + /// The WM_RBUTTONDBLCLK message is posted when the user double-clicks the right mouse button + /// + private const int WM_RBUTTONDBLCLK = 0x206; + /// + /// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button + /// + private const int WM_MBUTTONDBLCLK = 0x209; + /// + /// The WM_MOUSEWHEEL message is posted when the user presses the mouse wheel. + /// + private const int WM_MOUSEWHEEL = 0x020A; + + /// + /// The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem + /// key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed. + /// + private const int WM_KEYDOWN = 0x100; + /// + /// The WM_KEYUP message is posted to the window with the keyboard focus when a nonsystem + /// key is released. A nonsystem key is a key that is pressed when the ALT key is not pressed, + /// or a keyboard key that is pressed when a window has the keyboard focus. + /// + private const int WM_KEYUP = 0x101; + /// + /// The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user + /// presses the F10 key (which activates the menu bar) or holds down the ALT key and then + /// presses another key. It also occurs when no window currently has the keyboard focus; + /// in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that + /// receives the message can distinguish between these two contexts by checking the context + /// code in the lParam parameter. + /// + private const int WM_SYSKEYDOWN = 0x104; + /// + /// The WM_SYSKEYUP message is posted to the window with the keyboard focus when the user + /// releases a key that was pressed while the ALT key was held down. It also occurs when no + /// window currently has the keyboard focus; in this case, the WM_SYSKEYUP message is sent + /// to the active window. The window that receives the message can distinguish between + /// these two contexts by checking the context code in the lParam parameter. + /// + private const int WM_SYSKEYUP = 0x105; + + private const byte VK_SHIFT = 0x10; + private const byte VK_CAPITAL = 0x14; + private const byte VK_NUMLOCK = 0x90; + + #endregion + + /// + /// Creates an instance of UserActivityHook object and sets mouse and keyboard hooks. + /// + /// Any windows problem. + public UserActivityHook() + { + Start(); + } + + /// + /// Creates an instance of UserActivityHook object and installs both or one of mouse and/or keyboard hooks and starts rasing events + /// + /// true if mouse events must be monitored + /// true if keyboard events must be monitored + /// Any windows problem. + /// + /// To create an instance without installing hooks call new UserActivityHook(false, false) + /// + public UserActivityHook(bool InstallMouseHook, bool InstallKeyboardHook) + { + Start(InstallMouseHook, InstallKeyboardHook); + } + + /// + /// Destruction. + /// + ~UserActivityHook() + { + //uninstall hooks and do not throw exceptions + Stop(true, true, false); + } + + /// + /// Occurs when the user moves the mouse, presses any mouse button or scrolls the wheel + /// + /// + public bool EnableMouse = false; + + + public event MouseEventHandler OnMouseLeftDown; + + public event MouseEventHandler OnMouseLeftUp; + + public event MouseEventHandler OnMouseWheelDown; + + public event MouseEventHandler OnMouseWheelUp; + + public bool MouseLeftDownEnable() + { + return OnMouseLeftDown != null; + } + public bool MouseLeftUpEnable() + { + return OnMouseLeftUp != null; + } + public bool MouseWheelDownEnable() + { + return OnMouseWheelDown != null; + } + public bool MouseWheelUpEnable() + { + return OnMouseWheelUp != null; + } + /// + /// Occurs when the user presses a key + /// + public event KeyEventHandler KeyDown; + /// + /// Occurs when the user presses and releases + /// + public event KeyPressEventHandler KeyPress; + /// + /// Occurs when the user releases a key + /// + public event KeyEventHandler KeyUp; + + + /// + /// Stores the handle to the mouse hook procedure. + /// + private int hMouseHook = 0; + /// + /// Stores the handle to the keyboard hook procedure. + /// + private int hKeyboardHook = 0; + + + /// + /// Declare MouseHookProcedure as HookProc type. + /// + private static HookProc MouseHookProcedure; + /// + /// Declare KeyboardHookProcedure as HookProc type. + /// + private static HookProc KeyboardHookProcedure; + + + /// + /// Installs both mouse and keyboard hooks and starts rasing events + /// + /// Any windows problem. + public void Start() + { + this.Start(true, true); + } + + /// + /// Installs both or one of mouse and/or keyboard hooks and starts rasing events + /// + /// true if mouse events must be monitored + /// true if keyboard events must be monitored + /// Any windows problem. + public void Start(bool InstallMouseHook, bool InstallKeyboardHook) + { + this.EnableMouse = InstallMouseHook; + // install Mouse hook only if it is not installed and must be installed + if (hMouseHook == 0 && EnableMouse) + { + // Create an instance of HookProc. + MouseHookProcedure = new HookProc(MouseHookProc); + //install hook + hMouseHook = SetWindowsHookEx( + WH_MOUSE_LL, + MouseHookProcedure, + Marshal.GetHINSTANCE( + Assembly.GetExecutingAssembly().GetModules()[0]), + 0); + //If SetWindowsHookEx fails. + if (hMouseHook == 0) + { + //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. + int errorCode = Marshal.GetLastWin32Error(); + //do cleanup + Stop(true, false, false); + //Initializes and throws a new instance of the Win32Exception class with the specified error. + throw new Win32Exception(errorCode); + } + } + + // install Keyboard hook only if it is not installed and must be installed + if (hKeyboardHook == 0 && InstallKeyboardHook) + { + // Create an instance of HookProc. + KeyboardHookProcedure = new HookProc(KeyboardHookProc); + //install hook + hKeyboardHook = SetWindowsHookEx( + WH_KEYBOARD_LL, + KeyboardHookProcedure, + Marshal.GetHINSTANCE( + Assembly.GetExecutingAssembly().GetModules()[0]), + 0); + //If SetWindowsHookEx fails. + if (hKeyboardHook == 0) + { + //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. + int errorCode = Marshal.GetLastWin32Error(); + //do cleanup + Stop(false, true, false); + //Initializes and throws a new instance of the Win32Exception class with the specified error. + throw new Win32Exception(errorCode); + } + } + } + + /// + /// Stops monitoring both mouse and keyboard events and rasing events. + /// + /// Any windows problem. + public void Stop() + { + this.Stop(true, true, true); + } + + /// + /// Stops monitoring both or one of mouse and/or keyboard events and rasing events. + /// + /// true if mouse hook must be uninstalled + /// true if keyboard hook must be uninstalled + /// true if exceptions which occured during uninstalling must be thrown + /// Any windows problem. + public void Stop(bool UninstallMouseHook, bool UninstallKeyboardHook, bool ThrowExceptions) + { + //if mouse hook set and must be uninstalled + if (hMouseHook != 0 && UninstallMouseHook) + { + //uninstall hook + int retMouse = UnhookWindowsHookEx(hMouseHook); + //reset invalid handle + hMouseHook = 0; + //if failed and exception must be thrown + if (retMouse == 0 && ThrowExceptions) + { + //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. + int errorCode = Marshal.GetLastWin32Error(); + //Initializes and throws a new instance of the Win32Exception class with the specified error. + throw new Win32Exception(errorCode); + } + } + + //if keyboard hook set and must be uninstalled + if (hKeyboardHook != 0 && UninstallKeyboardHook) + { + //uninstall hook + int retKeyboard = UnhookWindowsHookEx(hKeyboardHook); + //reset invalid handle + hKeyboardHook = 0; + //if failed and exception must be thrown + if (retKeyboard == 0 && ThrowExceptions) + { + //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. + int errorCode = Marshal.GetLastWin32Error(); + //Initializes and throws a new instance of the Win32Exception class with the specified error. + throw new Win32Exception(errorCode); + } + } + } + + + /// + /// A callback function which will be called every time a mouse activity detected. + /// + /// + /// [in] Specifies whether the hook procedure must process the message. + /// If nCode is HC_ACTION, the hook procedure must process the message. + /// If nCode is less than zero, the hook procedure must pass the message to the + /// CallNextHookEx function without further processing and must return the + /// value returned by CallNextHookEx. + /// + /// + /// [in] Specifies whether the message was sent by the current thread. + /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. + /// + /// + /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. + /// + /// + /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. + /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx + /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC + /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook + /// procedure does not call CallNextHookEx, the return value should be zero. + /// + private int MouseHookProc(int nCode, int wParam, IntPtr lParam) + { + // if ok and someone listens to our events + if ((nCode >= 0) && EnableMouse) + { + //Marshall the data from callback. + MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct)); + + //detect button clicked + MouseButtons button = MouseButtons.None; + short mouseDelta = 0; + switch (wParam) + { + case WM_LBUTTONDOWN: + case WM_LBUTTONUP: + button = MouseButtons.Left; + + break; + case WM_RBUTTONDOWN: + case WM_RBUTTONUP: + //case WM_RBUTTONUP: + //case WM_RBUTTONDBLCLK: + button = MouseButtons.Right; + break; + case WM_MOUSEWHEEL: + case WM_MBUTTONDOWN: + case WM_MBUTTONUP: + button = MouseButtons.Middle; + //If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta. + //One wheel click is defined as WHEEL_DELTA, which is 120. + //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value + mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff); + + //TODO: X BUTTONS (I havent them so was unable to test) + //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, + //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, + //and the low-order word is reserved. This value can be one or more of the following values. + //Otherwise, mouseData is not used. + break; + } + + //double clicks + int clickCount = 0; + if (button != MouseButtons.None) + if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2; + else clickCount = 1; + + //generate event + MouseEventArgs e = new MouseEventArgs( + button, + clickCount, + mouseHookStruct.pt.x, + mouseHookStruct.pt.y, + mouseDelta); + //raise it + switch (wParam) + { + case WM_LBUTTONDOWN: + this.OnMouseLeftDown?.Invoke(this, e); + break; + case WM_LBUTTONUP: + this.OnMouseLeftUp?.Invoke(this, e); + break; + case WM_RBUTTONDOWN: + break; + case WM_RBUTTONUP: + break; + case WM_MOUSEWHEEL: + break; + case WM_MBUTTONDOWN: + this.OnMouseWheelDown?.Invoke(this, e); + break; + case WM_MBUTTONUP: + this.OnMouseWheelUp?.Invoke(this, e); + break; + } + } + //call next hook + return CallNextHookEx(hMouseHook, nCode, wParam, lParam); + } + + /// + /// A callback function which will be called every time a keyboard activity detected. + /// + /// + /// [in] Specifies whether the hook procedure must process the message. + /// If nCode is HC_ACTION, the hook procedure must process the message. + /// If nCode is less than zero, the hook procedure must pass the message to the + /// CallNextHookEx function without further processing and must return the + /// value returned by CallNextHookEx. + /// + /// + /// [in] Specifies whether the message was sent by the current thread. + /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. + /// + /// + /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. + /// + /// + /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. + /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx + /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC + /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook + /// procedure does not call CallNextHookEx, the return value should be zero. + /// + private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) + { + //indicates if any of underlaing events set e.Handled flag + bool handled = false; + //it was ok and someone listens to events + if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null)) + { + //read structure KeyboardHookStruct at lParam + KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); + //raise KeyDown + if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) + { + Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; + KeyEventArgs e = new KeyEventArgs(keyData); + KeyDown(this, e); + handled = handled || e.Handled; + } + + // raise KeyPress + if (KeyPress != null && wParam == WM_KEYDOWN) + { + bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false); + bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false); + + byte[] keyState = new byte[256]; + GetKeyboardState(keyState); + byte[] inBuffer = new byte[2]; + if (ToAscii(MyKeyboardHookStruct.vkCode, + MyKeyboardHookStruct.scanCode, + keyState, + inBuffer, + MyKeyboardHookStruct.flags) == 1) + { + char key = (char)inBuffer[0]; + if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key); + KeyPressEventArgs e = new KeyPressEventArgs(key); + KeyPress(this, e); + handled = handled || e.Handled; + } + } + + // raise KeyUp + if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) + { + Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; + KeyEventArgs e = new KeyEventArgs(keyData); + KeyUp(this, e); + handled = handled || e.Handled; + } + + } + + //if event handled in application do not handoff to other listeners + if (handled) + return 1; + else + return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); + } + } +} diff --git a/app.manifest b/app.manifest new file mode 100644 index 0000000..e9f4372 --- /dev/null +++ b/app.manifest @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PerMonitorV2 + True/PM + + + + + + + + diff --git a/packages.config b/packages.config index f7b62a9..27df0b1 100644 --- a/packages.config +++ b/packages.config @@ -2,7 +2,6 @@ -