* 重构一些笔刷和画笔的资源释放

This commit is contained in:
Sunny 2023-05-28 11:36:13 +08:00
parent f0c65df166
commit 3fc41f95d2
12 changed files with 89 additions and 67 deletions

View File

@ -201,12 +201,13 @@ namespace Sunny.UI
public static void DrawFrame(Graphics dc, RectangleF r, float cornerRadius, Color color)
{
Pen pen = new Pen(color);
using Pen pen = new Pen(color);
if (cornerRadius <= 0)
{
dc.DrawRectangle(pen, Rect(r));
return;
}
cornerRadius = (float)Math.Min(cornerRadius, Math.Floor(r.Width) - 2);
cornerRadius = (float)Math.Min(cornerRadius, Math.Floor(r.Height) - 2);

View File

@ -180,11 +180,7 @@ namespace Sunny.UI
Color bodyColor = this.AnalogMeter.BodyColor;
Color cDark = LBColorManager.StepColor(bodyColor, 20);
LinearGradientBrush br1 = new LinearGradientBrush(rc,
bodyColor,
cDark,
45);
using LinearGradientBrush br1 = new LinearGradientBrush(rc, bodyColor, cDark, 45);
Gr.FillEllipse(br1, rc);
float drawRatio = this.AnalogMeter.GetDrawRatio();
@ -195,10 +191,7 @@ namespace Sunny.UI
_rc.Width -= 6 * drawRatio;
_rc.Height -= 6 * drawRatio;
LinearGradientBrush br2 = new LinearGradientBrush(_rc,
cDark,
bodyColor,
45);
using LinearGradientBrush br2 = new LinearGradientBrush(_rc, cDark, bodyColor, 45);
Gr.FillEllipse(br2, _rc);
return true;
@ -234,8 +227,8 @@ namespace Sunny.UI
float radius = (float)(w / 2 - (w * 0.08));
float rulerValue = (float)minValue;
Pen pen = new Pen(scaleColor, (2 * drawRatio));
SolidBrush br = new SolidBrush(scaleColor);
using Pen pen = new Pen(scaleColor, (2 * drawRatio));
using SolidBrush br = new SolidBrush(scaleColor);
PointF ptStart = new PointF(0, 0);
PointF ptEnd = new PointF(0, 0);
@ -323,7 +316,7 @@ namespace Sunny.UI
PointF ptStart = new PointF(0, 0);
PointF ptEnd = new PointF(0, 0);
GraphicsPath pth1 = new GraphicsPath();
using GraphicsPath pth1 = new GraphicsPath();
ptStart.X = cx;
ptStart.Y = cy;
@ -346,8 +339,8 @@ namespace Sunny.UI
pth1.CloseFigure();
SolidBrush br = new SolidBrush(this.AnalogMeter.NeedleColor);
Pen pen = new Pen(this.AnalogMeter.NeedleColor);
using SolidBrush br = new SolidBrush(this.AnalogMeter.NeedleColor);
using Pen pen = new Pen(this.AnalogMeter.NeedleColor);
Gr.DrawPath(pen, pth1);
Gr.FillPath(br, pth1);
@ -367,15 +360,12 @@ namespace Sunny.UI
_rc.Inflate(5 * drawRatio, 5 * drawRatio);
SolidBrush brTransp = new SolidBrush(clr1);
using SolidBrush brTransp = new SolidBrush(clr1);
Gr.FillEllipse(brTransp, _rc);
clr1 = clr;
Color clr2 = LBColorManager.StepColor(clr, 75);
LinearGradientBrush br1 = new LinearGradientBrush(rc,
clr1,
clr2,
45);
using LinearGradientBrush br1 = new LinearGradientBrush(rc, clr1, clr2, 45);
Gr.FillEllipse(br1, rc);
return true;
}
@ -389,12 +379,8 @@ namespace Sunny.UI
return true;
Color clr1 = Color.FromArgb(40, 200, 200, 200);
Color clr2 = Color.FromArgb(0, 200, 200, 200);
LinearGradientBrush br1 = new LinearGradientBrush(rc,
clr1,
clr2,
45);
using LinearGradientBrush br1 = new LinearGradientBrush(rc, clr1, clr2, 45);
Gr.FillEllipse(br1, rc);
return true;

View File

@ -552,6 +552,16 @@ namespace Sunny.UI
#endregion properties
~UIKnob()
{
DottedPen?.Dispose();
knobFont?.Dispose();
brushKnob?.Dispose();
brushKnobPointer.Dispose();
gOffScreen?.Dispose();
OffScreenImage?.Dispose();
}
public UIKnob()
{
// This call is required by the Windows.Forms Form Designer.
@ -920,10 +930,10 @@ namespace Sunny.UI
Font font;
Pen penL = new Pen(_scaleColor, (2 * drawRatio));
Pen penS = new Pen(_scaleColor, (1 * drawRatio));
using Pen penL = new Pen(_scaleColor, (2 * drawRatio));
using Pen penS = new Pen(_scaleColor, (1 * drawRatio));
SolidBrush br = new SolidBrush(_scaleColor);
using SolidBrush br = new SolidBrush(_scaleColor);
PointF ptStart = new PointF(0, 0);
PointF ptEnd = new PointF(0, 0);
@ -978,10 +988,8 @@ namespace Sunny.UI
str = String.Format("{0,0:D}", (int)val);
// If autosize
if (_scaleFontAutoSize)
strsize = TextRenderer.MeasureText(str, new Font(_scaleFont.FontFamily, fSize));
else
strsize = TextRenderer.MeasureText(str, new Font(_scaleFont.FontFamily, _scaleFont.Size));
using Font tmpFont = _scaleFontAutoSize ? new Font(_scaleFont.FontFamily, fSize) : new Font(_scaleFont.FontFamily, _scaleFont.Size);
strsize = TextRenderer.MeasureText(str, tmpFont);
if (_drawDivInside)
{

View File

@ -182,7 +182,7 @@ namespace Sunny.UI
{
SizeF sf = new SizeF(0, 0);
float x = 0;
Pen pen = new Pen(rectColor, lineSize);
using Pen pen = new Pen(rectColor, lineSize);
if (LineDashStyle != UILineDashStyle.None)
{
pen.DashStyle = (DashStyle)((int)LineDashStyle);
@ -296,8 +296,6 @@ namespace Sunny.UI
int left = (Width - lineSize) / 2;
g.DrawLine(pen, left, Padding.Top, left, Height - Padding.Top - Padding.Bottom);
}
pen.Dispose();
}
UILineDashStyle lineDashStyle = UILineDashStyle.None;

View File

@ -74,15 +74,16 @@ namespace Sunny.UI
int len = 60;
x = x + 34;
y = y + 34;
g.DrawLine(new Pen(BackColor, 2), x, y, x - len, y - len / 2 - 4);
g.DrawLine(new Pen(BackColor, 2), x, y, x - len, y + len / 2 + 4);
using Pen pen = new Pen(BackColor, 2);
g.DrawLine(pen, x, y, x - len, y - len / 2 - 4);
g.DrawLine(pen, x, y, x - len, y + len / 2 + 4);
g.DrawLine(new Pen(BackColor, 2), x, y, x + len, y - len / 2 - 4);
g.DrawLine(new Pen(BackColor, 2), x, y, x + len, y + len / 2 + 4);
g.DrawLine(pen, x, y, x + len, y - len / 2 - 4);
g.DrawLine(pen, x, y, x + len, y + len / 2 + 4);
g.SetDefaultQuality();
g.DrawLine(new Pen(BackColor, 2), x, y, x, y - 60);
g.DrawLine(new Pen(BackColor, 2), x, y, x, y + 60);
g.DrawLine(pen, x, y, x, y - 60);
g.DrawLine(pen, x, y, x, y + 60);
//S
DrawVerticalLine(g, ForeColor, 88, 22, 6, 17, true);

View File

@ -343,7 +343,8 @@ namespace Sunny.UI
{
for (int i = 1; i < w; i++)
{
g.DrawArc(new Pen(colors[i], 2), new Rectangle(i, i, Width - i * 2, Width - i * 2), 180, 90);
using Pen pen = new Pen(colors[i], 2);
g.DrawArc(pen, new Rectangle(i, i, Width - i * 2, Width - i * 2), 180, 90);
}
for (int i = 0; i < w; i++)
@ -362,7 +363,8 @@ namespace Sunny.UI
{
for (int i = 1; i < w; i++)
{
g.DrawArc(new Pen(colors[i], 2), new Rectangle(i - 1, i, Width - i * 2, Width - i * 2), 270, 90);
using Pen pen = new Pen(colors[i], 2);
g.DrawArc(pen, new Rectangle(i - 1, i, Width - i * 2, Width - i * 2), 270, 90);
}
for (int i = 0; i < w; i++)
@ -385,7 +387,8 @@ namespace Sunny.UI
{
for (int i = 1; i < w; i++)
{
g.DrawArc(new Pen(colors[i], 2), new Rectangle(i, Height - Width + i - 1, Width - i * 2, Width - i * 2), 90, 90);
using Pen pen = new Pen(colors[i], 2);
g.DrawArc(pen, new Rectangle(i, Height - Width + i - 1, Width - i * 2, Width - i * 2), 90, 90);
}
for (int i = 0; i < w; i++)
@ -404,7 +407,8 @@ namespace Sunny.UI
{
for (int i = 1; i < w; i++)
{
g.DrawArc(new Pen(colors[i], 2), new Rectangle(i - 1, Height - Width - 1 + i, Width - i * 2, Width - i * 2), 0, 90);
using Pen pen = new Pen(colors[i], 2);
g.DrawArc(pen, new Rectangle(i - 1, Height - Width - 1 + i, Width - i * 2, Width - i * 2), 0, 90);
}
for (int i = 0; i < w; i++)

View File

@ -158,7 +158,8 @@ namespace Sunny.UI
// 将位图背景填充为白色
Graphics graph = Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(fillColor), 0, 0, destBmp.Width, destBmp.Height);
using SolidBrush br = new SolidBrush(fillColor);
graph.FillRectangle(br, 0, 0, destBmp.Width, destBmp.Height);
graph.Dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)

View File

@ -127,7 +127,7 @@ namespace Sunny.UI
{
this.SetDPIScaleFont();
if (!UIDPIScale.DPIScaleIsOne())
if (UIDPIScale.NeedSetDPIFont())
{
TitleFont = TitleFont.DPIScaleFont();
}

View File

@ -275,7 +275,7 @@ namespace Sunny.UI
if (!IsScaled)
{
this.SetDPIScaleFont();
if (!UIDPIScale.DPIScaleIsOne())
if (UIDPIScale.NeedSetDPIFont())
{
this.TitleFont = TitleFont.DPIScaleFont();
}

View File

@ -38,13 +38,14 @@ namespace Sunny.UI
using Bitmap bmp = new Bitmap(1, 1);
using Graphics g = bmp.Graphics();
dpiScale = g.DpiX / 96.0f / (UIStyles.FontSize / 12.0f);
dpiScale = g.DpiX / 96.0f;
if (UIStyles.GlobalFont) dpiScale = dpiScale / (UIStyles.FontScale / 100.0f);
return dpiScale;
}
public static bool DPIScaleIsOne()
public static bool NeedSetDPIFont()
{
return DPIScale().EqualsFloat(1);
return DPIScale() > 1 || UIStyles.GlobalFont;
}
internal static float DPIScaleFontSize(this Font font)
@ -64,10 +65,20 @@ namespace Sunny.UI
{
if (UIStyles.DPIScale)
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, fontSize / DPIScale(), font.Style, font.Unit, font.GdiCharSet);
if (UIStyles.GlobalFont)
{
if (font.GdiCharSet == 134)
return new Font(UIStyles.FontName, fontSize / DPIScale(), font.Style, font.Unit, font.GdiCharSet);
else
return new Font(UIStyles.FontName, fontSize / DPIScale());
}
else
return new Font(font.FontFamily, fontSize / DPIScale());
{
if (font.GdiCharSet == 134)
return new Font(font.FontFamily, fontSize / DPIScale(), font.Style, font.Unit, font.GdiCharSet);
else
return new Font(font.FontFamily, fontSize / DPIScale());
}
}
else
{
@ -81,7 +92,7 @@ namespace Sunny.UI
internal static void SetDPIScaleFont(this Control control)
{
if (!UIStyles.DPIScale) return;
if (!UIDPIScale.DPIScaleIsOne())
if (UIDPIScale.NeedSetDPIFont())
{
if (control is IStyleInterface ctrl)
{

View File

@ -79,22 +79,32 @@ namespace Sunny.UI
set => UIStyles.DPIScale = value;
}
[DefaultValue(12f), Description("DPI缩放开启后可调字体大小默认12"), Category("SunnyUI")]
public float FontSize
{
get => UIStyles.FontSize;
set => UIStyles.FontSize = value;
}
[Editor("System.Drawing.Design.FontNameEditor", "System.Drawing.Design.UITypeEditor")]
[TypeConverter(typeof(FontNameConverter))]
[DefaultValue("微软雅黑")]
public string FontName
[Description("全局字体设置开启后,可调字体名称"), Category("SunnyUI")]
public string GlobalFontName
{
get => UIStyles.FontName;
set => UIStyles.FontName = value;
}
[DefaultValue(100)]
[Description("全局字体设置开启后可调字体大小缩放百分比默认100%"), Category("SunnyUI")]
public int GlobalFontScale
{
get => UIStyles.FontScale;
set => UIStyles.FontScale = value;
}
[DefaultValue(false)]
[Description("全局字体设置"), Category("SunnyUI")]
public bool GlobalFont
{
get => UIStyles.GlobalFont;
set => UIStyles.GlobalFont = value;
}
/// <summary>
/// 版本
/// </summary>

View File

@ -39,15 +39,17 @@ namespace Sunny.UI
/// </summary>
public static class UIStyles
{
public static bool GlobalFont { get; set; } = false;
public static bool DPIScale { get; set; }
public static bool ZoomScale { get; set; }
[Editor("System.Drawing.Design.FontNameEditor", "System.Drawing.Design.UITypeEditor")]
[TypeConverter(typeof(FontNameConverter))]
public static string FontName { get; set; } = "微软雅黑";
internal static string FontName { get; set; } = "微软雅黑";
public static float FontSize { get; set; } = 12;
internal static int FontScale { get; set; } = 100;
private static readonly ConcurrentDictionary<string, byte> FontCharSets = new ConcurrentDictionary<string, byte>();
@ -403,13 +405,13 @@ namespace Sunny.UI
foreach (var form in Forms.Values)
{
if (!UIDPIScale.DPIScaleIsOne())
if (UIDPIScale.NeedSetDPIFont())
form.SetDPIScale();
}
foreach (var page in Pages.Values)
{
if (!UIDPIScale.DPIScaleIsOne())
if (UIDPIScale.NeedSetDPIFont())
page.SetDPIScale();
}
}