FontHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Logger.LogError("FontHelper.ChangeGlobalFont", ex.Message, ex.StackTrace);
  53. }
  54. }
  55. internal static string GetCurrentGlobalFont()
  56. {
  57. try
  58. {
  59. using (RegistryKey fontSubstitutesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes", false))
  60. {
  61. return fontSubstitutesKey.GetValue("Segoe UI", string.Empty) as string;
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. Logger.LogError("FontHelper.GetCurrentGlobalFont", ex.Message, ex.StackTrace);
  67. return string.Empty;
  68. }
  69. }
  70. internal static void RestoreDefaultGlobalFont()
  71. {
  72. try
  73. {
  74. using (RegistryKey fontsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", true))
  75. {
  76. fontsKey.SetValue("Segoe UI (TrueType)", "segoeui.ttf");
  77. fontsKey.SetValue("Segoe UI Black (TrueType)", "seguibl.ttf");
  78. fontsKey.SetValue("Segoe UI Black Italic (TrueType)", "seguibli.ttf");
  79. fontsKey.SetValue("Segoe UI Bold (TrueType)", "segoeuib.ttf");
  80. fontsKey.SetValue("Segoe UI Bold Italic (TrueType)", "segoeuiz.ttf");
  81. fontsKey.SetValue("Segoe UI Emoji (TrueType)", "seguiemj.ttf");
  82. fontsKey.SetValue("Segoe UI Historic (TrueType)", "seguihis.ttf");
  83. fontsKey.SetValue("Segoe UI Italic (TrueType)", "segoeuii.ttf");
  84. fontsKey.SetValue("Segoe UI Light (TrueType)", "segoeuil.ttf");
  85. fontsKey.SetValue("Segoe UI Light Italic (TrueType)", "seguili.ttf");
  86. fontsKey.SetValue("Segoe UI Semibold (TrueType)", "seguisb.ttf");
  87. fontsKey.SetValue("Segoe UI Semibold Italic (TrueType)", "seguisbi.ttf");
  88. fontsKey.SetValue("Segoe UI Semilight (TrueType)", "segoeuisl.ttf");
  89. fontsKey.SetValue("Segoe UI Semilight Italic (TrueType)", "seguisli.ttf");
  90. fontsKey.SetValue("Segoe UI Symbol (TrueType)", "seguisym.ttf");
  91. fontsKey.SetValue("Segoe MDL2 Assets (TrueType)", "segmdl2.ttf");
  92. fontsKey.SetValue("Segoe Print (TrueType)", "segoepr.ttf");
  93. fontsKey.SetValue("Segoe Print Bold (TrueType)", "segoeprb.ttf");
  94. fontsKey.SetValue("Segoe Script (TrueType)", "segoesc.ttf");
  95. fontsKey.SetValue("Segoe Script Bold (TrueType)", "segoescb.ttf");
  96. }
  97. using (RegistryKey fontSubstitutesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes", true))
  98. {
  99. fontSubstitutesKey.DeleteValue("Segoe UI", false);
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. Logger.LogError("FontHelper.RestoreDefaultGlobalFont", ex.Message, ex.StackTrace);
  105. }
  106. }
  107. }
  108. }