* UIMessageTip: 修复可能出错的问题

This commit is contained in:
Sunny 2021-12-03 12:14:08 +08:00
parent 49936e04dd
commit 4a03dec6dc
4 changed files with 130 additions and 97 deletions

Binary file not shown.

View File

@ -25,6 +25,7 @@ using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
@ -317,99 +318,7 @@ namespace Sunny.UI
return list;
}
public static float DPIScale(this Control control)
{
return control.CreateGraphics().DpiX / 96.0f;
}
public static Font DPIScaleFont(this Control control, Font font)
{
if (UIStyles.DPIScale)
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, font.Size / control.DPIScale(), font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, font.Size / control.DPIScale());
}
else
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, font.Size, font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, font.Size);
}
}
public static Font DPIScaleFont(this Control control, Font font, float fontSize)
{
if (UIStyles.DPIScale)
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, fontSize / control.DPIScale(), font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, fontSize / control.DPIScale());
}
else
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, fontSize, font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, fontSize);
}
}
public static Font DPIScaleFont(this Font font)
{
if (UIStyles.DPIScale)
{
using Control control = new();
return control.DPIScaleFont(font);
}
else
{
return font;
}
}
public static void SetDPIScaleFont(this Control control)
{
if (!UIStyles.DPIScale) return;
if (!control.DPIScale().EqualsFloat(1))
{
if (control is IStyleInterface ctrl)
{
if (!ctrl.IsScaled)
control.Font = control.DPIScaleFont(control.Font);
}
}
}
public static List<Control> GetAllDPIScaleControls(this Control control)
{
var list = new List<Control>();
foreach (Control con in control.Controls)
{
list.Add(con);
if (con is UITextBox) continue;
if (con is UIDropControl) continue;
if (con is UIListBox) continue;
if (con is UIImageListBox) continue;
if (con is UIPagination) continue;
if (con is UIRichTextBox) continue;
if (con is UITreeView) continue;
if (con is UINavBar) continue;
if (con.Controls.Count > 0)
{
list.AddRange(GetAllDPIScaleControls(con));
}
}
return list;
}
/// <summary>
/// 查找包含接口名称的控件列表
/// </summary>

View File

@ -213,7 +213,9 @@ namespace Sunny.UI
}
}
})
{ IsBackground = true, Name = "T_Showing" }.Start();
{
IsBackground = true, Name = "T_Showing"
}.Start();
}
static void layer_Showing(object sender, EventArgs e)
@ -292,6 +294,8 @@ namespace Sunny.UI
return point;
}
static Font tmpFont;
/// <summary>
/// 创建消息窗图像,同时输出内容区,用于外部定位
/// </summary>
@ -302,7 +306,8 @@ namespace Sunny.UI
var iconBounds = Rectangle.Empty;
var textBounds = Rectangle.Empty;
Font font = style.TextFont ?? DefaultFont;
using Font tmp = font.DPIScaleFont();
if (tmpFont == null || !tmpFont.Size.EqualsFloat(font.DPIScaleFontSize()))
tmpFont = font.DPIScaleFont();
if (style.Icon != null)
{
@ -319,7 +324,7 @@ namespace Sunny.UI
textBounds.X += style.IconSpacing;
}
textBounds.Size = Size.Truncate(GraphicsUtils.MeasureString(text, tmp, 0, DefStringFormat));
textBounds.Size = Size.Truncate(GraphicsUtils.MeasureString(text, tmpFont, 0, DefStringFormat));
size.Width += textBounds.Width;
if (size.Height < textBounds.Height)
@ -373,7 +378,7 @@ namespace Sunny.UI
{
textBrush = new SolidBrush(style.TextColor);
//DEBUG: g.DrawRectangle(new Border(Color.Red){ Width=1, Direction= Direction.Inner}.Pen, textBounds);
g.DrawString(text, tmp, textBrush, textBounds.Location, DefStringFormat);
g.DrawString(text, tmpFont, textBrush, textBounds.Location, DefStringFormat);
}
g.Flush(FlushIntention.Sync);

119
SunnyUI/Style/UIDPIScale.cs Normal file
View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
{
public static class UIDPIScale
{
public static float DPIScale(this Control control)
{
return control.CreateGraphics().DpiX / 96.0f;
}
public static Font DPIScaleFont(this Control control, Font font)
{
if (UIStyles.DPIScale)
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, font.Size / control.DPIScale(), font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, font.Size / control.DPIScale());
}
else
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, font.Size, font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, font.Size);
}
}
public static float DPIScaleFontSize(this Control control, Font font)
{
if (UIStyles.DPIScale)
return font.Size / control.DPIScale();
else
return font.Size;
}
public static float DPIScaleFontSize(this Font font)
{
using Control control = new();
if (UIStyles.DPIScale)
return font.Size / control.DPIScale();
else
return font.Size;
}
public static Font DPIScaleFont(this Control control, Font font, float fontSize)
{
if (UIStyles.DPIScale)
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, fontSize / control.DPIScale(), font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, fontSize / control.DPIScale());
}
else
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, fontSize, font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, fontSize);
}
}
public static Font DPIScaleFont(this Font font)
{
if (UIStyles.DPIScale)
{
using Control control = new();
return control.DPIScaleFont(font);
}
else
{
return font;
}
}
public static void SetDPIScaleFont(this Control control)
{
if (!UIStyles.DPIScale) return;
if (!control.DPIScale().EqualsFloat(1))
{
if (control is IStyleInterface ctrl)
{
if (!ctrl.IsScaled)
control.Font = control.DPIScaleFont(control.Font);
}
}
}
public static List<Control> GetAllDPIScaleControls(this Control control)
{
var list = new List<Control>();
foreach (Control con in control.Controls)
{
list.Add(con);
if (con is UITextBox) continue;
if (con is UIDropControl) continue;
if (con is UIListBox) continue;
if (con is UIImageListBox) continue;
if (con is UIPagination) continue;
if (con is UIRichTextBox) continue;
if (con is UITreeView) continue;
if (con is UINavBar) continue;
if (con.Controls.Count > 0)
{
list.AddRange(GetAllDPIScaleControls(con));
}
}
return list;
}
}
}