FontHelper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Text;
  6. using System.Linq;
  7. namespace Optimizer
  8. {
  9. internal static class FontHelper
  10. {
  11. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  12. private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
  13. IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
  14. static PrivateFontCollection fonts = new PrivateFontCollection();
  15. internal static Font Poppins15;
  16. internal static void LoadFont()
  17. {
  18. byte[] fontData = Properties.Resources.Poppins_Regular;
  19. IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
  20. System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
  21. uint dummy = 0;
  22. fonts.AddMemoryFont(fontPtr, Properties.Resources.Poppins_Regular.Length);
  23. AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.Poppins_Regular.Length, IntPtr.Zero, ref dummy);
  24. System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
  25. Poppins15 = new Font(fonts.Families[0], 13f);
  26. }
  27. internal static IEnumerable<string> GetAvailableFonts()
  28. {
  29. return new InstalledFontCollection().Families.Select(x => x.Name);
  30. }
  31. internal static void ChangeGlobalFont(string fontName)
  32. {
  33. try
  34. {
  35. using (RegistryKey fontsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", true))
  36. {
  37. fontsKey.SetValue("Segoe UI (TrueType)", "");
  38. fontsKey.SetValue("Segoe UI Bold (TrueType)", "");
  39. fontsKey.SetValue("Segoe UI Bold Italic (TrueType)", "");
  40. fontsKey.SetValue("Segoe UI Italic (TrueType)", "");
  41. fontsKey.SetValue("Segoe UI Light (TrueType)", "");
  42. fontsKey.SetValue("Segoe UI Semibold (TrueType)", "");
  43. fontsKey.SetValue("Segoe UI Symbol (TrueType)", "");
  44. }
  45. using (RegistryKey fontSubstitutesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes", true))
  46. {
  47. fontSubstitutesKey.SetValue("Segoe UI", fontName);
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. ErrorLogger.LogError("FontHelper.ChangeGlobalFont", ex.Message, ex.StackTrace);
  53. }
  54. }
  55. internal static void RestoreDefaultGlobalFont()
  56. {
  57. try
  58. {
  59. using (RegistryKey fontsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", true))
  60. {
  61. fontsKey.SetValue("Segoe UI (TrueType)", "segoeui.ttf");
  62. fontsKey.SetValue("Segoe UI Black (TrueType)", "seguibl.ttf");
  63. fontsKey.SetValue("Segoe UI Black Italic (TrueType)", "seguibli.ttf");
  64. fontsKey.SetValue("Segoe UI Bold (TrueType)", "segoeuib.ttf");
  65. fontsKey.SetValue("Segoe UI Bold Italic (TrueType)", "segoeuiz.ttf");
  66. fontsKey.SetValue("Segoe UI Emoji (TrueType)", "seguiemj.ttf");
  67. fontsKey.SetValue("Segoe UI Historic (TrueType)", "seguihis.ttf");
  68. fontsKey.SetValue("Segoe UI Italic (TrueType)", "segoeuii.ttf");
  69. fontsKey.SetValue("Segoe UI Light (TrueType)", "segoeuil.ttf");
  70. fontsKey.SetValue("Segoe UI Light Italic (TrueType)", "seguili.ttf");
  71. fontsKey.SetValue("Segoe UI Semibold (TrueType)", "seguisb.ttf");
  72. fontsKey.SetValue("Segoe UI Semibold Italic (TrueType)", "seguisbi.ttf");
  73. fontsKey.SetValue("Segoe UI Semilight (TrueType)", "segoeuisl.ttf");
  74. fontsKey.SetValue("Segoe UI Semilight Italic (TrueType)", "seguisli.ttf");
  75. fontsKey.SetValue("Segoe UI Symbol (TrueType)", "seguisym.ttf");
  76. fontsKey.SetValue("Segoe MDL2 Assets (TrueType)", "segmdl2.ttf");
  77. fontsKey.SetValue("Segoe Print (TrueType)", "segoepr.ttf");
  78. fontsKey.SetValue("Segoe Print Bold (TrueType)", "segoeprb.ttf");
  79. fontsKey.SetValue("Segoe Script (TrueType)", "segoesc.ttf");
  80. fontsKey.SetValue("Segoe Script Bold (TrueType)", "segoescb.ttf");
  81. }
  82. using (RegistryKey fontSubstitutesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes", true))
  83. {
  84. fontSubstitutesKey.DeleteValue("Segoe UI", false);
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. ErrorLogger.LogError("FontHelper.RestoreDefaultGlobalFont", ex.Message, ex.StackTrace);
  90. }
  91. }
  92. }
  93. }