Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

尝试优化高DPI下显示质量 #82

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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