From d8c2a7f7965bd8d748a9cecf894abaece0bc1db2 Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Sun, 29 Apr 2018 11:02:13 -0400 Subject: [PATCH 1/3] Use system hand cursor Universal fix, will fix any affected Form Fixes #3341 --- ShareX.HelpersLib/Native/NativeConstants.cs | 1 + ShareX.HelpersLib/Native/NativeMethods.cs | 3 +++ ShareX/Program.cs | 17 +++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/ShareX.HelpersLib/Native/NativeConstants.cs b/ShareX.HelpersLib/Native/NativeConstants.cs index c54425eb2..3de69c04a 100644 --- a/ShareX.HelpersLib/Native/NativeConstants.cs +++ b/ShareX.HelpersLib/Native/NativeConstants.cs @@ -82,6 +82,7 @@ public static class NativeConstants public const uint ECM_FIRST = 0x1500; public const uint EM_SETCUEBANNER = ECM_FIRST + 1; + public const int IDC_HAND = 32649; public const uint MA_ACTIVATE = 1; public const uint MA_ACTIVATEANDEAT = 2; public const uint MA_NOACTIVATE = 3; diff --git a/ShareX.HelpersLib/Native/NativeMethods.cs b/ShareX.HelpersLib/Native/NativeMethods.cs index 5723fc581..91065e23c 100644 --- a/ShareX.HelpersLib/Native/NativeMethods.cs +++ b/ShareX.HelpersLib/Native/NativeMethods.cs @@ -193,6 +193,9 @@ public static partial class NativeMethods [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsZoomed(IntPtr hWnd); + [DllImport("user32.dll")] + public static extern IntPtr LoadCursor(IntPtr hInstance, int iconId); + [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); diff --git a/ShareX/Program.cs b/ShareX/Program.cs index 0204e3c79..9daeb1ffa 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -30,6 +30,7 @@ You should have received a copy of the GNU General Public License using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Reflection; using System.Text; using System.Threading; using System.Windows.Forms; @@ -226,14 +227,23 @@ public static string ScreenshotsFolder [STAThread] private static void Main(string[] args) { +#if !DEBUG // Allow Visual Studio to break on exceptions in Debug builds. Application.ThreadException += Application_ThreadException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; +#endif StartTimer = Stopwatch.StartNew(); // For be able to show startup time CLI = new CLIManager(args); CLI.ParseCommands(); + // Fix the hand cursor before we open any form. + try + { + FixHandCursor(); + } + catch (Exception) { } // If it fails, we'll just have to live with the old hand. + #if STEAM if (CheckUninstall()) return; // Steam will run ShareX with -Uninstall when uninstalling #endif @@ -580,5 +590,12 @@ private static void CleanTempFiles() } }).Start(); } + + private static void FixHandCursor() + { + // https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Cursors.cs,423 + typeof(Cursors).GetField("hand", BindingFlags.NonPublic | BindingFlags.Static) + ?.SetValue(null, new Cursor(NativeMethods.LoadCursor(IntPtr.Zero, NativeConstants.IDC_HAND))); + } } } \ No newline at end of file From 82da3e353b1abb1230a9580be2533b5171411912 Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Sun, 29 Apr 2018 11:11:44 -0400 Subject: [PATCH 2/3] Change the hand cursor only when we actually open a form --- ShareX/Program.cs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/ShareX/Program.cs b/ShareX/Program.cs index 9daeb1ffa..6831feaf1 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -237,13 +237,6 @@ private static void Main(string[] args) CLI = new CLIManager(args); CLI.ParseCommands(); - // Fix the hand cursor before we open any form. - try - { - FixHandCursor(); - } - catch (Exception) { } // If it fails, we'll just have to live with the old hand. - #if STEAM if (CheckUninstall()) return; // Steam will run ShareX with -Uninstall when uninstalling #endif @@ -298,6 +291,7 @@ private static void Run() LanguageHelper.ChangeLanguage(Settings.Language); + TryFixHandCursor(); DebugHelper.WriteLine("MainForm init started."); MainForm = new MainForm(); DebugHelper.WriteLine("MainForm init finished."); @@ -510,6 +504,7 @@ private static bool CheckAdminTasks() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); + TryFixHandCursor(); Application.Run(new DNSChangerForm()); return true; } @@ -591,11 +586,15 @@ private static void CleanTempFiles() }).Start(); } - private static void FixHandCursor() + private static void TryFixHandCursor() { - // https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Cursors.cs,423 - typeof(Cursors).GetField("hand", BindingFlags.NonPublic | BindingFlags.Static) - ?.SetValue(null, new Cursor(NativeMethods.LoadCursor(IntPtr.Zero, NativeConstants.IDC_HAND))); + try + { + // https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Cursors.cs,423 + typeof(Cursors).GetField("hand", BindingFlags.NonPublic | BindingFlags.Static) + ?.SetValue(null, new Cursor(NativeMethods.LoadCursor(IntPtr.Zero, NativeConstants.IDC_HAND))); + } + catch { } // If it fails, we'll just have to live with the old hand. } } } \ No newline at end of file From d50d3ca7b804fa8ed692a4f3ad3ffc7cbc66e4a2 Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Sun, 29 Apr 2018 11:30:03 -0400 Subject: [PATCH 3/3] Move TryFixHandCursor to HelpersLib --- ShareX.HelpersLib/Helpers/Helpers.cs | 17 +++++++++++++++++ ShareX/Program.cs | 16 ++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index 81b56d02a..64a205c51 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -1267,5 +1267,22 @@ public static string NumberToLetters(int num) } return result; } + + public static bool TryFixHandCursor() + { + try + { + // https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Cursors.cs,423 + typeof(Cursors).GetField("hand", BindingFlags.NonPublic | BindingFlags.Static) + ?.SetValue(null, new Cursor(NativeMethods.LoadCursor(IntPtr.Zero, NativeConstants.IDC_HAND))); + + return true; + } + catch + { + // If it fails, we'll just have to live with the old hand. + return false; + } + } } } \ No newline at end of file diff --git a/ShareX/Program.cs b/ShareX/Program.cs index 6831feaf1..04bdb1843 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -30,7 +30,6 @@ You should have received a copy of the GNU General Public License using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Reflection; using System.Text; using System.Threading; using System.Windows.Forms; @@ -291,7 +290,7 @@ private static void Run() LanguageHelper.ChangeLanguage(Settings.Language); - TryFixHandCursor(); + Helpers.TryFixHandCursor(); DebugHelper.WriteLine("MainForm init started."); MainForm = new MainForm(); DebugHelper.WriteLine("MainForm init finished."); @@ -504,7 +503,7 @@ private static bool CheckAdminTasks() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - TryFixHandCursor(); + Helpers.TryFixHandCursor(); Application.Run(new DNSChangerForm()); return true; } @@ -585,16 +584,5 @@ private static void CleanTempFiles() } }).Start(); } - - private static void TryFixHandCursor() - { - try - { - // https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Cursors.cs,423 - typeof(Cursors).GetField("hand", BindingFlags.NonPublic | BindingFlags.Static) - ?.SetValue(null, new Cursor(NativeMethods.LoadCursor(IntPtr.Zero, NativeConstants.IDC_HAND))); - } - catch { } // If it fails, we'll just have to live with the old hand. - } } } \ No newline at end of file