static void Main(string[] args) { // Make sure that settings aren't corrupted, and fix them. try { string uiLanguage = Settings.Default.UILanguage; } catch (ConfigurationErrorsException ex) { //(requires System.Configuration) // Once the configuration system is corrupt, there doesn't appear a way to // fix it (Settings.Default.Reload() doesn't work, even though you would // think it would. So restarting the application appears to be the best way. // We inform the user in case deleting doesn't work they can try to delete the file // themselves. This is so rare it isn't worth localizing the message. string filename = ((ConfigurationErrorsException)ex.InnerException).Filename; MessageBox.Show(string.Format("The configuration file '{0}' is corrupted. Purple Pen will delete this file and restart.", filename), "Corrupt Configuration File", MessageBoxButtons.OK, MessageBoxIcon.Error); File.Delete(filename); System.Diagnostics.Process.Start(Application.ExecutablePath); // start new instance of application return; // exit current instance of application. } // Enable crash reporting. Application.ThreadException += (sender, e) => SendCrashReport(e.Exception); AppDomain.CurrentDomain.UnhandledException += (sender, e) => { SendCrashReport((Exception)e.ExceptionObject); Environment.Exit(0); }; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); InitUILanguage(); InitClientId(); FontDesc.InitializeFonts(); if (args.Length > 0 && LoadCommandLineFile(args[0])) { // We successfully loaded a file from the command line. // Nothing more to do here. } else { // No command line args. Show initial screen to load/create an event. new InitialScreen().Show(); } Application.Run(); }
private void DrawSingleLineText(IGraphicsTarget g, string text, FontDesc fontDesc, PointF pt, StringAlignment horizAlignment, StringAlignment vertAlignment) { object font = new object(); g.CreateFont(font, fontDesc.Name, fontDesc.EmHeight, fontDesc.TextEffects); ITextFaceMetrics fontMetrics = textMetrics.GetTextFaceMetrics(fontDesc.Name, fontDesc.EmHeight, fontDesc.TextEffects); SizeF size = fontMetrics.GetTextSize(text); switch (horizAlignment) { case StringAlignment.Near: break; case StringAlignment.Center: pt.X = pt.X - size.Width / 2F; break; case StringAlignment.Far: pt.X = pt.X - size.Width; break; } switch (vertAlignment) { case StringAlignment.Near: break; case StringAlignment.Center: pt.Y = pt.Y - size.Height / 2F; break; case StringAlignment.Far: pt.Y = pt.Y - size.Height; break; } g.DrawText(text, font, blackBrush, pt); fontMetrics.Dispose(); }
internal static SizeF GetTextSize(string text, FontDesc font, float fontScaling) { Graphics g = Util.GetHiresGraphics(); using (Font f = font.GetScaledFont(fontScaling)) { SizeF size = g.MeasureString(text, f, new PointF(0, 0), StringFormat.GenericTypographic); // We really want the size of just the digits/capital letters. So, reduce by the descender size from // bottom and top (no way to get offset from top of box to top of cap letters). FontFamily family = f.FontFamily; float descender = family.GetCellDescent(f.Style) * font.EmHeight * fontScaling / family.GetEmHeight(f.Style); size.Height = size.Height - 2 * descender; return size; } }
internal static PointF GetTextLocation(PointF controlLocation, float distanceFromCenter, string text, FontDesc font, float fontScaling, IEnumerable<CourseObj> list) { const double deltaAngle = Math.PI / 16; // angle to increase by each time when testing an angle. // Get a list of all nearby objects that we want to stay away from. List<CourseObj> nearbyObjects = GetNearbyObjects(list, controlLocation, distanceFromCenter * 4); // Get the size of the text. SizeF textSize = GetTextSize(text, font, fontScaling); // Try 32 different locations for the number, finding which angle has the largest distance from nearby objects. // Start at the default angle, so if all angles are equally good that is the one we pick. PointF bestPoint = new PointF(); double bestDistance = -1; for (double angle = NormalCourseAppearance.defaultControlNumberAngle; angle < NormalCourseAppearance.defaultControlNumberAngle + 2 * Math.PI; angle += deltaAngle) { PointF pt = GetRectangleCenter(controlLocation, distanceFromCenter, angle, textSize); double distanceFromNearby = GetMinDistanceFromNearby(pt, nearbyObjects); if (distanceFromNearby > bestDistance) { bestPoint = pt; bestDistance = distanceFromNearby; } } return bestPoint; }