Skip to content

Commit

Permalink
修复字体缩放时风格消失问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hlz2516 committed Nov 25, 2023
1 parent 126c873 commit da1f4fb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
5 changes: 3 additions & 2 deletions AutoScaleHelper/AutoScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void UpdateControlsLayout(bool recursive = true)
int fontHeight = (int)(ctrlInfo.Font.Height * 1.0f / ctrlInfo.Rect.Height * curCtrl.Height);
FontInfo fontInfo = FontInfos.GetFontInfo(ctrlInfo.Font.Name);
float fontSize = fontInfo.GetFloorFontSizeByHeight(fontHeight, ctrlInfo.FontSizeType);
curCtrl.Font = new Font(ctrlInfo.Font.Name, fontSize);
curCtrl.Font = new Font(ctrlInfo.Font.Name, fontSize,ctrlInfo.Font.Style);
}
}

Expand All @@ -323,7 +323,8 @@ public void UpdateControlsLayout(bool recursive = true)
{
foreach (var item in g.Value)
{
item.Font = g.Key.Font.Clone() as Font;
//item.Font = g.Key.Font.Clone() as Font;
item.Font = new Font(g.Key.Font.FontFamily, g.Key.Font.Size, item.Font.Style);
}
}

Expand Down
7 changes: 4 additions & 3 deletions AutoScaleHelper/TextScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public void ScaleSingle(Control ctrl,ScaleMode scaleMode = ScaleMode.MaintainSel
int fontHeight = (int)(ctrlInfo.Font.Height * 1.0f / ctrlInfo.Rect.Height * ctrl.Height);
FontInfo fontInfo = FontInfos.GetFontInfo(ctrlInfo.Font.Name);
float fontSize = fontInfo.GetFloorFontSizeByHeight(fontHeight, ctrlInfo.FontSizeType);
ctrl.Font = new Font(ctrlInfo.Font.Name, fontSize);
ctrl.Font = new Font(ctrlInfo.Font.Name, fontSize,ctrl.Font.Style);
}
else if (scaleMode == ScaleMode.MaintainSelfRatioH)
{
//将设计器中的控件宽度缩放比例作为字体的行高缩放比例
float rate = ctrl.Width * 1.0f / ctrlInfo.Rect.Width;
FontInfo fontInfo = FontInfos.GetFontInfo(ctrlInfo.Font.Name);
float fontSize = fontInfo.GetFloorFontSizeByHeight((int)(rate * ctrlInfo.Font.Height), ctrlInfo.FontSizeType);
ctrl.Font = new Font(ctrlInfo.Font.Name, fontSize);
ctrl.Font = new Font(ctrlInfo.Font.Name, fontSize,ctrl.Font.Style);
}

//针对设置了字体依赖的控件特殊处理
Expand All @@ -131,7 +131,8 @@ public void ScaleSingle(Control ctrl,ScaleMode scaleMode = ScaleMode.MaintainSel
var values = groups[ctrl];
foreach (var item in values)
{
item.Font = ctrl.Font.Clone() as Font;
//item.Font = ctrl.Font.Clone() as Font;
item.Font = new Font(ctrl.Font.FontFamily, ctrl.Font.Size, item.Font.Style);
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions Demo/6_字体自适应/FontScaleForm1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions Demo/6_字体自适应/FontScaleForm2.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Demo/7_常用控件测试/Form_Textbox.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da1f4fb

Please sign in to comment.