diff --git a/Bin/net40/SunnyUI.Demo.exe b/Bin/net40/SunnyUI.Demo.exe index bbf3b8d9..c0f10d6f 100644 Binary files a/Bin/net40/SunnyUI.Demo.exe and b/Bin/net40/SunnyUI.Demo.exe differ diff --git a/Bin/net40/SunnyUI.dll b/Bin/net40/SunnyUI.dll index 42261dad..1c02fcbc 100644 Binary files a/Bin/net40/SunnyUI.dll and b/Bin/net40/SunnyUI.dll differ diff --git a/Bin/net462/SunnyUI.dll b/Bin/net462/SunnyUI.dll index 61da0321..33148dc2 100644 Binary files a/Bin/net462/SunnyUI.dll and b/Bin/net462/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/SunnyUI.dll b/Bin/net5.0-windows/SunnyUI.dll index ecb26907..fdd7774c 100644 Binary files a/Bin/net5.0-windows/SunnyUI.dll and b/Bin/net5.0-windows/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/ref/SunnyUI.dll b/Bin/net5.0-windows/ref/SunnyUI.dll index 713d32ba..21d46ffe 100644 Binary files a/Bin/net5.0-windows/ref/SunnyUI.dll and b/Bin/net5.0-windows/ref/SunnyUI.dll differ diff --git a/Bin/netcoreapp3.1/SunnyUI.dll b/Bin/netcoreapp3.1/SunnyUI.dll index 77cba153..e818b721 100644 Binary files a/Bin/netcoreapp3.1/SunnyUI.dll and b/Bin/netcoreapp3.1/SunnyUI.dll differ diff --git a/SunnyUI/Controls/UIPanel.cs b/SunnyUI/Controls/UIPanel.cs index 733b6704..919d8e37 100644 --- a/SunnyUI/Controls/UIPanel.cs +++ b/SunnyUI/Controls/UIPanel.cs @@ -310,7 +310,7 @@ namespace Sunny.UI if (IsDisposed) return; Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1); - GraphicsPath path = GDIEx.CreateRoundedRectanglePath(rect, radius, RadiusSides); + GraphicsPath path = rect.CreateRoundedRectanglePath(radius, RadiusSides); //填充背景色 if (ShowFill && fillColor.IsValid()) diff --git a/SunnyUI/Controls/UIValve.cs b/SunnyUI/Controls/UIValve.cs index 599b2799..b2fb320c 100644 --- a/SunnyUI/Controls/UIValve.cs +++ b/SunnyUI/Controls/UIValve.cs @@ -122,7 +122,7 @@ namespace Sunny.UI get => valveColor; set { - valveColor = value; + valveColor = value; Invalidate(); } } @@ -216,7 +216,7 @@ namespace Sunny.UI rect = new Rectangle(Width - pipeSize - 8 - 14 - 10, Height / 2 - 14, 10, 27); e.Graphics.FillRectangle(valveColor, rect); - colors = GDIEx.GradientColors(Color.White, valveColor, 14); + colors = Color.White.GradientColors(valveColor, 14); rect = new Rectangle(Width - pipeSize - 8 - 14 - 10, Height / 2 - 14 + 4, 10, 4); e.Graphics.FillRectangle(colors[4], rect); rect = new Rectangle(Width - pipeSize - 8 - 14 - 10, Height / 2 - 14 + 12, 10, 4); @@ -269,7 +269,7 @@ namespace Sunny.UI rect = new Rectangle(Width / 2 - 14, pipeSize + 8 + 10 + 4, 27, 10); e.Graphics.FillRectangle(valveColor, rect); - colors = GDIEx.GradientColors(Color.White, valveColor, 14); + colors = Color.White.GradientColors(valveColor, 14); rect = new Rectangle(Width / 2 - 14 + 4, pipeSize + 8 + 10 + 4, 4, 10); e.Graphics.FillRectangle(colors[4], rect); rect = new Rectangle(Width / 2 - 14 + 12, pipeSize + 8 + 10 + 4, 4, 10); @@ -321,7 +321,7 @@ namespace Sunny.UI rect = new Rectangle(pipeSize + 8 + 10 + 4, Height / 2 - 14, 10, 27); e.Graphics.FillRectangle(valveColor, rect); - colors = GDIEx.GradientColors(Color.White, valveColor, 14); + colors = Color.White.GradientColors(valveColor, 14); rect = new Rectangle(pipeSize + 8 + 10 + 4, Height / 2 - 14 + 4, 10, 4); e.Graphics.FillRectangle(colors[4], rect); rect = new Rectangle(pipeSize + 8 + 10 + 4, Height / 2 - 14 + 12, 10, 4); @@ -373,7 +373,7 @@ namespace Sunny.UI rect = new Rectangle(Width / 2 - 14, Height - pipeSize - 8 - 14 - 10, 27, 10); e.Graphics.FillRectangle(valveColor, rect); - colors = GDIEx.GradientColors(Color.White, valveColor, 14); + colors = Color.White.GradientColors(valveColor, 14); rect = new Rectangle(Width / 2 - 14 + 4, Height - pipeSize - 8 - 14 - 10, 4, 10); e.Graphics.FillRectangle(colors[4], rect); rect = new Rectangle(Width / 2 - 14 + 12, Height - pipeSize - 8 - 14 - 10, 4, 10); diff --git a/SunnyUI/Forms/UIForm.cs b/SunnyUI/Forms/UIForm.cs index 4e69b13a..aad71f55 100644 --- a/SunnyUI/Forms/UIForm.cs +++ b/SunnyUI/Forms/UIForm.cs @@ -598,7 +598,7 @@ namespace Sunny.UI size = Size; // 若窗体从正常模式->最大化模式,该操作是由移动窗体至顶部触发的,记录的是移动前的窗体位置 location = IsOnMoving ? FormLocation : Location; - GDIEx.SetFormRoundRectRegion(this, 0); + FormEx.SetFormRoundRectRegion(this, 0); WindowState = FormWindowState.Maximized; } else if (WindowState == FormWindowState.Maximized) @@ -620,7 +620,7 @@ namespace Sunny.UI } Location = location; - GDIEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); + FormEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); WindowState = FormWindowState.Normal; } @@ -1371,11 +1371,11 @@ namespace Sunny.UI if (WindowState == FormWindowState.Maximized) { - GDIEx.SetFormRoundRectRegion(this, 0); + FormEx.SetFormRoundRectRegion(this, 0); } else { - GDIEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); + FormEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); } Invalidate(); diff --git a/SunnyUI/Forms/UIFormHelper.cs b/SunnyUI/Forms/UIFormHelper.cs index 304f413d..227089cc 100644 --- a/SunnyUI/Forms/UIFormHelper.cs +++ b/SunnyUI/Forms/UIFormHelper.cs @@ -602,6 +602,21 @@ namespace Sunny.UI { return new UIEditForm(option); } + + /// + /// 设置窗体的圆角矩形 + /// + /// 需要设置的窗体 + /// 圆角矩形的半径 + public static void SetFormRoundRectRegion(Form form, int rgnRadius) + { + if (form != null && form.FormBorderStyle == FormBorderStyle.None) + { + int region = Win32.GDI.CreateRoundRectRgn(0, 0, form.Width + 1, form.Height + 1, rgnRadius, rgnRadius); + Win32.User.SetWindowRgn(form.Handle, region, true); + Win32.GDI.DeleteObject(region); + } + } } public interface IFrame diff --git a/SunnyUI/Static/UGDI.cs b/SunnyUI/Static/UGDI.cs index 7a584807..7cb4b8bd 100644 --- a/SunnyUI/Static/UGDI.cs +++ b/SunnyUI/Static/UGDI.cs @@ -17,18 +17,17 @@ * 创建日期: 2020-01-01 * * 2020-01-01: V2.2.0 增加文件说明 - * 2021-08-19: V3.0.5 修复CreateRoundedRectanglePath参数radius=0时的错误 + * 2021-08-19: V3.0.6 修复CreateRoundedRectanglePath参数radius=0时的错误 + * 2021-08-20: V3.0.6 整理了一些GDI绘图的常用方法扩展 ******************************************************************************/ using System; -using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; -using System.Windows.Forms; namespace Sunny.UI { - public static class GDIEx + public static class GDI { /// /// 点是否在区域内 @@ -114,21 +113,6 @@ namespace Sunny.UI return region; } - /// - /// 设置窗体的圆角矩形 - /// - /// 需要设置的窗体 - /// 圆角矩形的半径 - public static void SetFormRoundRectRegion(Form form, int rgnRadius) - { - if (form != null && form.FormBorderStyle == FormBorderStyle.None) - { - int region = Win32.GDI.CreateRoundRectRgn(0, 0, form.Width + 1, form.Height + 1, rgnRadius, rgnRadius); - Win32.User.SetWindowRgn(form.Handle, region, true); - Win32.GDI.DeleteObject(region); - } - } - public static PointF Center(this Rectangle rect) { return new PointF(rect.Left + rect.Width / 2.0f, rect.Top + rect.Height / 2.0f); @@ -174,70 +158,7 @@ namespace Sunny.UI return Graphics().MeasureString(text, font); } - /// - /// 九宫切图背景填充,#,http://st233.com/blog.php?id=24 - /// 例如按钮是图片分成九个区域 然后只需要将四角填充到目标区域 其余的拉伸就可以了 - /// - /// - /// - /// - /// - public static void DrawImageWithNineCut(this Graphics g, Image img, Rectangle rect, int angleSize = 5) - { - //填充四个角 - g.DrawImage(img, new Rectangle(rect.X, rect.Y, angleSize, angleSize), - new Rectangle(0, 0, angleSize, angleSize), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(rect.Right - angleSize, rect.Y, angleSize, angleSize), - new Rectangle(img.Width - angleSize, 0, angleSize, angleSize), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(rect.X, rect.Bottom - angleSize, angleSize, angleSize), - new Rectangle(0, img.Height - angleSize, angleSize, angleSize), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(rect.Right - angleSize, rect.Bottom - angleSize, angleSize, angleSize), - new Rectangle(img.Width - angleSize, img.Height - angleSize, angleSize, angleSize), GraphicsUnit.Pixel); - //四边 - g.DrawImage(img, new Rectangle(rect.X, rect.Y + angleSize, angleSize, rect.Height - angleSize * 2), - new Rectangle(0, angleSize, angleSize, img.Height - angleSize * 2), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(rect.X + angleSize, rect.Y, rect.Width - angleSize * 2, angleSize), - new Rectangle(angleSize, 0, img.Width - angleSize * 2, angleSize), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(rect.Right - angleSize, rect.Y + angleSize, angleSize, rect.Height - angleSize * 2), - new Rectangle(img.Width - angleSize, angleSize, angleSize, img.Height - angleSize * 2), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(rect.X + angleSize, rect.Bottom - angleSize, rect.Width - angleSize * 2, angleSize), - new Rectangle(angleSize, img.Height - angleSize, img.Width - angleSize * 2, angleSize), GraphicsUnit.Pixel); - //中间 - g.DrawImage(img, - new Rectangle(rect.X + angleSize, rect.Y + angleSize, rect.Width - angleSize * 2, rect.Height - angleSize * 2), - new Rectangle(angleSize, angleSize, img.Width - angleSize * 2, img.Height - angleSize * 2), GraphicsUnit.Pixel); - } - - public static void DrawImageWithNineCut(this Graphics g, Image img, int destWidth, int destHeight, int cutLeft, int cutRight, int cutTop, int cutBottom, int iZoom = 1) - { - iZoom = Math.Max(1, iZoom); - - //填充四个角 - g.DrawImage(img, new Rectangle(0, 0, cutLeft * iZoom, cutTop * iZoom), - new Rectangle(0, 0, cutLeft, cutTop), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(destWidth - cutRight * iZoom, 0, cutRight * iZoom, cutTop * iZoom), - new Rectangle(img.Width - cutRight, 0, cutRight, cutTop), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(0, destHeight - cutBottom * iZoom, cutLeft * iZoom, cutBottom * iZoom), - new Rectangle(0, img.Height - cutBottom, cutLeft, cutBottom), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(destWidth - cutRight * iZoom, destHeight - cutBottom * iZoom, cutRight * iZoom, cutBottom * iZoom), - new Rectangle(img.Width - cutRight, img.Height - cutBottom, cutRight, cutBottom), GraphicsUnit.Pixel); - - //四边 - g.DrawImage(img, new Rectangle(cutLeft * iZoom, 0, destWidth - (cutLeft + cutRight) * iZoom, cutTop * iZoom), - new Rectangle(cutLeft, 0, img.Width - cutLeft - cutRight, cutTop), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(0, cutTop * iZoom, cutLeft * iZoom, destHeight - (cutTop + cutBottom) * iZoom), - new Rectangle(0, cutTop, cutLeft, img.Height - cutTop - cutBottom), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(destWidth - cutRight * iZoom, cutTop * iZoom, cutRight * iZoom, destHeight - (cutTop + cutBottom) * iZoom), - new Rectangle(img.Width - cutRight, cutTop, cutRight, img.Height - cutTop - cutBottom), GraphicsUnit.Pixel); - g.DrawImage(img, new Rectangle(cutLeft * iZoom, destHeight - cutBottom * iZoom, destWidth - (cutLeft + cutRight) * iZoom, cutBottom * iZoom), - new Rectangle(cutLeft, img.Height - cutBottom, img.Width - cutLeft - cutRight, cutBottom), GraphicsUnit.Pixel); - - //中间 - g.DrawImage(img, new Rectangle(cutLeft * iZoom, cutTop * iZoom, destWidth - (cutLeft + cutRight) * iZoom, destHeight - (cutTop + cutBottom) * iZoom), - new Rectangle(cutLeft, cutTop, img.Width - cutLeft - cutRight, img.Height - cutTop - cutBottom), GraphicsUnit.Pixel); - } - - public static Color[] GradientColors(Color startColor, Color endColor, int count) + public static Color[] GradientColors(this Color startColor, Color endColor, int count) { count = Math.Max(count, 2); Bitmap image = new Bitmap(1024, 3); @@ -300,387 +221,6 @@ namespace Sunny.UI } } - public static void DrawString(this Graphics g, string text, Font font, Color color, int x, int y) - { - using (Brush br = new SolidBrush(color)) - { - g.DrawString(text, font, br, x, y); - } - } - - public static void DrawString(this Graphics g, string text, Font font, Color color, RectangleF rect, StringFormat format) - { - using (Brush br = new SolidBrush(color)) - { - g.DrawString(text, font, br, rect, format); - } - } - - public static void DrawString(this Graphics g, string text, Font font, Color color, Point pt) - { - g.DrawString(text, font, color, pt.X, pt.Y); - } - - public static void DrawString(this Graphics g, string text, Font font, Color color, float x, float y) - { - using (Brush br = new SolidBrush(color)) - { - g.DrawString(text, font, br, x, y); - } - } - - public static void DrawString(this Graphics g, string text, Font font, Color color, PointF pt) - { - g.DrawString(text, font, color, pt.X, pt.Y); - } - - public static void DrawLines(this Graphics g, Color color, Point[] points, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawLines(pen, points); - g.Smooth(false); - } - } - - public static void DrawLines(this Graphics g, Color color, PointF[] points, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawLines(pen, points); - g.Smooth(false); - } - } - - public static void DrawCurve(this Graphics g, Color color, Point[] points, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawCurve(pen, points); - g.Smooth(false); - } - } - - public static void DrawCurve(this Graphics g, Color color, PointF[] points, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawCurve(pen, points); - g.Smooth(false); - } - } - - public static void DrawLine(this Graphics g, Color color, int x1, int y1, int x2, int y2, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawLine(pen, x1, y1, x2, y2); - g.Smooth(false); - } - } - - public static void DrawLine(this Graphics g, Color color, Point pt1, Point pt2, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawLine(pen, pt1, pt2); - g.Smooth(false); - } - } - - public static void DrawLine(this Graphics g, Color color, float x1, float y1, float x2, float y2, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawLine(pen, x1, y1, x2, y2); - g.Smooth(false); - } - } - - public static void DrawLine(this Graphics g, Color color, PointF pt1, PointF pt2, bool smooth = false, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawLine(pen, pt1, pt2); - g.Smooth(false); - } - } - - public static void DrawArc(this Graphics g, Color color, int x, int y, int width, int height, int startAngle, int sweepAngle, bool smooth = true, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawArc(pen, x, y, width, height, startAngle, sweepAngle); - g.Smooth(false); - } - } - - public static void DrawArc(this Graphics g, Color color, float x, float y, float width, float height, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) - { - using (Pen pen = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawArc(pen, x, y, width, height, startAngle, sweepAngle); - g.Smooth(false); - } - } - - public static void FillPath(this Graphics g, Color color, GraphicsPath path, bool smooth = true) - { - using (SolidBrush sb = new SolidBrush(color)) - { - g.Smooth(smooth); - g.FillPath(sb, path); - g.Smooth(false); - } - } - - public static void DrawPath(this Graphics g, Color color, GraphicsPath path, bool smooth = true, float penWidth = 1) - { - using (Pen pn = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawPath(pn, path); - g.Smooth(false); - } - } - - public static void FillPolygon(this Graphics g, Color color, PointF[] points, bool smooth = true) - { - using (SolidBrush sb = new SolidBrush(color)) - { - g.Smooth(smooth); - g.FillPolygon(sb, points); - g.Smooth(false); - } - } - - public static void DrawPolygon(this Graphics g, Color color, PointF[] points, bool smooth = true) - { - using (Pen pn = new Pen(color)) - { - g.Smooth(smooth); - g.DrawPolygon(pn, points); - g.Smooth(false); - } - } - - public static void FillEllipse(this Graphics g, Color color, Rectangle rect, bool smooth = true) - { - using (SolidBrush sb = new SolidBrush(color)) - { - g.Smooth(smooth); - g.FillEllipse(sb, rect); - g.Smooth(false); - } - } - - public static void DrawEllipse(this Graphics g, Color color, Rectangle rect, bool smooth = true, float penWidth = 1) - { - using (Pen pn = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawEllipse(pn, rect); - g.Smooth(false); - } - } - - public static void FillEllipse(this Graphics g, Color color, RectangleF rect, bool smooth = true) - { - using (SolidBrush sb = new SolidBrush(color)) - { - g.Smooth(smooth); - g.FillEllipse(sb, rect); - g.Smooth(false); - } - } - - public static void DrawEllipse(this Graphics g, Color color, RectangleF rect, bool smooth = true, float penWidth = 1) - { - using (Pen pn = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawEllipse(pn, rect); - g.Smooth(false); - } - } - - public static void FillEllipse(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = true) - { - g.FillEllipse(color, new Rectangle(left, top, width, height), smooth); - } - - public static void DrawEllipse(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = true) - { - g.DrawEllipse(color, new Rectangle(left, top, width, height), smooth); - } - - public static void FillEllipse(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = true) - { - g.FillEllipse(color, new RectangleF(left, top, width, height), smooth); - } - - public static void DrawEllipse(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = true) - { - g.DrawEllipse(color, new RectangleF(left, top, width, height), smooth); - } - - public static void FillRectangle(this Graphics g, Color color, Rectangle rect, bool smooth = false) - { - using (SolidBrush sb = new SolidBrush(color)) - { - g.Smooth(smooth); - g.FillRectangle(sb, rect); - g.Smooth(false); - } - } - - public static void DrawRectangle(this Graphics g, Color color, Rectangle rect, bool smooth = false, float penWidth = 1) - { - using (Pen pn = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawRectangle(pn, rect); - g.Smooth(false); - } - } - - public static void FillRectangle(this Graphics g, Color color, RectangleF rect, bool smooth = false) - { - using (SolidBrush sb = new SolidBrush(color)) - { - g.Smooth(smooth); - g.FillRectangle(sb, rect); - g.Smooth(false); - } - } - - public static void DrawRectangle(this Graphics g, Color color, RectangleF rect, bool smooth = false, float penWidth = 1) - { - using (Pen pn = new Pen(color, penWidth)) - { - g.Smooth(smooth); - g.DrawRectangle(pn, rect.X, rect.Y, rect.Width, rect.Height); - g.Smooth(false); - } - } - - public static void FillRectangle(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = false) - { - g.FillRectangle(color, new Rectangle(left, top, width, height), smooth); - } - - public static void DrawRectangle(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = false) - { - g.DrawRectangle(color, new Rectangle(left, top, width, height), smooth); - } - - public static void FillRectangle(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = false) - { - g.FillRectangle(color, new RectangleF(left, top, width, height), smooth); - } - - public static void DrawRectangle(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = false) - { - g.DrawRectangle(color, new RectangleF(left, top, width, height), smooth); - } - - public static void DrawRoundRectangle(this Graphics g, Pen pen, Rectangle rect, int cornerRadius, bool smooth = true) - { - if (cornerRadius > 0) - { - using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius)) - { - g.Smooth(smooth); - g.DrawPath(pen, path); - g.Smooth(false); - } - } - else - { - g.Smooth(smooth); - g.DrawRectangle(pen, rect); - g.Smooth(false); - } - } - - public static void FillRoundRectangle(this Graphics g, Brush brush, Rectangle rect, int cornerRadius, bool smooth = true) - { - if (cornerRadius > 0) - { - using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius)) - { - g.Smooth(smooth); - g.FillPath(brush, path); - g.Smooth(false); - } - } - else - { - g.Smooth(smooth); - g.FillRectangle(brush, rect); - g.Smooth(false); - } - } - - public static void DrawRoundRectangle(this Graphics g, Pen pen, int left, int top, int width, int height, int cornerRadius, bool smooth = true) - { - g.DrawRoundRectangle(pen, new Rectangle(left, top, width, height), cornerRadius, smooth); - } - - public static void FillRoundRectangle(this Graphics g, Brush brush, int left, int top, int width, int height, int cornerRadius, bool smooth = true) - { - g.FillRoundRectangle(brush, new Rectangle(left, top, width, height), cornerRadius, smooth); - } - - public static void DrawRoundRectangle(this Graphics g, Color color, Rectangle rect, int cornerRadius, bool smooth = true) - { - if (cornerRadius > 0) - { - using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius)) - { - g.DrawPath(color, path, smooth); - } - } - else - { - g.DrawRectangle(color, rect, smooth); - } - } - - public static void FillRoundRectangle(this Graphics g, Color color, Rectangle rect, int cornerRadius, bool smooth = true) - { - if (cornerRadius > 0) - { - using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius)) - { - g.FillPath(color, path, smooth); - } - } - else - { - g.FillRectangle(color, rect, smooth); - } - } - - public static void DrawRoundRectangle(this Graphics g, Color color, int left, int top, int width, int height, int cornerRadius, bool smooth = true) - { - g.DrawRoundRectangle(color, new Rectangle(left, top, width, height), cornerRadius, smooth); - } - - public static void FillRoundRectangle(this Graphics g, Color color, int left, int top, int width, int height, int cornerRadius, bool smooth = true) - { - g.FillRoundRectangle(color, new Rectangle(left, top, width, height), cornerRadius, smooth); - } public static GraphicsPath CreateRoundedRectanglePath(this Graphics g, Rectangle rect, int radius, UICornerRadiusSides radiusSides) { @@ -698,13 +238,7 @@ namespace Sunny.UI if (radiusSides == UICornerRadiusSides.None || radius == 0) { - Point[] points = new Point[] { - new Point(rect.Left, rect.Top), - new Point(rect.Right, rect.Top), - new Point(rect.Right, rect.Bottom), - new Point(rect.Left, rect.Bottom), - new Point(rect.Left, rect.Top) }; - path = points.Path(); + path = rect.GraphicsPath(); } else { @@ -722,6 +256,17 @@ namespace Sunny.UI return path; } + public static GraphicsPath GraphicsPath(this Rectangle rect) + { + Point[] points = new Point[] { + new Point(rect.Left, rect.Top), + new Point(rect.Right, rect.Top), + new Point(rect.Right, rect.Bottom), + new Point(rect.Left, rect.Bottom), + new Point(rect.Left, rect.Top) }; + return points.Path(); + } + public static GraphicsPath CreateFanPath(this Graphics g, Point center, float d1, float d2, float startAngle, float sweepAngle) { return center.CreateFanPath(d1, d2, startAngle, sweepAngle); @@ -746,165 +291,13 @@ namespace Sunny.UI return path; } - public static void DrawCross(this Graphics g, Color color, Point center, int size = 3) - { - g.DrawLine(color, center.X - size, center.Y, center.X + size, center.Y); - g.DrawLine(color, center.X, center.Y - size, center.X, center.Y + size); - } - - public static void DrawFan(this Graphics g, Color color, Point center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true) - { - if (d1.Equals(0)) - { - g.DrawPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle); - } - else - { - GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); - g.DrawPath(color, path, smooth); - path.Dispose(); - } - } - - public static void DrawFan(this Graphics g, Color color, PointF center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true) - { - if (d1.Equals(0)) - { - g.DrawPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle); - } - else - { - GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); - g.DrawPath(color, path, smooth); - path.Dispose(); - } - } - - public static void FillFan(this Graphics g, Color color, Point center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true) - { - if (d1.Equals(0)) - { - g.FillPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle); - } - else - { - GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); - g.FillPath(color, path, smooth); - path.Dispose(); - } - } - - public static void FillFan(this Graphics g, Color color, PointF center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true) - { - if (d1.Equals(0)) - { - g.FillPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle); - } - else - { - GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); - g.FillPath(color, path, smooth); - path.Dispose(); - } - } - - public static void FillPie(this Graphics g, Color color, int x, int y, int width, int height, float startAngle, float sweepAngle, bool smooth = true) - { - g.Smooth(smooth); - using (Brush br = new SolidBrush(color)) - { - g.FillPie(br, x, y, width, height, startAngle, sweepAngle); - } - - g.Smooth(false); - } - - public static void FillPie(this Graphics g, Color color, Rectangle rect, float startAngle, float sweepAngle, bool smooth = true) - { - g.FillPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth); - } - - public static void FillPie(this Graphics g, Color color, float x, float y, float width, float height, float startAngle, float sweepAngle, bool smooth = true) - { - g.Smooth(smooth); - using (Brush br = new SolidBrush(color)) - { - g.FillPie(br, x, y, width, height, startAngle, sweepAngle); - } - - g.Smooth(false); - } - - public static void FillPie(this Graphics g, Color color, RectangleF rect, float startAngle, float sweepAngle, bool smooth = true) - { - g.FillPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth); - } - - public static void DrawPie(this Graphics g, Color color, int x, int y, int width, int height, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) - { - g.Smooth(smooth); - using (Pen pen = new Pen(color, penWidth)) - { - g.DrawPie(pen, x, y, width, height, startAngle, sweepAngle); - } - - g.Smooth(false); - } - - public static void DrawPoint(this Graphics g, Color color, int x, int y, float size) - { - g.FillEllipse(color, x - size / 2.0f, y - size / 2.0f, size, size); - } - - public static void DrawPoint(this Graphics g, Color color, float x, float y, float size) - { - g.FillEllipse(color, x - size / 2.0f, y - size / 2.0f, size, size); - } - - public static void DrawPoint(this Graphics g, Color color, Point point, float size) - { - g.DrawPoint(color, point.X, point.Y, size); - } - - public static void DrawPoint(this Graphics g, Color color, PointF point, float size) - { - g.DrawPoint(color, point.X, point.Y, size); - } - - public static void DrawPie(this Graphics g, Color color, Rectangle rect, float startAngle, float sweepAngle, bool smooth = true) - { - g.DrawPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth); - } - - public static void DrawPie(this Graphics g, Color color, float x, float y, float width, float height, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) - { - g.Smooth(smooth); - using (Pen pen = new Pen(color, penWidth)) - { - g.DrawPie(pen, x, y, width, height, startAngle, sweepAngle); - } - - g.Smooth(false); - } - - public static void DrawPie(this Graphics g, Color color, RectangleF rect, float startAngle, float sweepAngle, bool smooth = true) - { - g.DrawPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth); - } - public static GraphicsPath CreateRoundedRectanglePath(this Rectangle rect, int radius, bool cornerLeftTop = true, bool cornerRightTop = true, bool cornerRightBottom = true, bool cornerLeftBottom = true) { GraphicsPath path = new GraphicsPath(); if ((!cornerLeftTop && !cornerRightTop && !cornerRightBottom && !cornerLeftBottom) || radius <= 0) { - Point[] points = new Point[] { - new Point(rect.Left, rect.Top), - new Point(rect.Right, rect.Top), - new Point(rect.Right, rect.Bottom), - new Point(rect.Left, rect.Bottom), - new Point(rect.Left, rect.Top) }; - path = points.Path(); + path = rect.GraphicsPath(); } else { @@ -934,157 +327,6 @@ namespace Sunny.UI return path; } - public static void DrawString(this Graphics g, string str, Font font, Color color, Size size, Padding padding, ContentAlignment align) - { - SizeF sf = g.MeasureString(str, font); - using (Brush br = new SolidBrush(color)) - { - switch (align) - { - case ContentAlignment.MiddleCenter: - g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, - padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); - break; - - case ContentAlignment.TopLeft: - g.DrawString(str, font, br, padding.Left, padding.Top); - break; - - case ContentAlignment.TopCenter: - g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, padding.Top); - break; - - case ContentAlignment.TopRight: - g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, padding.Top); - break; - - case ContentAlignment.MiddleLeft: - g.DrawString(str, font, br, padding.Left, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); - break; - - case ContentAlignment.MiddleRight: - g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); - break; - - case ContentAlignment.BottomLeft: - g.DrawString(str, font, br, padding.Left, size.Height - sf.Height - padding.Bottom); - break; - - case ContentAlignment.BottomCenter: - g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, size.Height - sf.Height - padding.Bottom); - break; - - case ContentAlignment.BottomRight: - g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, size.Height - sf.Height - padding.Bottom); - break; - } - } - } - - public static void DrawString(this Graphics g, string s, Font font, Color color, - RectangleF rect, StringFormat format, float angle) - { - using (Brush br = new SolidBrush(color)) - { - g.DrawStringRotateAtCenter(s, font, color, rect.Center(), (int)angle); - //g.DrawString(s, font, br, layoutRectangle, format, angle); - } - } - - /// - /// 以文字中心点为原点,旋转文字 - /// - /// Graphics - /// 文字 - /// 字体 - /// 颜色 - /// 文字中心点 - /// 角度 - public static void DrawStringRotateAtCenter(this Graphics g, string text, Font font, Color color, PointF centerPoint, float angle) - { - using (Brush br = new SolidBrush(color)) - { - g.DrawStringRotateAtCenter(text, font, br, centerPoint, angle); - } - } - - /// - /// 以文字中心点为原点,旋转文字 - /// - /// Graphics - /// 文字 - /// 字体 - /// 笔刷 - /// 文字中心点 - /// 角度 - public static void DrawStringRotateAtCenter(this Graphics g, string text, Font font, Brush brush, PointF centerPoint, float angle) - { - SizeF sf = g.MeasureString(text, font); - float x1 = centerPoint.X - sf.Width / 2.0f; - float y1 = centerPoint.Y - sf.Height / 2.0f; - - // 把画板的原点(默认是左上角)定位移到文字中心 - g.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2); - // 旋转画板 - g.RotateTransform(angle); - // 回退画板x,y轴移动过的距离 - g.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2)); - g.DrawString(text, font, brush, x1, y1); - - //恢复 - g.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2); - g.RotateTransform(-angle); - g.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2)); - } - - /// - /// 以旋转点为原点,旋转文字 - /// - /// Graphics - /// 文本 - /// 字体 - /// 填充 - /// 旋转点 - /// 布局方式 - /// 角度 - public static void DrawString(this Graphics g, string text, Font font, Brush brush, PointF rotatePoint, StringFormat format, float angle) - { - // Save the matrix - Matrix mtxSave = g.Transform; - - Matrix mtxRotate = g.Transform; - mtxRotate.RotateAt(angle, rotatePoint); - g.Transform = mtxRotate; - - g.DrawString(text, font, brush, rotatePoint, format); - - // Reset the matrix - g.Transform = mtxSave; - } - - public static void DrawString(this Graphics g, string s, Font font, Color color, PointF rotatePoint, StringFormat format, float angle) - { - using (Brush br = new SolidBrush(color)) - { - g.DrawString(s, font, br, rotatePoint, format, angle); - } - } - - /// - /// 绘制根据矩形旋转文本 - /// - /// Graphics - /// 文本 - /// 字体 - /// 填充 - /// 局部矩形 - /// 布局方式 - /// 角度 - public static void DrawString(this Graphics g, string text, Font font, Brush brush, RectangleF rect, StringFormat format, float angle) - { - g.DrawStringRotateAtCenter(text, font, brush, rect.Center(), angle); - } - /// /// 两个矩阵是否重叠(边沿重叠,也认为是重叠) /// @@ -1177,153 +419,6 @@ namespace Sunny.UI return (y - b) * 1.0 / a; } - - public static void DrawTwoPoints(this Graphics g, Color color, float width, PointF pf1, PointF pf2, Rectangle rect) - { - using (Pen pen = new Pen(color, width)) - { - DrawTwoPoints(g, pen, pf1, pf2, rect); - } - } - - public static void DrawTwoPoints(this Graphics g, Pen pen, PointF pf1, PointF pf2, Rectangle rect) - { - bool haveLargePixel = Math.Abs(pf1.X - pf2.X) >= rect.Width * 100 || Math.Abs(pf1.Y - pf2.Y) >= rect.Height * 100; - - //两点都在区域内 - if (pf1.InRect(rect) && pf2.InRect(rect)) - { - g.DrawLine(pen, pf1, pf2); - return; - } - - //无大坐标像素 - if (!haveLargePixel) - { - g.DrawLine(pen, pf1, pf2); - return; - } - - //垂直线 - if (pf1.X.Equals(pf2.X)) - { - if (pf1.X <= rect.Left) return; - if (pf1.X >= rect.Right) return; - if (pf1.Y <= rect.Top && pf2.Y <= rect.Top) return; - if (pf1.Y >= rect.Bottom && pf2.Y >= rect.Bottom) return; - - float yy1 = Math.Min(pf1.Y, pf2.Y); - float yy2 = Math.Max(pf1.Y, pf2.Y); - if (yy1 <= rect.Top) - { - if (yy2 <= rect.Bottom) g.DrawLine(pen, pf1.X, rect.Top, pf1.X, yy2); - else g.DrawLine(pen, pf1.X, rect.Top, pf1.X, rect.Bottom); - } - else - { - if (yy2 <= rect.Bottom) g.DrawLine(pen, pf1.X, yy1, pf1.X, yy2); - else g.DrawLine(pen, pf1.X, yy1, pf1.X, rect.Bottom); - } - - return; - } - - //水平线 - if (pf1.Y.Equals(pf2.Y)) - { - if (pf1.Y <= rect.Top) return; - if (pf1.Y >= rect.Bottom) return; - if (pf1.X <= rect.Left && pf2.X <= rect.Left) return; - if (pf1.X >= rect.Right && pf2.X >= rect.Right) return; - - float xx1 = Math.Min(pf1.X, pf2.X); - float xx2 = Math.Max(pf1.X, pf2.X); - if (xx1 <= rect.Left) - { - if (xx2 <= rect.Right) g.DrawLine(pen, rect.Left, pf1.Y, xx2, pf1.Y); - else g.DrawLine(pen, rect.Left, pf1.Y, rect.Right, pf1.Y); - } - else - { - if (xx2 <= rect.Right) g.DrawLine(pen, xx1, pf1.Y, xx2, pf1.Y); - else g.DrawLine(pen, xx1, pf1.Y, rect.Right, pf1.Y); - } - - return; - } - - //判断两个区域是否相交 - RectangleF rect1 = pf1.CreateRectangleF(pf2); - if (!rect1.IsOverlap(rect)) return; - - double x1 = CalcX(pf1, pf2, rect.Top); - double x2 = CalcX(pf1, pf2, rect.Bottom); - double y1 = CalcY(pf1, pf2, rect.Left); - double y2 = CalcY(pf1, pf2, rect.Right); - - //判断线段是否和区域有交点 - bool isExist = x1.InRange(rect.Left, rect.Right) || x2.InRange(rect.Left, rect.Right) || y1.InRange(rect.Top, rect.Bottom) || y2.InRange(rect.Top, rect.Bottom); - if (!isExist) return; - - List TwoPoints = new List(); - if (!pf1.InRect(rect) && !pf2.InRect(rect)) - { - if (x1.InRange(rect.Left, rect.Right)) TwoPoints.Add(new PointF((float)x1, rect.Top)); - if (x2.InRange(rect.Left, rect.Right)) TwoPoints.Add(new PointF((float)x2, rect.Bottom)); - if (y1.InRange(rect.Top, rect.Bottom)) TwoPoints.Add(new PointF(rect.Left, (float)y1)); - if (y2.InRange(rect.Top, rect.Bottom)) TwoPoints.Add(new PointF(rect.Right, (float)y2)); - } - else - { - PointF center = pf1.InRect(rect) ? pf1 : pf2; - PointF border = pf2.InRect(rect) ? pf1 : pf2; - TwoPoints.Add(center); - if (border.X >= center.X) - { - if (border.Y >= center.Y) - { - TwoPoints.Add(x2 <= rect.Right ? new PointF((float)x2, rect.Bottom) : new PointF(rect.Right, (float)y2)); - } - else - { - TwoPoints.Add(x1 <= rect.Right ? new PointF((float)x1, rect.Top) : new PointF(rect.Right, (float)y2)); - } - } - else - { - if (border.Y >= center.Y) - { - TwoPoints.Add(x2 >= rect.Left ? new PointF((float)x2, rect.Bottom) : new PointF(rect.Left, (float)y1)); - } - else - { - TwoPoints.Add(x1 >= rect.Left ? new PointF((float)x1, rect.Bottom) : new PointF(rect.Left, (float)y1)); - } - } - } - - if (TwoPoints.Count == 2) - { - g.DrawLine(pen, TwoPoints[0], TwoPoints[1]); - } - else - { - Console.WriteLine(TwoPoints.Count); - } - - TwoPoints.Clear(); - } - - public static bool IsNullOrEmpty(this Color color) - { - return color == Color.Empty || color == Color.Transparent; - } - - public static bool IsValid(this Color color) - { - return !color.IsNullOrEmpty(); - } - /// /// 设置GDI高质量模式抗锯齿 /// @@ -1346,133 +441,64 @@ namespace Sunny.UI g.CompositingQuality = CompositingQuality.Default; } - /// - /// 设置递进颜色 - /// - /// 颜色 - /// alpha - /// 颜色 - public static Color StepColor(this Color color, int alpha) + public static string ToHTML(this Color color) { - if (alpha == 100) - { - return color; - } - - byte a = color.A; - byte r = color.R; - byte g = color.G; - byte b = color.B; - float bg; - - int _alpha = Math.Max(alpha, 0); - double d = (_alpha - 100.0) / 100.0; - - if (d > 100) - { - // blend with white - bg = 255.0F; - d = 1.0F - d; // 0 = transparent fg; 1 = opaque fg - } - else - { - // blend with black - bg = 0.0F; - d = 1.0F + d; // 0 = transparent fg; 1 = opaque fg - } - - r = (byte)(BlendColor(r, bg, d)); - g = (byte)(BlendColor(g, bg, d)); - b = (byte)(BlendColor(b, bg, d)); - - return Color.FromArgb(a, r, g, b); + return ColorTranslator.ToHtml(color); } - private static double BlendColor(double fg, double bg, double alpha) + public static Color ToColor(this string htmlColor, int alpha = 255) { - double result = bg + (alpha * (fg - bg)); - if (result < 0.0) - { - result = 0.0; - } - - if (result > 255) - { - result = 255; - } - - return result; + return Color.FromArgb(alpha > 255 ? 255 : alpha, ColorTranslator.FromHtml(htmlColor)); } - public static Color RandomColor() + public static bool IsNullOrEmpty(this Color color) { - Random random = new Random(); - return Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + return color == Color.Empty || color == Color.Transparent; } - /// - /// 平移 - /// - public static Point Offset(this Point point, Size size) + public static bool IsValid(this Color color) { - point.Offset(size.Width, size.Height); - return point; + return !color.IsNullOrEmpty(); } - /// - /// 宽度放大 - /// - public static Size MultiplyWidth(this Size size, int multiple) + public static SolidBrush Brush(this Color color) { - return new Size(size.Width * multiple, size.Height); + return new SolidBrush(color); } - /// - /// 高度放大 - /// - public static Size MultiplyHeight(this Size size, int multiple) + public static Pen Pen(this Color color, float size = 1) { - return new Size(size.Width, size.Height * multiple); + return new Pen(color, size); } - /// - /// 宽度和高度放大 - /// - public static Size MultiplyAll(this Size size, int multiple) + public static SolidBrush Brush(this string htmlColor, int alpha = 255) { - return new Size(size.Width * multiple, size.Height * multiple); + return new SolidBrush(Color.FromArgb(alpha > 255 ? 255 : alpha, ColorTranslator.FromHtml(htmlColor))); } - /// - /// 宽度放大 - /// - public static Size MultiplyWidth(this Size size, double multiple) + public static Pen Pen(this string htmlColor, int alpha = 255, float size = 1, LineCap startCap = LineCap.Custom, LineCap endCap = LineCap.Custom) { - return new Size((int)(size.Width * multiple), size.Height); + return new Pen(Color.FromArgb(alpha > 255 ? 255 : alpha, ColorTranslator.FromHtml(htmlColor)), size) { StartCap = startCap, EndCap = endCap }; } - /// - /// 高度放大 - /// - public static Size MultiplyHeight(this Size size, double multiple) + public static Font ToFont(this string fontName, float size, FontStyle fontStyle = FontStyle.Regular, GraphicsUnit unit = GraphicsUnit.Pixel) { - return new Size(size.Width, (int)(size.Height * multiple)); + return new Font(fontName, size, fontStyle, unit); } - /// - /// 宽度和高度放大 - /// - public static Size MultiplyAll(this Size size, double multiple) + public static Brush GlowBrush(Color centerColor, Color[] surroundColor, PointF point, GraphicsPath gp, WrapMode wrapMode = WrapMode.Clamp) { - return new Size((int)(size.Width * multiple), (int)(size.Height * multiple)); + return new PathGradientBrush(gp) { CenterColor = centerColor, SurroundColors = surroundColor, FocusScales = point, WrapMode = wrapMode }; } - /// - /// 区域是否可见 - /// - public static bool IsVisible(this Rectangle rect) + public static Brush GlowBrush(Color centerColor, Color[] surroundColor, PointF[] point, WrapMode wrapMode = WrapMode.Clamp) { - return rect.Width > 0 && rect.Height > 0; + return new PathGradientBrush(point) { CenterColor = centerColor, SurroundColors = surroundColor, WrapMode = wrapMode }; + } + + public static StringFormat SetAlignment(StringAlignment horizontalAlignment = StringAlignment.Center, StringAlignment verticalAlignment = StringAlignment.Center) + { + return new StringFormat { Alignment = horizontalAlignment, LineAlignment = verticalAlignment }; } } } \ No newline at end of file diff --git a/SunnyUI/Static/UGraphics.cs b/SunnyUI/Static/UGraphics.cs new file mode 100644 index 00000000..e1a5cf5b --- /dev/null +++ b/SunnyUI/Static/UGraphics.cs @@ -0,0 +1,1075 @@ +/****************************************************************************** + * SunnyUI 开源控件库、工具类库、扩展类库、多页面开发框架。 + * CopyRight (C) 2012-2021 ShenYongHua(沈永华). + * QQ群:56829229 QQ:17612584 EMail:SunnyUI@QQ.Com + * + * Blog: https://www.cnblogs.com/yhuse + * Gitee: https://gitee.com/yhuse/SunnyUI + * GitHub: https://github.com/yhuse/SunnyUI + * + * SunnyUI.dll can be used for free under the GPL-3.0 license. + * If you use this code, please keep this note. + * 如果您使用此代码,请保留此说明。 + ****************************************************************************** + * 文件名称: UGraphics.cs + * 文件说明: GDI扩展类 + * 当前版本: V3.0 + * 创建日期: 2020-08-20 + * + * 2021-08-20: V3.0.6 整理了一些GDI绘图的常用方法扩展 +******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms; + +namespace Sunny.UI +{ + public static class GraphicsEx + { + public static void DrawString(this Graphics g, string text, Font font, Color color, RectangleF rect, StringFormat format) + { + using (Brush br = color.Brush()) + { + g.DrawString(text, font, br, rect, format); + } + } + + public static void DrawString(this Graphics g, string text, Font font, Color color, RectangleF rect) + { + using (Brush br = color.Brush()) + { + g.DrawString(text, font, br, rect); + } + } + + public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, StringFormat format) + { + using (Brush br = color.Brush()) + { + g.DrawString(text, font, br, rect, format); + } + } + + public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect) + { + using (Brush br = color.Brush()) + { + g.DrawString(text, font, br, rect); + } + } + + public static void DrawString(this Graphics g, string text, Font font, Color color, float x, float y) + { + using (Brush br = color.Brush()) + { + g.DrawString(text, font, br, x, y); + } + } + + public static void DrawString(this Graphics g, string text, Font font, Color color, Point pt) + => g.DrawString(text, font, color, pt.X, pt.Y); + + public static void DrawString(this Graphics g, string text, Font font, Color color, PointF pt) + => g.DrawString(text, font, color, pt.X, pt.Y); + + + public static void DrawString(this Graphics g, string text, Font font, Color color, float x, float y, StringFormat format) + { + using (Brush br = color.Brush()) + { + g.DrawString(text, font, br, x, y, format); + } + } + + public static void DrawString(this Graphics g, string text, Font font, Color color, PointF pt, StringFormat format) + => g.DrawString(text, font, color, pt.X, pt.Y, format); + + public static void DrawString(this Graphics g, string text, Font font, Color color, Point pt, StringFormat format) + => g.DrawString(text, font, color, pt.X, pt.Y, format); + + + public static void DrawLines(this Graphics g, Color color, Point[] points, bool smooth = false, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawLines(pen, points); + g.Smooth(false); + } + } + + public static void DrawLines(this Graphics g, Color color, PointF[] points, bool smooth = false, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawLines(pen, points); + g.Smooth(false); + } + } + + public static void DrawCurve(this Graphics g, Color color, Point[] points, bool smooth = false, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawCurve(pen, points); + g.Smooth(false); + } + } + + public static void DrawCurve(this Graphics g, Color color, PointF[] points, bool smooth = false, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawCurve(pen, points); + g.Smooth(false); + } + } + + public static void DrawLine(this Graphics g, Color color, int x1, int y1, int x2, int y2, bool smooth = false, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawLine(pen, x1, y1, x2, y2); + g.Smooth(false); + } + } + + public static void DrawLine(this Graphics g, Color color, Point pt1, Point pt2, bool smooth = false, float penWidth = 1) + => g.DrawLine(color, pt1.X, pt1.Y, pt2.X, pt2.Y, smooth, penWidth); + + public static void DrawLine(this Graphics g, Color color, float x1, float y1, float x2, float y2, bool smooth = false, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawLine(pen, x1, y1, x2, y2); + g.Smooth(false); + } + } + + public static void DrawLine(this Graphics g, Color color, PointF pt1, PointF pt2, bool smooth = false, float penWidth = 1) + => g.DrawLine(color, pt1.X, pt1.Y, pt2.X, pt2.Y, smooth, penWidth); + + public static void DrawArc(this Graphics g, Color color, int x, int y, int width, int height, int startAngle, int sweepAngle, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawArc(pen, x, y, width, height, startAngle, sweepAngle); + g.Smooth(false); + } + } + + public static void DrawArc(this Graphics g, Color color, float x, float y, float width, float height, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawArc(pen, x, y, width, height, startAngle, sweepAngle); + g.Smooth(false); + } + } + + public static void DrawArc(this Graphics g, Color color, Rectangle rect, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + => g.DrawArc(color, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle, smooth, penWidth); + + + public static void DrawArc(this Graphics g, Color color, RectangleF rect, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + => g.DrawArc(color, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle, smooth, penWidth); + + + public static void DrawBezier(this Graphics g, Color color, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawBezier(pen, x1, y1, x2, y2, x3, y3, x4, y4); + g.Smooth(false); + } + } + + public static void DrawBezier(this Graphics g, Color color, PointF pt1, PointF pt2, PointF pt3, PointF pt4, bool smooth = true, float penWidth = 1) + => g.DrawBezier(color, pt1.X, pt1.Y, pt2.X, pt2.Y, pt3.X, pt3.Y, pt4.X, pt4.Y, smooth, penWidth); + + public static void DrawBezier(this Graphics g, Color color, Point pt1, Point pt2, Point pt3, Point pt4, bool smooth = true, float penWidth = 1) + => g.DrawBezier(color, pt1.X, pt1.Y, pt2.X, pt2.Y, pt3.X, pt3.Y, pt4.X, pt4.Y, smooth, penWidth); + + public static void DrawBeziers(this Graphics g, Color color, PointF[] points, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawBeziers(pen, points); + g.Smooth(false); + } + } + + public static void DrawBeziers(this Graphics g, Color color, Point[] points, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawBeziers(pen, points); + g.Smooth(false); + } + } + + public static void DrawClosedCurve(this Graphics g, Color color, Point[] points, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawClosedCurve(pen, points); + g.Smooth(false); + } + } + + public static void DrawClosedCurve(this Graphics g, Color color, Point[] points, float tension, FillMode fillmode, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawClosedCurve(pen, points, tension, fillmode); + g.Smooth(false); + } + } + + public static void FillClosedCurve(this Graphics g, Color color, Point[] points, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillClosedCurve(sb, points); + g.Smooth(false); + } + } + + public static void FillClosedCurve(this Graphics g, Color color, Point[] points, FillMode fillmode, float tension, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillClosedCurve(sb, points, fillmode, tension); + g.Smooth(false); + } + } + + public static void FillPath(this Graphics g, Color color, GraphicsPath path, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillPath(sb, path); + g.Smooth(false); + } + } + + public static void DrawPath(this Graphics g, Color color, GraphicsPath path, bool smooth = true, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawPath(pn, path); + g.Smooth(false); + } + } + + public static void FillPolygon(this Graphics g, Color color, PointF[] points, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillPolygon(sb, points); + g.Smooth(false); + } + } + + public static void FillPolygon(this Graphics g, Color color, PointF[] points, FillMode fillMode, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillPolygon(sb, points, fillMode); + g.Smooth(false); + } + } + + public static void FillPolygon(this Graphics g, Color color, Point[] points, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillPolygon(sb, points); + g.Smooth(false); + } + } + + public static void FillPolygon(this Graphics g, Color color, Point[] points, FillMode fillMode, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillPolygon(sb, points, fillMode); + g.Smooth(false); + } + } + + public static void DrawPolygon(this Graphics g, Color color, PointF[] points, bool smooth = true, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawPolygon(pn, points); + g.Smooth(false); + } + } + + public static void DrawPolygon(this Graphics g, Color color, Point[] points, bool smooth = true, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawPolygon(pn, points); + g.Smooth(false); + } + } + + public static void FillEllipse(this Graphics g, Color color, Rectangle rect, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillEllipse(sb, rect); + g.Smooth(false); + } + } + + public static void DrawEllipse(this Graphics g, Color color, Rectangle rect, bool smooth = true, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawEllipse(pn, rect); + g.Smooth(false); + } + } + + public static void FillEllipse(this Graphics g, Color color, RectangleF rect, bool smooth = true) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillEllipse(sb, rect); + g.Smooth(false); + } + } + + public static void DrawEllipse(this Graphics g, Color color, RectangleF rect, bool smooth = true, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawEllipse(pn, rect); + g.Smooth(false); + } + } + + public static void FillEllipse(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = true) + => g.FillEllipse(color, new Rectangle(left, top, width, height), smooth); + + public static void DrawEllipse(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = true, float penWidth = 1) + => g.DrawEllipse(color, new Rectangle(left, top, width, height), smooth, penWidth); + + + public static void FillEllipse(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = true) + => g.FillEllipse(color, new RectangleF(left, top, width, height), smooth); + + + public static void DrawEllipse(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = true, float penWidth = 1) + => g.DrawEllipse(color, new RectangleF(left, top, width, height), smooth, penWidth); + + public static void FillRectangle(this Graphics g, Color color, Rectangle rect, bool smooth = false) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillRectangle(sb, rect); + g.Smooth(false); + } + } + + public static void DrawRectangle(this Graphics g, Color color, Rectangle rect, bool smooth = false, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawRectangle(pn, rect); + g.Smooth(false); + } + } + + public static void FillRectangle(this Graphics g, Color color, RectangleF rect, bool smooth = false) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillRectangle(sb, rect); + g.Smooth(false); + } + } + + public static void DrawRectangle(this Graphics g, Color color, RectangleF rect, bool smooth = false, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawRectangle(pn, rect.X, rect.Y, rect.Width, rect.Height); + g.Smooth(false); + } + } + + public static void FillRectangle(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = false) + => g.FillRectangle(color, new Rectangle(left, top, width, height), smooth); + + public static void DrawRectangle(this Graphics g, Color color, int left, int top, int width, int height, bool smooth = false, float penWidth = 1) + => g.DrawRectangle(color, new Rectangle(left, top, width, height), smooth, penWidth); + + + public static void FillRectangle(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = false) + => g.FillRectangle(color, new RectangleF(left, top, width, height), smooth); + + + public static void DrawRectangle(this Graphics g, Color color, float left, float top, float width, float height, bool smooth = false, float penWidth = 1) + => g.DrawRectangle(color, new RectangleF(left, top, width, height), smooth, penWidth); + + + public static void FillRectangles(this Graphics g, Color color, Rectangle[] rects, bool smooth = false) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillRectangles(sb, rects); + g.Smooth(false); + } + } + + public static void FillRectangles(this Graphics g, Color color, RectangleF[] rects, bool smooth = false) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillRectangles(sb, rects); + g.Smooth(false); + } + } + + public static void FillRegion(this Graphics g, Color color, Region region, bool smooth = false) + { + using (SolidBrush sb = color.Brush()) + { + g.Smooth(smooth); + g.FillRegion(sb, region); + g.Smooth(false); + } + } + + + public static void DrawRectangles(this Graphics g, Color color, Rectangle[] rects, bool smooth = false, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawRectangles(pn, rects); + g.Smooth(false); + } + } + + public static void DrawRectangles(this Graphics g, Color color, RectangleF[] rects, bool smooth = false, float penWidth = 1) + { + using (Pen pn = color.Pen(penWidth)) + { + g.Smooth(smooth); + g.DrawRectangles(pn, rects); + g.Smooth(false); + } + } + + public static void DrawRoundRectangle(this Graphics g, Pen pen, Rectangle rect, int cornerRadius, bool smooth = true) + { + g.Smooth(smooth); + if (cornerRadius > 0) + { + using (GraphicsPath path = rect.CreateRoundedRectanglePath(cornerRadius)) + { + g.DrawPath(pen, path); + } + } + else + { + g.DrawRectangle(pen, rect); + } + + g.Smooth(false); + } + + public static void FillRoundRectangle(this Graphics g, Brush brush, Rectangle rect, int cornerRadius, bool smooth = true) + { + g.Smooth(smooth); + if (cornerRadius > 0) + { + using (GraphicsPath path = rect.CreateRoundedRectanglePath(cornerRadius)) + { + g.FillPath(brush, path); + } + } + else + { + g.FillRectangle(brush, rect); + } + + g.Smooth(false); + } + + public static void DrawRoundRectangle(this Graphics g, Pen pen, int left, int top, int width, int height, int cornerRadius, bool smooth = true) + { + g.DrawRoundRectangle(pen, new Rectangle(left, top, width, height), cornerRadius, smooth); + } + + public static void FillRoundRectangle(this Graphics g, Brush brush, int left, int top, int width, int height, int cornerRadius, bool smooth = true) + { + g.FillRoundRectangle(brush, new Rectangle(left, top, width, height), cornerRadius, smooth); + } + + public static void DrawRoundRectangle(this Graphics g, Color color, Rectangle rect, int cornerRadius, bool smooth = true, float penWidth = 1) + { + if (cornerRadius > 0) + { + using (GraphicsPath path = rect.CreateRoundedRectanglePath(cornerRadius)) + { + g.DrawPath(color, path, smooth, penWidth); + } + } + else + { + g.DrawRectangle(color, rect, smooth, penWidth); + } + } + + public static void FillRoundRectangle(this Graphics g, Color color, Rectangle rect, int cornerRadius, bool smooth = true) + { + if (cornerRadius > 0) + { + using (GraphicsPath path = rect.CreateRoundedRectanglePath(cornerRadius)) + { + g.FillPath(color, path, smooth); + } + } + else + { + g.FillRectangle(color, rect, smooth); + } + } + + public static void DrawRoundRectangle(this Graphics g, Color color, int left, int top, int width, int height, int cornerRadius, bool smooth = true, float penWidth = 1) + => g.DrawRoundRectangle(color, new Rectangle(left, top, width, height), cornerRadius, smooth, penWidth); + + public static void FillRoundRectangle(this Graphics g, Color color, int left, int top, int width, int height, int cornerRadius, bool smooth = true) + => g.FillRoundRectangle(color, new Rectangle(left, top, width, height), cornerRadius, smooth); + + public static void DrawCross(this Graphics g, Color color, Point center, int crossSize = 3, float penWidth = 1) + { + g.DrawLine(color, center.X - crossSize, center.Y, center.X + crossSize, center.Y, false, penWidth); + g.DrawLine(color, center.X, center.Y - crossSize, center.X, center.Y + crossSize, false, penWidth); + } + + public static void DrawFan(this Graphics g, Color color, Point center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + { + if (d1.Equals(0)) + { + g.DrawPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle, smooth, penWidth); + } + else + { + GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); + g.DrawPath(color, path, smooth, penWidth); + path.Dispose(); + } + } + + public static void DrawFan(this Graphics g, Color color, PointF center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + { + if (d1.Equals(0)) + { + g.DrawPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle, smooth, penWidth); + } + else + { + GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); + g.DrawPath(color, path, smooth, penWidth); + path.Dispose(); + } + } + + public static void FillFan(this Graphics g, Color color, Point center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true) + { + if (d1.Equals(0)) + { + g.FillPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle, smooth); + } + else + { + GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); + g.FillPath(color, path, smooth); + path.Dispose(); + } + } + + public static void FillFan(this Graphics g, Color color, PointF center, float d1, float d2, float startAngle, float sweepAngle, bool smooth = true) + { + if (d1.Equals(0)) + { + g.FillPie(color, center.X - d2, center.Y - d2, d2 * 2, d2 * 2, startAngle, sweepAngle, smooth); + } + else + { + GraphicsPath path = g.CreateFanPath(center, d1, d2, startAngle, sweepAngle); + g.FillPath(color, path, smooth); + path.Dispose(); + } + } + + public static void FillPie(this Graphics g, Color color, int x, int y, int width, int height, float startAngle, float sweepAngle, bool smooth = true) + { + g.Smooth(smooth); + using (Brush br = color.Brush()) + { + g.FillPie(br, x, y, width, height, startAngle, sweepAngle); + } + + g.Smooth(false); + } + + public static void FillPie(this Graphics g, Color color, Rectangle rect, float startAngle, float sweepAngle, bool smooth = true) + => g.FillPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth); + + public static void FillPie(this Graphics g, Color color, float x, float y, float width, float height, float startAngle, float sweepAngle, bool smooth = true) + { + g.Smooth(smooth); + using (Brush br = color.Brush()) + { + g.FillPie(br, x, y, width, height, startAngle, sweepAngle); + } + + g.Smooth(false); + } + + public static void FillPie(this Graphics g, Color color, RectangleF rect, float startAngle, float sweepAngle, bool smooth = true) + => g.FillPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth); + + public static void DrawPoint(this Graphics g, Color color, int x, int y, float size) + => g.FillEllipse(color, x - size / 2.0f, y - size / 2.0f, size, size); + + public static void DrawPoint(this Graphics g, Color color, float x, float y, float size) + => g.FillEllipse(color, x - size / 2.0f, y - size / 2.0f, size, size); + + public static void DrawPoint(this Graphics g, Color color, Point point, float size) + => g.DrawPoint(color, point.X, point.Y, size); + + public static void DrawPoint(this Graphics g, Color color, PointF point, float size) + => g.DrawPoint(color, point.X, point.Y, size); + + public static void DrawPie(this Graphics g, Color color, Rectangle rect, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + => g.DrawPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth, penWidth); + + public static void DrawPie(this Graphics g, Color color, float x, float y, float width, float height, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + { + g.Smooth(smooth); + using (Pen pen = color.Pen(penWidth)) + { + g.DrawPie(pen, x, y, width, height, startAngle, sweepAngle); + } + + g.Smooth(false); + } + + public static void DrawPie(this Graphics g, Color color, RectangleF rect, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + => g.DrawPie(color, rect.Left, rect.Top, rect.Width, rect.Height, startAngle, sweepAngle, smooth, penWidth); + + public static void DrawPie(this Graphics g, Color color, int x, int y, int width, int height, float startAngle, float sweepAngle, bool smooth = true, float penWidth = 1) + { + g.Smooth(smooth); + using (Pen pen = color.Pen(penWidth)) + { + g.DrawPie(pen, x, y, width, height, startAngle, sweepAngle); + } + + g.Smooth(false); + } + + /// + /// 九宫切图背景填充,#,http://st233.com/blog.php?id=24 + /// 例如按钮是图片分成九个区域 然后只需要将四角填充到目标区域 其余的拉伸就可以了 + /// + /// + /// + /// + /// + public static void DrawImageWithNineCut(this Graphics g, Image img, Rectangle rect, int angleSize = 5) + { + //填充四个角 + g.DrawImage(img, new Rectangle(rect.X, rect.Y, angleSize, angleSize), + new Rectangle(0, 0, angleSize, angleSize), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(rect.Right - angleSize, rect.Y, angleSize, angleSize), + new Rectangle(img.Width - angleSize, 0, angleSize, angleSize), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(rect.X, rect.Bottom - angleSize, angleSize, angleSize), + new Rectangle(0, img.Height - angleSize, angleSize, angleSize), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(rect.Right - angleSize, rect.Bottom - angleSize, angleSize, angleSize), + new Rectangle(img.Width - angleSize, img.Height - angleSize, angleSize, angleSize), GraphicsUnit.Pixel); + //四边 + g.DrawImage(img, new Rectangle(rect.X, rect.Y + angleSize, angleSize, rect.Height - angleSize * 2), + new Rectangle(0, angleSize, angleSize, img.Height - angleSize * 2), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(rect.X + angleSize, rect.Y, rect.Width - angleSize * 2, angleSize), + new Rectangle(angleSize, 0, img.Width - angleSize * 2, angleSize), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(rect.Right - angleSize, rect.Y + angleSize, angleSize, rect.Height - angleSize * 2), + new Rectangle(img.Width - angleSize, angleSize, angleSize, img.Height - angleSize * 2), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(rect.X + angleSize, rect.Bottom - angleSize, rect.Width - angleSize * 2, angleSize), + new Rectangle(angleSize, img.Height - angleSize, img.Width - angleSize * 2, angleSize), GraphicsUnit.Pixel); + //中间 + g.DrawImage(img, + new Rectangle(rect.X + angleSize, rect.Y + angleSize, rect.Width - angleSize * 2, rect.Height - angleSize * 2), + new Rectangle(angleSize, angleSize, img.Width - angleSize * 2, img.Height - angleSize * 2), GraphicsUnit.Pixel); + } + + public static void DrawImageWithNineCut(this Graphics g, Image img, int destWidth, int destHeight, int cutLeft, int cutRight, int cutTop, int cutBottom, int iZoom = 1) + { + iZoom = Math.Max(1, iZoom); + + //填充四个角 + g.DrawImage(img, new Rectangle(0, 0, cutLeft * iZoom, cutTop * iZoom), + new Rectangle(0, 0, cutLeft, cutTop), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(destWidth - cutRight * iZoom, 0, cutRight * iZoom, cutTop * iZoom), + new Rectangle(img.Width - cutRight, 0, cutRight, cutTop), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(0, destHeight - cutBottom * iZoom, cutLeft * iZoom, cutBottom * iZoom), + new Rectangle(0, img.Height - cutBottom, cutLeft, cutBottom), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(destWidth - cutRight * iZoom, destHeight - cutBottom * iZoom, cutRight * iZoom, cutBottom * iZoom), + new Rectangle(img.Width - cutRight, img.Height - cutBottom, cutRight, cutBottom), GraphicsUnit.Pixel); + + //四边 + g.DrawImage(img, new Rectangle(cutLeft * iZoom, 0, destWidth - (cutLeft + cutRight) * iZoom, cutTop * iZoom), + new Rectangle(cutLeft, 0, img.Width - cutLeft - cutRight, cutTop), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(0, cutTop * iZoom, cutLeft * iZoom, destHeight - (cutTop + cutBottom) * iZoom), + new Rectangle(0, cutTop, cutLeft, img.Height - cutTop - cutBottom), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(destWidth - cutRight * iZoom, cutTop * iZoom, cutRight * iZoom, destHeight - (cutTop + cutBottom) * iZoom), + new Rectangle(img.Width - cutRight, cutTop, cutRight, img.Height - cutTop - cutBottom), GraphicsUnit.Pixel); + g.DrawImage(img, new Rectangle(cutLeft * iZoom, destHeight - cutBottom * iZoom, destWidth - (cutLeft + cutRight) * iZoom, cutBottom * iZoom), + new Rectangle(cutLeft, img.Height - cutBottom, img.Width - cutLeft - cutRight, cutBottom), GraphicsUnit.Pixel); + + //中间 + g.DrawImage(img, new Rectangle(cutLeft * iZoom, cutTop * iZoom, destWidth - (cutLeft + cutRight) * iZoom, destHeight - (cutTop + cutBottom) * iZoom), + new Rectangle(cutLeft, cutTop, img.Width - cutLeft - cutRight, img.Height - cutTop - cutBottom), GraphicsUnit.Pixel); + } + + public static void DrawString(this Graphics g, string str, Font font, Color color, Size size, Padding padding, ContentAlignment align) + { + SizeF sf = g.MeasureString(str, font); + using (Brush br = color.Brush()) + { + switch (align) + { + case ContentAlignment.MiddleCenter: + g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, + padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); + break; + + case ContentAlignment.TopLeft: + g.DrawString(str, font, br, padding.Left, padding.Top); + break; + + case ContentAlignment.TopCenter: + g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, padding.Top); + break; + + case ContentAlignment.TopRight: + g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, padding.Top); + break; + + case ContentAlignment.MiddleLeft: + g.DrawString(str, font, br, padding.Left, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); + break; + + case ContentAlignment.MiddleRight: + g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); + break; + + case ContentAlignment.BottomLeft: + g.DrawString(str, font, br, padding.Left, size.Height - sf.Height - padding.Bottom); + break; + + case ContentAlignment.BottomCenter: + g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, size.Height - sf.Height - padding.Bottom); + break; + + case ContentAlignment.BottomRight: + g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, size.Height - sf.Height - padding.Bottom); + break; + } + } + } + + public static void DrawString(this Graphics g, string s, Font font, Color color, RectangleF rect, StringFormat format, float angle) + { + using (Brush br = color.Brush()) + { + g.DrawStringRotateAtCenter(s, font, color, rect.Center(), (int)angle); + //g.DrawString(s, font, br, layoutRectangle, format, angle); + } + } + + /// + /// 以文字中心点为原点,旋转文字 + /// + /// Graphics + /// 文字 + /// 字体 + /// 颜色 + /// 文字中心点 + /// 角度 + public static void DrawStringRotateAtCenter(this Graphics g, string text, Font font, Color color, PointF centerPoint, float angle) + { + using (Brush br = color.Brush()) + { + g.DrawStringRotateAtCenter(text, font, br, centerPoint, angle); + } + } + + /// + /// 以文字中心点为原点,旋转文字 + /// + /// Graphics + /// 文字 + /// 字体 + /// 笔刷 + /// 文字中心点 + /// 角度 + public static void DrawStringRotateAtCenter(this Graphics g, string text, Font font, Brush brush, PointF centerPoint, float angle) + { + SizeF sf = g.MeasureString(text, font); + float x1 = centerPoint.X - sf.Width / 2.0f; + float y1 = centerPoint.Y - sf.Height / 2.0f; + + // 把画板的原点(默认是左上角)定位移到文字中心 + g.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2); + // 旋转画板 + g.RotateTransform(angle); + // 回退画板x,y轴移动过的距离 + g.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2)); + g.DrawString(text, font, brush, x1, y1); + + //恢复 + g.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2); + g.RotateTransform(-angle); + g.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2)); + } + + /// + /// 以旋转点为原点,旋转文字 + /// + /// Graphics + /// 文本 + /// 字体 + /// 填充 + /// 旋转点 + /// 布局方式 + /// 角度 + public static void DrawString(this Graphics g, string text, Font font, Brush brush, PointF rotatePoint, StringFormat format, float angle) + { + // Save the matrix + Matrix mtxSave = g.Transform; + + Matrix mtxRotate = g.Transform; + mtxRotate.RotateAt(angle, rotatePoint); + g.Transform = mtxRotate; + + g.DrawString(text, font, brush, rotatePoint, format); + + // Reset the matrix + g.Transform = mtxSave; + } + + public static void DrawString(this Graphics g, string s, Font font, Color color, PointF rotatePoint, StringFormat format, float angle) + { + using (Brush br = color.Brush()) + { + g.DrawString(s, font, br, rotatePoint, format, angle); + } + } + + /// + /// 绘制根据矩形旋转文本 + /// + /// Graphics + /// 文本 + /// 字体 + /// 填充 + /// 局部矩形 + /// 布局方式 + /// 角度 + public static void DrawString(this Graphics g, string text, Font font, Brush brush, RectangleF rect, StringFormat format, float angle) + { + g.DrawStringRotateAtCenter(text, font, brush, rect.Center(), angle); + } + + public static void DrawTwoPoints(this Graphics g, Color color, PointF pf1, PointF pf2, Rectangle rect, bool smooth = true, float penWidth = 1) + { + using (Pen pen = color.Pen(penWidth)) + { + DrawTwoPoints(g, pen, pf1, pf2, rect, smooth); + } + } + + public static void DrawTwoPoints(this Graphics g, Pen pen, PointF pf1, PointF pf2, Rectangle rect, bool smooth = true) + { + bool haveLargePixel = Math.Abs(pf1.X - pf2.X) >= rect.Width * 100 || Math.Abs(pf1.Y - pf2.Y) >= rect.Height * 100; + + //两点都在区域内 + if (pf1.InRect(rect) && pf2.InRect(rect)) + { + g.Smooth(smooth); + g.DrawLine(pen, pf1, pf2); + g.Smooth(false); + return; + } + + //无大坐标像素 + if (!haveLargePixel) + { + g.Smooth(smooth); + g.DrawLine(pen, pf1, pf2); + g.Smooth(false); + return; + } + + //垂直线 + if (pf1.X.Equals(pf2.X)) + { + if (pf1.X <= rect.Left) return; + if (pf1.X >= rect.Right) return; + if (pf1.Y <= rect.Top && pf2.Y <= rect.Top) return; + if (pf1.Y >= rect.Bottom && pf2.Y >= rect.Bottom) return; + + g.Smooth(smooth); + float yy1 = Math.Min(pf1.Y, pf2.Y); + float yy2 = Math.Max(pf1.Y, pf2.Y); + if (yy1 <= rect.Top) + { + if (yy2 <= rect.Bottom) g.DrawLine(pen, pf1.X, rect.Top, pf1.X, yy2); + else g.DrawLine(pen, pf1.X, rect.Top, pf1.X, rect.Bottom); + } + else + { + if (yy2 <= rect.Bottom) g.DrawLine(pen, pf1.X, yy1, pf1.X, yy2); + else g.DrawLine(pen, pf1.X, yy1, pf1.X, rect.Bottom); + } + + g.Smooth(false); + return; + } + + //水平线 + if (pf1.Y.Equals(pf2.Y)) + { + if (pf1.Y <= rect.Top) return; + if (pf1.Y >= rect.Bottom) return; + if (pf1.X <= rect.Left && pf2.X <= rect.Left) return; + if (pf1.X >= rect.Right && pf2.X >= rect.Right) return; + + g.Smooth(smooth); + float xx1 = Math.Min(pf1.X, pf2.X); + float xx2 = Math.Max(pf1.X, pf2.X); + if (xx1 <= rect.Left) + { + if (xx2 <= rect.Right) g.DrawLine(pen, rect.Left, pf1.Y, xx2, pf1.Y); + else g.DrawLine(pen, rect.Left, pf1.Y, rect.Right, pf1.Y); + } + else + { + if (xx2 <= rect.Right) g.DrawLine(pen, xx1, pf1.Y, xx2, pf1.Y); + else g.DrawLine(pen, xx1, pf1.Y, rect.Right, pf1.Y); + } + + g.Smooth(false); + return; + } + + //判断两个区域是否相交 + RectangleF rect1 = pf1.CreateRectangleF(pf2); + if (!rect1.IsOverlap(rect)) return; + + double x1 = GDI.CalcX(pf1, pf2, rect.Top); + double x2 = GDI.CalcX(pf1, pf2, rect.Bottom); + double y1 = GDI.CalcY(pf1, pf2, rect.Left); + double y2 = GDI.CalcY(pf1, pf2, rect.Right); + + //判断线段是否和区域有交点 + bool isExist = x1.InRange(rect.Left, rect.Right) || x2.InRange(rect.Left, rect.Right) || y1.InRange(rect.Top, rect.Bottom) || y2.InRange(rect.Top, rect.Bottom); + if (!isExist) return; + + List TwoPoints = new List(); + if (!pf1.InRect(rect) && !pf2.InRect(rect)) + { + if (x1.InRange(rect.Left, rect.Right)) TwoPoints.Add(new PointF((float)x1, rect.Top)); + if (x2.InRange(rect.Left, rect.Right)) TwoPoints.Add(new PointF((float)x2, rect.Bottom)); + if (y1.InRange(rect.Top, rect.Bottom)) TwoPoints.Add(new PointF(rect.Left, (float)y1)); + if (y2.InRange(rect.Top, rect.Bottom)) TwoPoints.Add(new PointF(rect.Right, (float)y2)); + } + else + { + PointF center = pf1.InRect(rect) ? pf1 : pf2; + PointF border = pf2.InRect(rect) ? pf1 : pf2; + TwoPoints.Add(center); + if (border.X >= center.X) + { + if (border.Y >= center.Y) + { + TwoPoints.Add(x2 <= rect.Right ? new PointF((float)x2, rect.Bottom) : new PointF(rect.Right, (float)y2)); + } + else + { + TwoPoints.Add(x1 <= rect.Right ? new PointF((float)x1, rect.Top) : new PointF(rect.Right, (float)y2)); + } + } + else + { + if (border.Y >= center.Y) + { + TwoPoints.Add(x2 >= rect.Left ? new PointF((float)x2, rect.Bottom) : new PointF(rect.Left, (float)y1)); + } + else + { + TwoPoints.Add(x1 >= rect.Left ? new PointF((float)x1, rect.Bottom) : new PointF(rect.Left, (float)y1)); + } + } + } + + if (TwoPoints.Count == 2) + { + g.Smooth(smooth); + g.DrawLine(pen, TwoPoints[0], TwoPoints[1]); + g.Smooth(false); + } + else + { + Console.WriteLine(TwoPoints.Count); + } + + TwoPoints.Clear(); + } + } +} diff --git a/SunnyUI/Static/UImage.cs b/SunnyUI/Static/UImage.cs index 5e6b538d..cfccaa12 100644 --- a/SunnyUI/Static/UImage.cs +++ b/SunnyUI/Static/UImage.cs @@ -572,5 +572,76 @@ namespace Sunny.UI } } } + + public static void DrawImageFromBase64(this Graphics g, string base64Image, Rectangle rect) + { + using (var ms = new System.IO.MemoryStream(Convert.FromBase64String(base64Image))) + using (var image = Image.FromStream(ms)) + { + g.DrawImage(image, rect); + image.Dispose(); + } + } + + public static void DrawTransparentImage(this Graphics g, float alpha, Image image, Rectangle rect) + { + var colorMatrix = new ColorMatrix { Matrix33 = alpha }; + var imageAttributes = new ImageAttributes(); + imageAttributes.SetColorMatrix(colorMatrix); + g.DrawImage(image, new Rectangle(rect.X, rect.Y, image.Width, image.Height), rect.X, rect.Y, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); + imageAttributes.Dispose(); + } + + 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); + } + } + } + + 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); + } + } + } + + public static string ToBase64(this Image image) + { + using (var toBase64 = new MemoryStream()) + { + image.Save(toBase64, image.RawFormat); + image.Dispose(); + return Convert.ToBase64String(toBase64.ToArray()); + } + } + + public static Image ToImage(string base64Image) + { + using (var toImage = new System.IO.MemoryStream(Convert.FromBase64String(base64Image))) + { + return Image.FromStream(toImage); + } + } } } \ No newline at end of file diff --git a/SunnyUI/Style/UIStyleColor.cs b/SunnyUI/Style/UIStyleColor.cs index 8e09942d..01d43a9e 100644 --- a/SunnyUI/Style/UIStyleColor.cs +++ b/SunnyUI/Style/UIStyleColor.cs @@ -129,8 +129,8 @@ namespace Sunny.UI Name = style; MenuSelectedColor = TitleColor = RectColor = PrimaryColor = color; - Color[] colors = GDIEx.GradientColors(Color.White, PrimaryColor, 16); - Color[] colors1 = GDIEx.GradientColors(PrimaryColor, Color.Black, 16); + Color[] colors = Color.White.GradientColors(PrimaryColor, 16); + Color[] colors1 = PrimaryColor.GradientColors(Color.Black, 16); PlainColor = colors[1]; SecondaryColor = colors[5]; RegularColor = colors[10]; @@ -147,8 +147,8 @@ namespace Sunny.UI { Name = style; MenuSelectedColor = RectColor = RectHoverColor = TitleColor = ButtonForeColor = ButtonFillHoverColor = PrimaryColor = color; - Color[] colors = GDIEx.GradientColors(Color.White, PrimaryColor, 16); - Color[] colors1 = GDIEx.GradientColors(PrimaryColor, Color.Black, 16); + Color[] colors = Color.White.GradientColors(PrimaryColor, 16); + Color[] colors1 = PrimaryColor.GradientColors(Color.Black, 16); ButtonFillColor = PlainColor = colors[1]; SecondaryColor = colors[5]; RegularColor = colors[10];