Skip to content

Commit

Permalink
Merge pull request #82 from hoywu/master
Browse files Browse the repository at this point in the history
尝试优化高DPI下显示质量
  • Loading branch information
jitwxs committed May 2, 2022
2 parents 8230be4 + 2f5b037 commit 5ff15c1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
9 changes: 6 additions & 3 deletions MusicLyricApp/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>
</configuration>
35 changes: 35 additions & 0 deletions MusicLyricApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,8 @@ public MainForm()

InitializeComponent();
InitialConfig();

TrySetHighDPIFont("Segoe UI");
}

private void InitialConfig()
Expand Down Expand Up @@ -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) { }
}
}
}

/// <summary>
/// 读取搜索框并重新加载配置
/// </summary>
Expand Down

0 comments on commit 5ff15c1

Please sign in to comment.