From 36d9f932a7433908f46055dc50c4ff85055a7873 Mon Sep 17 00:00:00 2001 From: geovens Date: Wed, 20 Jul 2016 14:31:26 +0800 Subject: [PATCH] add log of exceptions to help tracking the rare crash. fix incorrect snapshot area when dpi setting is not 100%. --- .gitignore | 1 + changelog.txt | 7 ++- src/FormDisplay.cs | 12 +++++ src/Program.cs | 89 ++++++++++++++++++++++++++++++++++ src/Properties/AssemblyInfo.cs | 4 +- 5 files changed, 110 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 379789b..e013d26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ build release +bin/crash.txt *.pdb *.exp diff --git a/changelog.txt b/changelog.txt index b8bba38..457783a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,9 @@ +v0.3.4 (ongoing) +================= +- Log unhandled exceptions to a file to help tracking rare crashes. +- Fix incorrect snapshot area when the DPI setting is not 100%. (not fully tested) + v0.3.3 (Jul. 10, 2016) ================= - Change system tray icon size. @@ -6,7 +11,7 @@ v0.3.3 (Jul. 10, 2016) v0.3.2 (Feb. 1, 2016) ================= -- Click throung mode has a dedicated button now. +- Click through mode has a dedicated button now. - Add two new pens, to a total of five. - Add an entry to pen settings. diff --git a/src/FormDisplay.cs b/src/FormDisplay.cs index b64c8bc..52df269 100644 --- a/src/FormDisplay.cs +++ b/src/FormDisplay.cs @@ -223,6 +223,18 @@ public void SnapShot(Rectangle rect) //tempbmp.Dispose(); IntPtr screenDc = GetDC(IntPtr.Zero); + + const int VERTRES = 10; + const int DESKTOPVERTRES = 117; + int LogicalScreenHeight = GetDeviceCaps(screenDc, VERTRES); + int PhysicalScreenHeight = GetDeviceCaps(screenDc, DESKTOPVERTRES); + float ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight; + + rect.X = (int)(rect.X * ScreenScalingFactor); + rect.Y = (int)(rect.Y * ScreenScalingFactor); + rect.Width = (int)(rect.Width * ScreenScalingFactor); + rect.Height = (int)(rect.Height * ScreenScalingFactor); + IntPtr hDest = CreateCompatibleDC(screenDc); Bitmap tempbmp = new Bitmap(rect.Width, rect.Height); IntPtr hBmp = tempbmp.GetHbitmap(); diff --git a/src/Program.cs b/src/Program.cs index c076c1b..8457021 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.Windows.Forms; +using System.Threading; +using System.Diagnostics; +using System.IO; namespace gInk { @@ -12,11 +15,97 @@ static class Program [STAThread] static void Main() { + Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException); + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException); + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); new Root(); Application.Run(); } + + private static void UIThreadException(object sender, ThreadExceptionEventArgs t) + { + DialogResult result = DialogResult.Cancel; + try + { + Exception ex = (Exception)t.Exception; + + string errorMsg = "UIThreadException\r\n\r\n"; + errorMsg += "Oops, gInk crashed! Please include the following information if you plan to contact the developers (a copy of the following information is stored in crash.txt in the application folder):\r\n\r\n"; + errorMsg += ex.Message + "\r\n\r\n"; + errorMsg += "Stack Trace:\r\n" + ex.StackTrace + "\r\n\r\n"; + WriteErrorLog(errorMsg); + + errorMsg += "!!! PLEASE PRESS ESC KEY TO EXIT IF YOU FEEL YOUR MOUSE CLICK IS BLOCKED BY SOMETHING"; + ShowErrorDialog("UIThreadException", errorMsg); + } + catch + { + try + { + MessageBox.Show("Fatal Windows Forms Error", "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); + } + finally + { + Application.Exit(); + } + } + + // Exits the program when the user clicks Abort. + if (result == DialogResult.Abort) + Application.Exit(); + } + + private static void UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + try + { + Exception ex = (Exception)e.ExceptionObject; + + string errorMsg = "UnhandledException\r\n\r\n"; + errorMsg += "Oops, gInk crashed! Please include the following information if you plan to contact the developers:\r\n\r\n"; + errorMsg += ex.Message + "\r\n\r\n"; + errorMsg += "Stack Trace:\r\n" + ex.StackTrace + "\r\n\r\n"; + WriteErrorLog(errorMsg); + + ShowErrorDialog("UnhandledException", errorMsg); + + if (!EventLog.SourceExists("UnhandledException")) + { + EventLog.CreateEventSource("UnhandledException", "Application"); + } + EventLog myLog = new EventLog(); + myLog.Source = "UnhandledException"; + myLog.WriteEntry(errorMsg); + } + catch (Exception exc) + { + try + { + MessageBox.Show("Fatal Non-UI Error", "Fatal Non-UI Error. Could not write the error to the event log. Reason: " + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop); + } + finally + { + Application.Exit(); + } + } + } + + private static DialogResult ShowErrorDialog(string title, string errormsg) + { + return MessageBox.Show(errormsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); + } + + private static void WriteErrorLog(string errormsg) + { + FileStream fs = new FileStream("crash.txt", FileMode.Create); + StreamWriter sw = new StreamWriter(fs); + sw.Write(errormsg); + sw.Close(); + fs.Close(); + } } } diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index 3d0bdeb..77dc5c3 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.3.3.0")] -[assembly: AssemblyFileVersion("0.3.3.0")] +[assembly: AssemblyVersion("0.3.4.0")] +[assembly: AssemblyFileVersion("0.3.4.0")]