重构GDI一些方法

This commit is contained in:
Sunny 2023-10-30 10:06:40 +08:00
parent eefd366b6f
commit a04160cff2
20 changed files with 250 additions and 321 deletions

View File

@ -547,12 +547,10 @@ namespace Sunny.UI
if (!Option.YAxis.ShowGridLine) continue; if (!Option.YAxis.ShowGridLine) continue;
if (!YLabels[i].EqualsDouble(0)) if (!YLabels[i].EqualsDouble(0))
{ {
using (Pen pn = new Pen(ForeColor)) using Pen pn = new Pen(ForeColor);
{ pn.DashStyle = DashStyle.Dash;
pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 };
pn.DashPattern = new float[] { 3, 3 }; g.DrawLine(pn, DrawOrigin.X, labels[i], Width - Option.Grid.Right, labels[i]);
g.DrawLine(pn, DrawOrigin.X, labels[i], Width - Option.Grid.Right, labels[i]);
}
} }
else else
{ {

View File

@ -418,12 +418,10 @@ namespace Sunny.UI
if (Option.XAxis.ShowGridLine) if (Option.XAxis.ShowGridLine)
{ {
using (Pen pn = new Pen(ForeColor)) using Pen pn = new Pen(ForeColor);
{ pn.DashStyle = DashStyle.Dash;
pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 };
pn.DashPattern = new float[] { 3, 3 }; g.DrawLine(pn, x, DrawOrigin.Y, x, Option.Grid.Top);
g.DrawLine(pn, x, DrawOrigin.Y, x, Option.Grid.Top);
}
} }
} }
@ -464,12 +462,10 @@ namespace Sunny.UI
if (Option.YAxis.ShowGridLine) if (Option.YAxis.ShowGridLine)
{ {
using (Pen pn = new Pen(ForeColor)) using Pen pn = new Pen(ForeColor);
{ pn.DashStyle = DashStyle.Dash;
pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 };
pn.DashPattern = new float[] { 3, 3 }; g.DrawLine(pn, DrawOrigin.X, y, Width - Option.Grid.Right, y);
g.DrawLine(pn, DrawOrigin.X, y, Width - Option.Grid.Right, y);
}
} }
} }
@ -511,16 +507,6 @@ namespace Sunny.UI
{ {
g.DrawLine(ForeColor, Width - Option.Grid.Right, y, Width - Option.Grid.Right + Option.YAxis.AxisTick.Length, y); g.DrawLine(ForeColor, Width - Option.Grid.Right, y, Width - Option.Grid.Right + Option.YAxis.AxisTick.Length, y);
} }
if (y.Equals(DrawOrigin.Y)) continue;
if (y.Equals(DrawOrigin.X - DrawSize.Height)) continue;
using (Pen pn = new Pen(ForeColor))
{
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
//g.DrawLine(pn, DrawOrigin.X, y, Width - Option.Grid.Right, y);
}
} }
Size sfName = TextRenderer.MeasureText(Option.Y2Axis.Name, TempFont); Size sfName = TextRenderer.MeasureText(Option.Y2Axis.Name, TempFont);
@ -545,49 +531,47 @@ namespace Sunny.UI
if (series.ShowLine || series.Symbol == UILinePointSymbol.None) if (series.ShowLine || series.Symbol == UILinePointSymbol.None)
{ {
using (Pen pen = new Pen(color, series.Width)) using Pen pen = new Pen(color, series.Width);
{ pen.DashStyle = series.DashStyle;
pen.DashStyle = series.DashStyle; if (series.DashPattern.IsValid()) pen.DashPattern = series.DashPattern;
if (series.DashPattern.IsValid()) pen.DashPattern = series.DashPattern; g.SetHighQuality();
g.SetHighQuality();
if (series is UISwitchLineSeries lineSeries) if (series is UISwitchLineSeries lineSeries)
{
List<PointF> points = new List<PointF>();
points.Add(new PointF(lineSeries.Points[0].X, lineSeries.YOffsetPos));
points.AddRange(lineSeries.Points);
points.Add(new PointF(lineSeries.Points[series.Points.Count - 1].X, lineSeries.YOffsetPos));
g.FillPath(color, points.ToArray().Path());
g.DrawLine(color, points[0], points[points.Count - 1]);
points.Clear();
}
else
{
if (series.ContainsNan || !series.Smooth || series.Points.Count == 2 || ZoomAreas.Count > 5)
{ {
List<PointF> points = new List<PointF>(); for (int i = 0; i < series.Points.Count - 1; i++)
points.Add(new PointF(lineSeries.Points[0].X, lineSeries.YOffsetPos)); {
points.AddRange(lineSeries.Points); g.DrawTwoPoints(pen, series.Points[i], series.Points[i + 1], DrawRect);
points.Add(new PointF(lineSeries.Points[series.Points.Count - 1].X, lineSeries.YOffsetPos)); }
g.FillPath(color, points.ToArray().Path());
g.DrawLine(color, points[0], points[points.Count - 1]);
points.Clear();
} }
else else
{ {
if (series.ContainsNan || !series.Smooth || series.Points.Count == 2 || ZoomAreas.Count > 5) try
{
g.DrawCurve(pen, series.Points.ToArray());
}
catch
{ {
for (int i = 0; i < series.Points.Count - 1; i++) for (int i = 0; i < series.Points.Count - 1; i++)
{ {
g.DrawTwoPoints(pen, series.Points[i], series.Points[i + 1], DrawRect); g.DrawTwoPoints(pen, series.Points[i], series.Points[i + 1], DrawRect);
} }
} }
else
{
try
{
g.DrawCurve(pen, series.Points.ToArray());
}
catch
{
for (int i = 0; i < series.Points.Count - 1; i++)
{
g.DrawTwoPoints(pen, series.Points[i], series.Points[i + 1], DrawRect);
}
}
}
} }
g.SetDefaultQuality();
} }
g.SetDefaultQuality();
} }
} }
@ -612,10 +596,8 @@ namespace Sunny.UI
{ {
Color color = series.Color; Color color = series.Color;
if (!series.CustomColor) color = ChartStyle.GetColor(idx); if (!series.CustomColor) color = ChartStyle.GetColor(idx);
using (Graphics graphics = bmp.Graphics()) using Graphics graphics = bmp.Graphics();
{ DrawSeries(graphics, color, series);
DrawSeries(graphics, color, series);
}
idx++; idx++;
} }
@ -701,71 +683,70 @@ namespace Sunny.UI
if (series.Symbol != UILinePointSymbol.None) if (series.Symbol != UILinePointSymbol.None)
{ {
using (Brush br = new SolidBrush(FillColor)) using Brush br = new SolidBrush(FillColor);
using (Brush br1 = new SolidBrush(color)) using Brush br1 = new SolidBrush(color);
using (Pen pn = new Pen(color, series.SymbolLineWidth)) using Pen pn = new Pen(color, series.SymbolLineWidth);
{
foreach (var p in series.Points)
{
if (p.X < Option.Grid.Left || p.X > Width - Option.Grid.Right) continue;
if (p.Y < Option.Grid.Top || p.Y > Height - Option.Grid.Bottom) continue;
if (double.IsNaN(p.X) || double.IsNaN(p.Y)) continue;
switch (series.Symbol) foreach (var p in series.Points)
{ {
case UILinePointSymbol.Square: if (p.X < Option.Grid.Left || p.X > Width - Option.Grid.Right) continue;
g.FillRectangle(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2); if (p.Y < Option.Grid.Top || p.Y > Height - Option.Grid.Bottom) continue;
g.DrawRectangle(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2); if (double.IsNaN(p.X) || double.IsNaN(p.Y)) continue;
break;
case UILinePointSymbol.Diamond: switch (series.Symbol)
{ {
PointF pt1 = new PointF(p.X - series.SymbolSize, p.Y); case UILinePointSymbol.Square:
PointF pt2 = new PointF(p.X, p.Y - series.SymbolSize); g.FillRectangle(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
PointF pt3 = new PointF(p.X + series.SymbolSize, p.Y); g.DrawRectangle(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
PointF pt4 = new PointF(p.X, p.Y + series.SymbolSize); break;
PointF[] pts = { pt1, pt2, pt3, pt4, pt1 }; case UILinePointSymbol.Diamond:
g.SetHighQuality(); {
GraphicsPath path = pts.Path(); PointF pt1 = new PointF(p.X - series.SymbolSize, p.Y);
g.FillPath(br, path); PointF pt2 = new PointF(p.X, p.Y - series.SymbolSize);
g.DrawPath(pn, path); PointF pt3 = new PointF(p.X + series.SymbolSize, p.Y);
path.Dispose(); PointF pt4 = new PointF(p.X, p.Y + series.SymbolSize);
} PointF[] pts = { pt1, pt2, pt3, pt4, pt1 };
break;
case UILinePointSymbol.Triangle:
{
PointF pt1 = new PointF(p.X, p.Y - series.SymbolSize);
PointF pt2 = new PointF(p.X - series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f);
PointF pt3 = new PointF(p.X + series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f);
PointF[] pts = { pt1, pt2, pt3, pt1 };
g.SetHighQuality();
GraphicsPath path = pts.Path();
g.FillPath(br, path);
g.DrawPath(pn, path);
path.Dispose();
}
break;
case UILinePointSymbol.Circle:
g.SetHighQuality(); g.SetHighQuality();
g.FillEllipse(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2); GraphicsPath path = pts.Path();
g.DrawEllipse(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2); g.FillPath(br, path);
break; g.DrawPath(pn, path);
case UILinePointSymbol.Round: path.Dispose();
}
break;
case UILinePointSymbol.Triangle:
{
PointF pt1 = new PointF(p.X, p.Y - series.SymbolSize);
PointF pt2 = new PointF(p.X - series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f);
PointF pt3 = new PointF(p.X + series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f);
PointF[] pts = { pt1, pt2, pt3, pt1 };
g.SetHighQuality(); g.SetHighQuality();
g.FillEllipse(br1, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2); GraphicsPath path = pts.Path();
break; g.FillPath(br, path);
case UILinePointSymbol.Plus: g.DrawPath(pn, path);
g.DrawLine(pn, p.X - series.SymbolSize, p.Y, p.X + series.SymbolSize, p.Y); path.Dispose();
g.DrawLine(pn, p.X, p.Y - series.SymbolSize, p.X, p.Y + series.SymbolSize); }
break; break;
case UILinePointSymbol.Star: case UILinePointSymbol.Circle:
g.SetHighQuality(); g.SetHighQuality();
g.DrawLine(pn, p.X, p.Y - series.SymbolSize, p.X, p.Y + series.SymbolSize); g.FillEllipse(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
g.DrawLine(pn, p.X - series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f, g.DrawEllipse(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
p.X + series.SymbolSize * 0.866f, p.Y - series.SymbolSize * 0.5f); break;
g.DrawLine(pn, p.X - series.SymbolSize * 0.866f, p.Y - series.SymbolSize * 0.5f, case UILinePointSymbol.Round:
p.X + series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f); g.SetHighQuality();
break; g.FillEllipse(br1, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
} break;
case UILinePointSymbol.Plus:
g.DrawLine(pn, p.X - series.SymbolSize, p.Y, p.X + series.SymbolSize, p.Y);
g.DrawLine(pn, p.X, p.Y - series.SymbolSize, p.X, p.Y + series.SymbolSize);
break;
case UILinePointSymbol.Star:
g.SetHighQuality();
g.DrawLine(pn, p.X, p.Y - series.SymbolSize, p.X, p.Y + series.SymbolSize);
g.DrawLine(pn, p.X - series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f,
p.X + series.SymbolSize * 0.866f, p.Y - series.SymbolSize * 0.5f);
g.DrawLine(pn, p.X - series.SymbolSize * 0.866f, p.Y - series.SymbolSize * 0.5f,
p.X + series.SymbolSize * 0.866f, p.Y + series.SymbolSize * 0.5f);
break;
} }
} }
@ -786,17 +767,14 @@ namespace Sunny.UI
if (pos <= Option.Grid.Top || pos >= Height - Option.Grid.Bottom) continue; if (pos <= Option.Grid.Top || pos >= Height - Option.Grid.Bottom) continue;
using (Pen pn = new Pen(line.Color, line.Size)) using Pen pn = new Pen(line.Color, line.Size);
if (line.DashDot)
{ {
if (line.DashDot) pn.DashStyle = DashStyle.Dash;
{ pn.DashPattern = new float[] { 3, 3 };
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
}
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos);
} }
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos);
g.DrawString(line.Name, TempFont, line.Color, new Rectangle(DrawOrigin.X + 4, (int)pos - 2 - Height, DrawSize.Width - 8, Height), (StringAlignment)((int)line.Left), StringAlignment.Far); g.DrawString(line.Name, TempFont, line.Color, new Rectangle(DrawOrigin.X + 4, (int)pos - 2 - Height, DrawSize.Width - 8, Height), (StringAlignment)((int)line.Left), StringAlignment.Far);
} }
} }
@ -808,17 +786,14 @@ namespace Sunny.UI
float pos = Y2Scale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height, Option.YDataOrder); float pos = Y2Scale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height, Option.YDataOrder);
if (pos <= Option.Grid.Top || pos >= Height - Option.Grid.Bottom) continue; if (pos <= Option.Grid.Top || pos >= Height - Option.Grid.Bottom) continue;
using (Pen pn = new Pen(line.Color, line.Size)) using Pen pn = new Pen(line.Color, line.Size);
if (line.DashDot)
{ {
if (line.DashDot) pn.DashStyle = DashStyle.Dash;
{ pn.DashPattern = new float[] { 3, 3 };
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
}
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos);
} }
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos);
g.DrawString(line.Name, TempFont, line.Color, new Rectangle(DrawOrigin.X + 4, (int)pos - 2 - Height, DrawSize.Width - 8, Height), (StringAlignment)((int)line.Left), StringAlignment.Far); g.DrawString(line.Name, TempFont, line.Color, new Rectangle(DrawOrigin.X + 4, (int)pos - 2 - Height, DrawSize.Width - 8, Height), (StringAlignment)((int)line.Left), StringAlignment.Far);
} }
} }
@ -833,17 +808,15 @@ namespace Sunny.UI
float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width); float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width);
if (pos <= Option.Grid.Left || pos >= Width - Option.Grid.Right) continue; if (pos <= Option.Grid.Left || pos >= Width - Option.Grid.Right) continue;
using (Pen pn = new Pen(line.Color, line.Size)) using Pen pn = new Pen(line.Color, line.Size);
if (line.DashDot)
{ {
if (line.DashDot) pn.DashStyle = DashStyle.Dash;
{ pn.DashPattern = new float[] { 3, 3 };
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
}
g.DrawLine(pn, pos, Height - Option.Grid.Bottom - 1, pos, Option.Grid.Top + 1);
} }
g.DrawLine(pn, pos, Height - Option.Grid.Bottom - 1, pos, Option.Grid.Top + 1);
Size sf = TextRenderer.MeasureText(line.Name, TempFont); Size sf = TextRenderer.MeasureText(line.Name, TempFont);
float x = pos - sf.Width; float x = pos - sf.Width;
if (x < Option.Grid.Left) x = pos + 2; if (x < Option.Grid.Left) x = pos + 2;
@ -973,12 +946,10 @@ namespace Sunny.UI
} }
else else
{ {
using (Graphics g = this.CreateGraphics()) using Graphics g = this.CreateGraphics();
{ using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont);
Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont); tip.Size = new Size(sf.Width + 4, sf.Height + 4);
tip.Size = new Size(sf.Width + 4, sf.Height + 4);
}
int x = e.Location.X + 15; int x = e.Location.X + 15;
int y = e.Location.Y + 20; int y = e.Location.Y + 20;

View File

@ -266,21 +266,20 @@ namespace Sunny.UI
public static void ResetBorderColor(Message m, Control control, int width, Color color) public static void ResetBorderColor(Message m, Control control, int width, Color color)
{ {
//根据颜色和边框像素取得一条线 //根据颜色和边框像素取得一条线
using (Pen pen = new Pen(color, width)) using Pen pen = new Pen(color, width);
//得到当前的句柄
IntPtr hDC = (IntPtr)Win32.User.GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0)
{ {
//得到当前的句柄 return;
IntPtr hDC = (IntPtr)Win32.User.GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0)
{
return;
}
//绘制边框
Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawRectangle(pen, 0, 0, control.Width - width, control.Height - width);
//释放
Win32.User.ReleaseDC(m.HWnd, hDC);
} }
//绘制边框
Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawRectangle(pen, 0, 0, control.Width - width, control.Height - width);
//释放
Win32.User.ReleaseDC(m.HWnd, hDC);
} }
/// <summary> /// <summary>

View File

@ -126,12 +126,10 @@ namespace Sunny.UI
public static Color[] GradientColors(this Color startColor, Color endColor, int count) public static Color[] GradientColors(this Color startColor, Color endColor, int count)
{ {
count = Math.Max(count, 2); count = Math.Max(count, 2);
Bitmap image = new Bitmap(1024, 3); using Bitmap image = new Bitmap(1024, 3);
Graphics g = image.Graphics(); using Graphics g = image.Graphics();
Brush br = new LinearGradientBrush(image.Bounds(), startColor, endColor, 0.0F); using Brush br = new LinearGradientBrush(image.Bounds(), startColor, endColor, 0.0F);
g.FillRectangle(br, image.Bounds()); g.FillRectangle(br, image.Bounds());
br.Dispose();
g.Dispose();
Color[] colors = new Color[count]; Color[] colors = new Color[count];
colors[0] = startColor; colors[0] = startColor;
@ -150,7 +148,6 @@ namespace Sunny.UI
fb.Dispose(); fb.Dispose();
} }
image.Dispose();
return colors; return colors;
} }

View File

@ -115,7 +115,8 @@ namespace Sunny.UI
FrameDimension fd = Image.GifFrameDimension(); FrameDimension fd = Image.GifFrameDimension();
Image.SelectActiveFrame(fd, frameIndex); Image.SelectActiveFrame(fd, frameIndex);
ShowImage.Graphics().DrawImage(Image, 0, 0, Image.Width, Image.Height); using var g = ShowImage.Graphics();
g.DrawImage(Image, 0, 0, Image.Width, Image.Height);
ImageChanged?.Invoke(this, ShowImage); ImageChanged?.Invoke(this, ShowImage);
} }
} }

View File

@ -128,13 +128,12 @@ namespace Sunny.UI
public static Bitmap ChangeOpacity(Image img, float opacity) public static Bitmap ChangeOpacity(Image img, float opacity)
{ {
Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image
Graphics graphics = bmp.Graphics(); using Graphics graphics = bmp.Graphics();
ColorMatrix matrix = new ColorMatrix(); ColorMatrix matrix = new ColorMatrix();
matrix.Matrix33 = opacity; matrix.Matrix33 = opacity;
ImageAttributes imgAttribute = new ImageAttributes(); ImageAttributes imgAttribute = new ImageAttributes();
imgAttribute.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imgAttribute.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute); graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
graphics.Dispose(); // Releasing all resource used by graphics
return bmp; return bmp;
} }
@ -663,19 +662,15 @@ namespace Sunny.UI
/// <param name="strokeThickness"></param> /// <param name="strokeThickness"></param>
public static void DrawStrokedRectangle(this Graphics g, Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1) public static void DrawStrokedRectangle(this Graphics g, Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1)
{ {
using (var bodyBrush = new SolidBrush(bodyColor)) using var bodyBrush = new SolidBrush(bodyColor);
{ var x = strokeThickness == 1 ? 0 : strokeThickness;
var x = strokeThickness == 1 ? 0 : strokeThickness; var y = strokeThickness == 1 ? 0 : strokeThickness;
var y = strokeThickness == 1 ? 0 : strokeThickness; var h = strokeThickness == 1 ? 1 : strokeThickness + 1;
var h = strokeThickness == 1 ? 1 : strokeThickness + 1; var w = strokeThickness == 1 ? 1 : strokeThickness + 1;
var w = strokeThickness == 1 ? 1 : strokeThickness + 1; var newRect = new Rectangle(rect.X + x, rect.Y + y, rect.Width - w, rect.Height - h);
var newRect = new Rectangle(rect.X + x, rect.Y + y, rect.Width - w, rect.Height - h); using var strokePen = new Pen(strokeColor, strokeThickness);
using (var strokePen = new Pen(strokeColor, strokeThickness)) g.FillRectangle(bodyBrush, newRect);
{ g.DrawRectangle(strokePen, newRect);
g.FillRectangle(bodyBrush, newRect);
g.DrawRectangle(strokePen, newRect);
}
}
} }
/// <summary> /// <summary>
@ -688,19 +683,15 @@ namespace Sunny.UI
/// <param name="strokeThickness"></param> /// <param name="strokeThickness"></param>
public static void DrawStrokedEllipse(this Graphics g, Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1) public static void DrawStrokedEllipse(this Graphics g, Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1)
{ {
using (var bodyBrush = new SolidBrush(bodyColor)) using var bodyBrush = new SolidBrush(bodyColor);
{ var x = strokeThickness == 1 ? 0 : strokeThickness;
var x = strokeThickness == 1 ? 0 : strokeThickness; var y = strokeThickness == 1 ? 0 : strokeThickness;
var y = strokeThickness == 1 ? 0 : strokeThickness; var h = strokeThickness == 1 ? 1 : strokeThickness + 1;
var h = strokeThickness == 1 ? 1 : strokeThickness + 1; var w = strokeThickness == 1 ? 1 : strokeThickness + 1;
var w = strokeThickness == 1 ? 1 : strokeThickness + 1; var newRect = new Rectangle(rect.X + x, rect.Y + y, rect.Width - w, rect.Height - h);
var newRect = new Rectangle(rect.X + x, rect.Y + y, rect.Width - w, rect.Height - h); using var strokePen = new Pen(strokeColor, strokeThickness);
using (var strokePen = new Pen(strokeColor, strokeThickness)) g.FillEllipse(bodyBrush, newRect);
{ g.DrawEllipse(strokePen, newRect);
g.FillEllipse(bodyBrush, newRect);
g.DrawEllipse(strokePen, newRect);
}
}
} }
/// <summary> /// <summary>

View File

@ -288,13 +288,10 @@ namespace Sunny.UI
{ {
for (int col = 0; col < m_cols; col++) for (int col = 0; col < m_cols; col++)
{ {
if (index >= m_colors.Count) if (index >= m_colors.Count) break;
break;
Rectangle rect = GetRectangle(row, col); Rectangle rect = GetRectangle(row, col);
using (SolidBrush brush = new SolidBrush(m_colors[index++])) using SolidBrush brush = new SolidBrush(m_colors[index++]);
{ e.Graphics.FillRectangle(brush, rect);
e.Graphics.FillRectangle(brush, rect);
}
} }
} }
if (m_selindex >= 0) if (m_selindex >= 0)
@ -302,10 +299,9 @@ namespace Sunny.UI
Rectangle rect = GetSelectedItemRect(); Rectangle rect = GetSelectedItemRect();
e.Graphics.FillRectangle(Brushes.White, rect); e.Graphics.FillRectangle(Brushes.White, rect);
rect.Inflate(-3, -3); rect.Inflate(-3, -3);
using (SolidBrush brush = new SolidBrush(SelectedItem)) using SolidBrush brush = new SolidBrush(SelectedItem);
{ e.Graphics.FillRectangle(brush, rect);
e.Graphics.FillRectangle(brush, rect);
}
if (Focused) if (Focused)
{ {
rect.Inflate(2, 2); rect.Inflate(2, 2);

View File

@ -131,10 +131,8 @@ namespace Sunny.UI
Height = Width; Height = Width;
} }
using (SolidBrush b = new SolidBrush(BackColor)) using SolidBrush b = new SolidBrush(BackColor);
{ e.Graphics.FillRectangle(b, ClientRectangle);
e.Graphics.FillRectangle(b, ClientRectangle);
}
RectangleF wheelRectangle = WheelRectangle; RectangleF wheelRectangle = WheelRectangle;
UIColorUtil.DrawFrame(e.Graphics, wheelRectangle, 6, m_frameColor); UIColorUtil.DrawFrame(e.Graphics, wheelRectangle, 6, m_frameColor);

View File

@ -143,67 +143,66 @@ namespace Sunny.UI
r.Width -= Padding.Right; r.Width -= Padding.Right;
r.Height -= Padding.Bottom; r.Height -= Padding.Bottom;
using (SolidBrush b = new SolidBrush(ForeColor)) using SolidBrush b = new SolidBrush(ForeColor);
if (TextAngle.EqualsFloat(0))
{ {
if (TextAngle.EqualsFloat(0)) e.Graphics.DrawString(Text, Font, ForeColor, r, TextAlign);
}
else
{
PointF center = UIColorUtil.Center(ClientRectangle);
switch (RotatePointAlignment)
{ {
e.Graphics.DrawString(Text, Font, ForeColor, r, TextAlign); case ContentAlignment.TopLeft:
center.X = r.Left;
center.Y = r.Top;
break;
case ContentAlignment.TopCenter:
center.Y = r.Top;
break;
case ContentAlignment.TopRight:
center.X = r.Right;
center.Y = r.Top;
break;
case ContentAlignment.MiddleLeft:
center.X = r.Left;
break;
case ContentAlignment.MiddleCenter:
break;
case ContentAlignment.MiddleRight:
center.X = r.Right;
break;
case ContentAlignment.BottomLeft:
center.X = r.Left;
center.Y = r.Bottom;
break;
case ContentAlignment.BottomCenter:
center.Y = r.Bottom;
break;
case ContentAlignment.BottomRight:
center.X = r.Right;
center.Y = r.Bottom;
break;
} }
else
{
PointF center = UIColorUtil.Center(ClientRectangle);
switch (RotatePointAlignment)
{
case ContentAlignment.TopLeft:
center.X = r.Left;
center.Y = r.Top;
break;
case ContentAlignment.TopCenter: center.X += Padding.Left;
center.Y = r.Top; center.Y += Padding.Top;
break; center.X -= Padding.Right;
center.Y -= Padding.Bottom;
case ContentAlignment.TopRight: e.Graphics.TranslateTransform(center.X, center.Y);
center.X = r.Right; e.Graphics.RotateTransform(TextAngle);
center.Y = r.Top;
break;
case ContentAlignment.MiddleLeft: e.Graphics.DrawString(Text, Font, b, new PointF(0, 0), format);
center.X = r.Left; e.Graphics.ResetTransform();
break;
case ContentAlignment.MiddleCenter:
break;
case ContentAlignment.MiddleRight:
center.X = r.Right;
break;
case ContentAlignment.BottomLeft:
center.X = r.Left;
center.Y = r.Bottom;
break;
case ContentAlignment.BottomCenter:
center.Y = r.Bottom;
break;
case ContentAlignment.BottomRight:
center.X = r.Right;
center.Y = r.Bottom;
break;
}
center.X += Padding.Left;
center.Y += Padding.Top;
center.X -= Padding.Right;
center.Y -= Padding.Bottom;
e.Graphics.TranslateTransform(center.X, center.Y);
e.Graphics.RotateTransform(TextAngle);
e.Graphics.DrawString(Text, Font, b, new PointF(0, 0), format);
e.Graphics.ResetTransform();
}
} }
} }

View File

@ -165,11 +165,8 @@ namespace Sunny.UI
points.Add(new PointF(3 + Height / 2.0f, Height / 2.0f)); points.Add(new PointF(3 + Height / 2.0f, Height / 2.0f));
points.Add(new PointF(3, 0)); points.Add(new PointF(3, 0));
using (Brush br = new SolidBrush(SelectedColor)) using Brush br = new SolidBrush(SelectedColor);
{ g.FillPolygon(br, points.ToArray());
g.FillPolygon(br, points.ToArray());
}
g.DrawString(Text, Font, ForeColor, ClientRectangle, ContentAlignment.MiddleCenter); g.DrawString(Text, Font, ForeColor, ClientRectangle, ContentAlignment.MiddleCenter);
} }
else else
@ -233,10 +230,8 @@ namespace Sunny.UI
ClickArea[index] = pts; ClickArea[index] = pts;
} }
using (Brush br = new SolidBrush(index <= ItemIndex ? SelectedColor : UnSelectedColor)) using Brush br = new SolidBrush(index <= ItemIndex ? SelectedColor : UnSelectedColor);
{ g.FillPolygon(br, points.ToArray());
g.FillPolygon(br, points.ToArray());
}
g.DrawString(item.ToString(), Font, index <= ItemIndex ? ForeColor : UnSelectedForeColor, g.DrawString(item.ToString(), Font, index <= ItemIndex ? ForeColor : UnSelectedForeColor,
new Rectangle(begin, 0, itemWidth, Height), ContentAlignment.MiddleCenter); new Rectangle(begin, 0, itemWidth, Height), ContentAlignment.MiddleCenter);

View File

@ -222,7 +222,7 @@ namespace Sunny.UI
} }
var img = new Bitmap(Width, Height); var img = new Bitmap(Width, Height);
Graphics g = img.Graphics(); using Graphics g = img.Graphics();
g.Clear(FillColor); g.Clear(FillColor);
float size = avatarSize; float size = avatarSize;

View File

@ -835,13 +835,15 @@ namespace Sunny.UI
{ {
if (lastIndex >= 0 && lastIndex != SelectedIndex) if (lastIndex >= 0 && lastIndex != SelectedIndex)
{ {
OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed)); using var g = CreateGraphics();
OnDrawItem(new DrawItemEventArgs(g, Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed));
} }
mouseIndex = value; mouseIndex = value;
if (mouseIndex >= 0 && mouseIndex != SelectedIndex) if (mouseIndex >= 0 && mouseIndex != SelectedIndex)
{ {
OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight)); using var g = CreateGraphics();
OnDrawItem(new DrawItemEventArgs(g, Font, GetItemRectangle(value), value, DrawItemState.HotLight));
} }
lastIndex = mouseIndex; lastIndex = mouseIndex;

View File

@ -1078,7 +1078,7 @@ namespace Sunny.UI
if (_showLargeScale) if (_showLargeScale)
{ {
Graphics Gr = this.CreateGraphics(); using Graphics Gr = this.CreateGraphics();
string strvalmax = _maximum.ToString(); string strvalmax = _maximum.ToString();
string strvalmin = _minimum.ToString(); string strvalmin = _minimum.ToString();
string strval = strvalmax.Length > strvalmin.Length ? strvalmax : strvalmin; string strval = strvalmax.Length > strvalmin.Length ? strvalmax : strvalmin;
@ -1132,8 +1132,6 @@ namespace Sunny.UI
// Rectangle of the rounded knob // Rectangle of the rounded knob
this.rKnob = new Rectangle((int)x, (int)y, (int)w, (int)h); this.rKnob = new Rectangle((int)x, (int)y, (int)w, (int)h);
Gr.Dispose();
} }
else else
{ {

View File

@ -396,13 +396,15 @@ namespace Sunny.UI
{ {
if (lastIndex >= 0 && lastIndex >= 0 && lastIndex < Items.Count && lastIndex != SelectedIndex) if (lastIndex >= 0 && lastIndex >= 0 && lastIndex < Items.Count && lastIndex != SelectedIndex)
{ {
OnDrawItem(new DrawItemEventArgs(CreateGraphics(), Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed)); using var g = CreateGraphics();
OnDrawItem(new DrawItemEventArgs(g, Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed));
} }
mouseIndex = value; mouseIndex = value;
if (mouseIndex >= 0 && mouseIndex >= 0 && mouseIndex < Items.Count && mouseIndex != SelectedIndex) if (mouseIndex >= 0 && mouseIndex >= 0 && mouseIndex < Items.Count && mouseIndex != SelectedIndex)
{ {
OnDrawItem(new DrawItemEventArgs(CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight)); using var g = CreateGraphics();
OnDrawItem(new DrawItemEventArgs(g, Font, GetItemRectangle(value), value, DrawItemState.HotLight));
} }
lastIndex = mouseIndex; lastIndex = mouseIndex;

View File

@ -555,7 +555,7 @@ namespace Sunny.UI
return; return;
} }
Graphics g = CreateGraphics(); using Graphics g = CreateGraphics();
if (CurrentNode != null && CurrentNode != SelectedNode) if (CurrentNode != null && CurrentNode != SelectedNode)
{ {
ClearCurrentNode(g); ClearCurrentNode(g);
@ -566,8 +566,6 @@ namespace Sunny.UI
CurrentNode = node; CurrentNode = node;
OnDrawNode(new DrawTreeNodeEventArgs(g, CurrentNode, new Rectangle(0, CurrentNode.Bounds.Y, Width, CurrentNode.Bounds.Height), TreeNodeStates.Hot)); OnDrawNode(new DrawTreeNodeEventArgs(g, CurrentNode, new Rectangle(0, CurrentNode.Bounds.Y, Width, CurrentNode.Bounds.Height), TreeNodeStates.Hot));
} }
g.Dispose();
} }
private void ClearCurrentNode(Graphics g) private void ClearCurrentNode(Graphics g)
@ -585,9 +583,8 @@ namespace Sunny.UI
/// <param name="e">鼠标参数</param> /// <param name="e">鼠标参数</param>
protected override void OnMouseLeave(EventArgs e) protected override void OnMouseLeave(EventArgs e)
{ {
Graphics g = CreateGraphics(); using Graphics g = CreateGraphics();
ClearCurrentNode(g); ClearCurrentNode(g);
g.Dispose();
base.OnMouseLeave(e); base.OnMouseLeave(e);
} }

View File

@ -166,7 +166,7 @@ namespace Sunny.UI
imageRadius = Radius; imageRadius = Radius;
} }
Graphics g = image.Graphics(); using Graphics g = image.Graphics();
g.Clear(Color.Transparent); g.Clear(Color.Transparent);
Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1); Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
@ -178,8 +178,6 @@ namespace Sunny.UI
DrawString(g, processText, Font, fillColor, Size, Padding, TextAlign); DrawString(g, processText, Font, fillColor, Size, Padding, TextAlign);
} }
g.Dispose();
if (Direction == UILine.LineDirection.Horizontal) if (Direction == UILine.LineDirection.Horizontal)
{ {
e.Graphics.DrawImage(image, e.Graphics.DrawImage(image,

View File

@ -102,14 +102,12 @@ namespace Sunny.UI
if (image == null) if (image == null)
{ {
image = new Bitmap(Width, Height); image = new Bitmap(Width, Height);
Graphics ig = image.Graphics(); using Graphics ig = image.Graphics();
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
Point pt = GetPoint(i, circleSize); Point pt = GetPoint(i, circleSize);
ig.FillEllipse(Color.FromArgb(192, foreColor), pt.X, pt.Y, circleSize, circleSize); ig.FillEllipse(Color.FromArgb(192, foreColor), pt.X, pt.Y, circleSize, circleSize);
} }
ig.Dispose();
} }
g.DrawImage(image, 0, 0); g.DrawImage(image, 0, 0);

View File

@ -378,12 +378,10 @@ namespace Sunny.UI
Rectangle arrowRect = CalcArrowRect(CollapseRect); Rectangle arrowRect = CalcArrowRect(CollapseRect);
Color handleRectColor = _uiControlState == UIControlState.Hover ? handleHoverColor : HandleColor; Color handleRectColor = _uiControlState == UIControlState.Hover ? handleHoverColor : HandleColor;
Point[] points = GetHandlePoints(); Point[] points = GetHandlePoints();
using (Brush br = new SolidBrush(handleRectColor)) using Brush br = new SolidBrush(handleRectColor);
{ e.Graphics.SetHighQuality();
e.Graphics.SetHighQuality(); e.Graphics.FillPolygon(br, points);
e.Graphics.FillPolygon(br, points); e.Graphics.SetDefaultQuality();
e.Graphics.SetDefaultQuality();
}
if (SplitterWidth >= 9) if (SplitterWidth >= 9)
{ {

View File

@ -959,7 +959,7 @@ namespace Sunny.UI
var node = GetNodeAt(e.Location); var node = GetNodeAt(e.Location);
if (node == null || CurrentNode == node) return; if (node == null || CurrentNode == node) return;
var g = CreateGraphics(); using var g = CreateGraphics();
if (CurrentNode != null && CurrentNode != SelectedNode) if (CurrentNode != null && CurrentNode != SelectedNode)
{ {
ClearCurrentNode(g); ClearCurrentNode(g);
@ -970,8 +970,6 @@ namespace Sunny.UI
CurrentNode = node; CurrentNode = node;
OnDrawNode(new DrawTreeNodeEventArgs(g, CurrentNode, new Rectangle(0, CurrentNode.Bounds.Y, Width, CurrentNode.Bounds.Height), TreeNodeStates.Hot)); OnDrawNode(new DrawTreeNodeEventArgs(g, CurrentNode, new Rectangle(0, CurrentNode.Bounds.Y, Width, CurrentNode.Bounds.Height), TreeNodeStates.Hot));
} }
g.Dispose();
} }
/// <summary> /// <summary>
@ -980,9 +978,8 @@ namespace Sunny.UI
/// <param name="e">鼠标参数</param> /// <param name="e">鼠标参数</param>
protected override void OnMouseLeave(EventArgs e) protected override void OnMouseLeave(EventArgs e)
{ {
var g = CreateGraphics(); using var g = CreateGraphics();
ClearCurrentNode(g); ClearCurrentNode(g);
g.Dispose();
} }
private void ClearCurrentNode(Graphics g) private void ClearCurrentNode(Graphics g)
@ -1306,10 +1303,8 @@ namespace Sunny.UI
if (ByMouse) if (ByMouse)
{ {
var g = CreateGraphics(); using var g = CreateGraphics();
OnDrawNode(new DrawTreeNodeEventArgs(g, parentNode, OnDrawNode(new DrawTreeNodeEventArgs(g, parentNode, new Rectangle(0, parentNode.Bounds.Y, Width, parentNode.Bounds.Height), TreeNodeStates.Hot));
new Rectangle(0, parentNode.Bounds.Y, Width, parentNode.Bounds.Height), TreeNodeStates.Hot));
g.Dispose();
if (parentNode.Parent != null) //如果父节点之上还有父节点 if (parentNode.Parent != null) //如果父节点之上还有父节点
{ {

View File

@ -164,13 +164,9 @@ namespace Sunny.UI
public static Image CreateImage(int symbol, int size, Color color) public static Image CreateImage(int symbol, int size, Color color)
{ {
Bitmap image = new Bitmap(size, size); Bitmap image = new Bitmap(size, size);
using Graphics g = image.Graphics();
using (Graphics g = image.Graphics()) SizeF sf = g.GetFontImageSize(symbol, size);
{ g.DrawFontImage(symbol, size, color, (image.Width - sf.Width) / 2.0f, (image.Height - sf.Height) / 2.0f);
SizeF sf = g.GetFontImageSize(symbol, size);
g.DrawFontImage(symbol, size, color, (image.Width - sf.Width) / 2.0f, (image.Height - sf.Height) / 2.0f);
}
return image; return image;
} }