重构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 (!YLabels[i].EqualsDouble(0))
{
using (Pen pn = new Pen(ForeColor))
{
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
g.DrawLine(pn, DrawOrigin.X, labels[i], Width - Option.Grid.Right, labels[i]);
}
using Pen pn = new Pen(ForeColor);
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
g.DrawLine(pn, DrawOrigin.X, labels[i], Width - Option.Grid.Right, labels[i]);
}
else
{

View File

@ -418,12 +418,10 @@ namespace Sunny.UI
if (Option.XAxis.ShowGridLine)
{
using (Pen pn = new Pen(ForeColor))
{
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
g.DrawLine(pn, x, DrawOrigin.Y, x, Option.Grid.Top);
}
using Pen pn = new Pen(ForeColor);
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
g.DrawLine(pn, x, DrawOrigin.Y, x, Option.Grid.Top);
}
}
@ -464,12 +462,10 @@ namespace Sunny.UI
if (Option.YAxis.ShowGridLine)
{
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);
}
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);
}
}
@ -511,16 +507,6 @@ namespace Sunny.UI
{
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);
@ -545,49 +531,47 @@ namespace Sunny.UI
if (series.ShowLine || series.Symbol == UILinePointSymbol.None)
{
using (Pen pen = new Pen(color, series.Width))
{
pen.DashStyle = series.DashStyle;
if (series.DashPattern.IsValid()) pen.DashPattern = series.DashPattern;
g.SetHighQuality();
using Pen pen = new Pen(color, series.Width);
pen.DashStyle = series.DashStyle;
if (series.DashPattern.IsValid()) pen.DashPattern = series.DashPattern;
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>();
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();
for (int i = 0; i < series.Points.Count - 1; i++)
{
g.DrawTwoPoints(pen, series.Points[i], series.Points[i + 1], DrawRect);
}
}
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++)
{
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;
if (!series.CustomColor) color = ChartStyle.GetColor(idx);
using (Graphics graphics = bmp.Graphics())
{
DrawSeries(graphics, color, series);
}
using Graphics graphics = bmp.Graphics();
DrawSeries(graphics, color, series);
idx++;
}
@ -701,71 +683,70 @@ namespace Sunny.UI
if (series.Symbol != UILinePointSymbol.None)
{
using (Brush br = new SolidBrush(FillColor))
using (Brush br1 = new SolidBrush(color))
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;
using Brush br = new SolidBrush(FillColor);
using Brush br1 = new SolidBrush(color);
using Pen pn = new Pen(color, series.SymbolLineWidth);
switch (series.Symbol)
{
case UILinePointSymbol.Square:
g.FillRectangle(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
g.DrawRectangle(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
break;
case UILinePointSymbol.Diamond:
{
PointF pt1 = new PointF(p.X - series.SymbolSize, p.Y);
PointF pt2 = new PointF(p.X, p.Y - series.SymbolSize);
PointF pt3 = new PointF(p.X + series.SymbolSize, p.Y);
PointF pt4 = new PointF(p.X, p.Y + series.SymbolSize);
PointF[] pts = { pt1, pt2, pt3, pt4, pt1 };
g.SetHighQuality();
GraphicsPath path = pts.Path();
g.FillPath(br, path);
g.DrawPath(pn, path);
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();
GraphicsPath path = pts.Path();
g.FillPath(br, path);
g.DrawPath(pn, path);
path.Dispose();
}
break;
case UILinePointSymbol.Circle:
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)
{
case UILinePointSymbol.Square:
g.FillRectangle(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
g.DrawRectangle(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
break;
case UILinePointSymbol.Diamond:
{
PointF pt1 = new PointF(p.X - series.SymbolSize, p.Y);
PointF pt2 = new PointF(p.X, p.Y - series.SymbolSize);
PointF pt3 = new PointF(p.X + series.SymbolSize, p.Y);
PointF pt4 = new PointF(p.X, p.Y + series.SymbolSize);
PointF[] pts = { pt1, pt2, pt3, pt4, pt1 };
g.SetHighQuality();
g.FillEllipse(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
g.DrawEllipse(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
break;
case UILinePointSymbol.Round:
GraphicsPath path = pts.Path();
g.FillPath(br, path);
g.DrawPath(pn, path);
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.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;
}
GraphicsPath path = pts.Path();
g.FillPath(br, path);
g.DrawPath(pn, path);
path.Dispose();
}
break;
case UILinePointSymbol.Circle:
g.SetHighQuality();
g.FillEllipse(br, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
g.DrawEllipse(pn, p.X - series.SymbolSize, p.Y - series.SymbolSize, series.SymbolSize * 2, series.SymbolSize * 2);
break;
case UILinePointSymbol.Round:
g.SetHighQuality();
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;
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 };
}
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos);
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
}
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);
}
}
@ -808,17 +786,14 @@ namespace Sunny.UI
float pos = Y2Scale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height, Option.YDataOrder);
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 };
}
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos);
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
}
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);
}
}
@ -833,17 +808,15 @@ namespace Sunny.UI
float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width);
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 };
}
g.DrawLine(pn, pos, Height - Option.Grid.Bottom - 1, pos, Option.Grid.Top + 1);
pn.DashStyle = DashStyle.Dash;
pn.DashPattern = new float[] { 3, 3 };
}
g.DrawLine(pn, pos, Height - Option.Grid.Bottom - 1, pos, Option.Grid.Top + 1);
Size sf = TextRenderer.MeasureText(line.Name, TempFont);
float x = pos - sf.Width;
if (x < Option.Grid.Left) x = pos + 2;
@ -973,12 +946,10 @@ namespace Sunny.UI
}
else
{
using (Graphics g = this.CreateGraphics())
{
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont);
tip.Size = new Size(sf.Width + 4, sf.Height + 4);
}
using Graphics g = this.CreateGraphics();
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont);
tip.Size = new Size(sf.Width + 4, sf.Height + 4);
int x = e.Location.X + 15;
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)
{
//根据颜色和边框像素取得一条线
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)
{
//得到当前的句柄
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);
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);
}
/// <summary>

View File

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

View File

@ -115,7 +115,8 @@ namespace Sunny.UI
FrameDimension fd = Image.GifFrameDimension();
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);
}
}

View File

@ -128,13 +128,12 @@ namespace Sunny.UI
public static Bitmap ChangeOpacity(Image img, float opacity)
{
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();
matrix.Matrix33 = opacity;
ImageAttributes imgAttribute = new ImageAttributes();
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.Dispose(); // Releasing all resource used by graphics
return bmp;
}
@ -663,19 +662,15 @@ namespace Sunny.UI
/// <param name="strokeThickness"></param>
public static void DrawStrokedRectangle(this Graphics g, Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1)
{
using (var bodyBrush = new SolidBrush(bodyColor))
{
var x = strokeThickness == 1 ? 0 : strokeThickness;
var y = strokeThickness == 1 ? 0 : strokeThickness;
var h = 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);
using (var strokePen = new Pen(strokeColor, strokeThickness))
{
g.FillRectangle(bodyBrush, newRect);
g.DrawRectangle(strokePen, newRect);
}
}
using var bodyBrush = new SolidBrush(bodyColor);
var x = strokeThickness == 1 ? 0 : strokeThickness;
var y = strokeThickness == 1 ? 0 : strokeThickness;
var h = 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);
using var strokePen = new Pen(strokeColor, strokeThickness);
g.FillRectangle(bodyBrush, newRect);
g.DrawRectangle(strokePen, newRect);
}
/// <summary>
@ -688,19 +683,15 @@ namespace Sunny.UI
/// <param name="strokeThickness"></param>
public static void DrawStrokedEllipse(this Graphics g, Rectangle rect, Color bodyColor, Color strokeColor, int strokeThickness = 1)
{
using (var bodyBrush = new SolidBrush(bodyColor))
{
var x = strokeThickness == 1 ? 0 : strokeThickness;
var y = strokeThickness == 1 ? 0 : strokeThickness;
var h = 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);
using (var strokePen = new Pen(strokeColor, strokeThickness))
{
g.FillEllipse(bodyBrush, newRect);
g.DrawEllipse(strokePen, newRect);
}
}
using var bodyBrush = new SolidBrush(bodyColor);
var x = strokeThickness == 1 ? 0 : strokeThickness;
var y = strokeThickness == 1 ? 0 : strokeThickness;
var h = 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);
using var strokePen = new Pen(strokeColor, strokeThickness);
g.FillEllipse(bodyBrush, newRect);
g.DrawEllipse(strokePen, newRect);
}
/// <summary>

View File

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

View File

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

View File

@ -143,67 +143,66 @@ namespace Sunny.UI
r.Width -= Padding.Right;
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.Y = r.Top;
break;
center.X += Padding.Left;
center.Y += Padding.Top;
center.X -= Padding.Right;
center.Y -= Padding.Bottom;
case ContentAlignment.TopRight:
center.X = r.Right;
center.Y = r.Top;
break;
e.Graphics.TranslateTransform(center.X, center.Y);
e.Graphics.RotateTransform(TextAngle);
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;
}
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();
}
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, 0));
using (Brush br = new SolidBrush(SelectedColor))
{
g.FillPolygon(br, points.ToArray());
}
using Brush br = new SolidBrush(SelectedColor);
g.FillPolygon(br, points.ToArray());
g.DrawString(Text, Font, ForeColor, ClientRectangle, ContentAlignment.MiddleCenter);
}
else
@ -233,10 +230,8 @@ namespace Sunny.UI
ClickArea[index] = pts;
}
using (Brush br = new SolidBrush(index <= ItemIndex ? SelectedColor : UnSelectedColor))
{
g.FillPolygon(br, points.ToArray());
}
using Brush br = new SolidBrush(index <= ItemIndex ? SelectedColor : UnSelectedColor);
g.FillPolygon(br, points.ToArray());
g.DrawString(item.ToString(), Font, index <= ItemIndex ? ForeColor : UnSelectedForeColor,
new Rectangle(begin, 0, itemWidth, Height), ContentAlignment.MiddleCenter);

View File

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

View File

@ -835,13 +835,15 @@ namespace Sunny.UI
{
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;
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;

View File

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

View File

@ -396,13 +396,15 @@ namespace Sunny.UI
{
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;
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;

View File

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

View File

@ -166,7 +166,7 @@ namespace Sunny.UI
imageRadius = Radius;
}
Graphics g = image.Graphics();
using Graphics g = image.Graphics();
g.Clear(Color.Transparent);
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);
}
g.Dispose();
if (Direction == UILine.LineDirection.Horizontal)
{
e.Graphics.DrawImage(image,

View File

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

View File

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

View File

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

View File

@ -164,13 +164,9 @@ namespace Sunny.UI
public static Image CreateImage(int symbol, int size, Color color)
{
Bitmap image = new Bitmap(size, size);
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);
}
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);
return image;
}