diff --git a/MusicLyricApp/App.config b/MusicLyricApp/App.config index d1428ad..27f151b 100644 --- a/MusicLyricApp/App.config +++ b/MusicLyricApp/App.config @@ -1,6 +1,9 @@ - - - + + + + + + diff --git a/MusicLyricApp/MainForm.cs b/MusicLyricApp/MainForm.cs index 00d00d6..d0e300d 100644 --- a/MusicLyricApp/MainForm.cs +++ b/MusicLyricApp/MainForm.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Reflection; using System.Text; using System.Threading; using System.Windows.Forms; @@ -68,6 +69,8 @@ public MainForm() InitializeComponent(); InitialConfig(); + + TrySetHighDPIFont("Segoe UI"); } private void InitialConfig() @@ -101,6 +104,38 @@ private void InitialConfig() } } + private void TrySetHighDPIFont(String fontName) + { + //缩放比例大于100%才更改字体 + if (DeviceDpi <= 96) return; + + Font font = null; + try + { + font = new Font(fontName, 9F, FontStyle.Regular, GraphicsUnit.Point); + } + catch (System.Exception) { } + + if (font == null || !fontName.Equals(font.Name)) return; + + Type type = this.GetType(); + FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); + foreach (FieldInfo fieldInfo in fieldInfos) + { + Type fieldType = fieldInfo.FieldType; + if ("System.Windows.Forms".Equals(fieldType.Namespace)) + { + try + { + PropertyInfo propertyInfo = fieldType.GetProperty("Font"); + Object obj = fieldInfo.GetValue(this); + propertyInfo.SetValue(obj, font); + } + catch (System.Exception) { } + } + } + } + /// /// 读取搜索框并重新加载配置 ///