* 整理了一些GDI绘图的常用方法扩展

This commit is contained in:
Sunny 2021-08-20 16:21:53 +08:00
parent 7127305915
commit a874692b34
14 changed files with 1219 additions and 1032 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -310,7 +310,7 @@ namespace Sunny.UI
if (IsDisposed) return; if (IsDisposed) return;
Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1); 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()) if (ShowFill && fillColor.IsValid())

View File

@ -122,7 +122,7 @@ namespace Sunny.UI
get => valveColor; get => valveColor;
set set
{ {
valveColor = value; valveColor = value;
Invalidate(); Invalidate();
} }
} }
@ -216,7 +216,7 @@ namespace Sunny.UI
rect = new Rectangle(Width - pipeSize - 8 - 14 - 10, Height / 2 - 14, 10, 27); rect = new Rectangle(Width - pipeSize - 8 - 14 - 10, Height / 2 - 14, 10, 27);
e.Graphics.FillRectangle(valveColor, rect); 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); rect = new Rectangle(Width - pipeSize - 8 - 14 - 10, Height / 2 - 14 + 4, 10, 4);
e.Graphics.FillRectangle(colors[4], rect); e.Graphics.FillRectangle(colors[4], rect);
rect = new Rectangle(Width - pipeSize - 8 - 14 - 10, Height / 2 - 14 + 12, 10, 4); 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); rect = new Rectangle(Width / 2 - 14, pipeSize + 8 + 10 + 4, 27, 10);
e.Graphics.FillRectangle(valveColor, rect); 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); rect = new Rectangle(Width / 2 - 14 + 4, pipeSize + 8 + 10 + 4, 4, 10);
e.Graphics.FillRectangle(colors[4], rect); e.Graphics.FillRectangle(colors[4], rect);
rect = new Rectangle(Width / 2 - 14 + 12, pipeSize + 8 + 10 + 4, 4, 10); 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); rect = new Rectangle(pipeSize + 8 + 10 + 4, Height / 2 - 14, 10, 27);
e.Graphics.FillRectangle(valveColor, rect); 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); rect = new Rectangle(pipeSize + 8 + 10 + 4, Height / 2 - 14 + 4, 10, 4);
e.Graphics.FillRectangle(colors[4], rect); e.Graphics.FillRectangle(colors[4], rect);
rect = new Rectangle(pipeSize + 8 + 10 + 4, Height / 2 - 14 + 12, 10, 4); 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); rect = new Rectangle(Width / 2 - 14, Height - pipeSize - 8 - 14 - 10, 27, 10);
e.Graphics.FillRectangle(valveColor, rect); 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); rect = new Rectangle(Width / 2 - 14 + 4, Height - pipeSize - 8 - 14 - 10, 4, 10);
e.Graphics.FillRectangle(colors[4], rect); e.Graphics.FillRectangle(colors[4], rect);
rect = new Rectangle(Width / 2 - 14 + 12, Height - pipeSize - 8 - 14 - 10, 4, 10); rect = new Rectangle(Width / 2 - 14 + 12, Height - pipeSize - 8 - 14 - 10, 4, 10);

View File

@ -598,7 +598,7 @@ namespace Sunny.UI
size = Size; size = Size;
// 若窗体从正常模式->最大化模式,该操作是由移动窗体至顶部触发的,记录的是移动前的窗体位置 // 若窗体从正常模式->最大化模式,该操作是由移动窗体至顶部触发的,记录的是移动前的窗体位置
location = IsOnMoving ? FormLocation : Location; location = IsOnMoving ? FormLocation : Location;
GDIEx.SetFormRoundRectRegion(this, 0); FormEx.SetFormRoundRectRegion(this, 0);
WindowState = FormWindowState.Maximized; WindowState = FormWindowState.Maximized;
} }
else if (WindowState == FormWindowState.Maximized) else if (WindowState == FormWindowState.Maximized)
@ -620,7 +620,7 @@ namespace Sunny.UI
} }
Location = location; Location = location;
GDIEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); FormEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0);
WindowState = FormWindowState.Normal; WindowState = FormWindowState.Normal;
} }
@ -1371,11 +1371,11 @@ namespace Sunny.UI
if (WindowState == FormWindowState.Maximized) if (WindowState == FormWindowState.Maximized)
{ {
GDIEx.SetFormRoundRectRegion(this, 0); FormEx.SetFormRoundRectRegion(this, 0);
} }
else else
{ {
GDIEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0); FormEx.SetFormRoundRectRegion(this, ShowRadius ? 5 : 0);
} }
Invalidate(); Invalidate();

View File

@ -602,6 +602,21 @@ namespace Sunny.UI
{ {
return new UIEditForm(option); return new UIEditForm(option);
} }
/// <summary>
/// 设置窗体的圆角矩形
/// </summary>
/// <param name="form">需要设置的窗体</param>
/// <param name="rgnRadius">圆角矩形的半径</param>
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 public interface IFrame

File diff suppressed because it is too large Load Diff

1075
SunnyUI/Static/UGraphics.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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);
}
}
} }
} }

View File

@ -129,8 +129,8 @@ namespace Sunny.UI
Name = style; Name = style;
MenuSelectedColor = TitleColor = RectColor = PrimaryColor = color; MenuSelectedColor = TitleColor = RectColor = PrimaryColor = color;
Color[] colors = GDIEx.GradientColors(Color.White, PrimaryColor, 16); Color[] colors = Color.White.GradientColors(PrimaryColor, 16);
Color[] colors1 = GDIEx.GradientColors(PrimaryColor, Color.Black, 16); Color[] colors1 = PrimaryColor.GradientColors(Color.Black, 16);
PlainColor = colors[1]; PlainColor = colors[1];
SecondaryColor = colors[5]; SecondaryColor = colors[5];
RegularColor = colors[10]; RegularColor = colors[10];
@ -147,8 +147,8 @@ namespace Sunny.UI
{ {
Name = style; Name = style;
MenuSelectedColor = RectColor = RectHoverColor = TitleColor = ButtonForeColor = ButtonFillHoverColor = PrimaryColor = color; MenuSelectedColor = RectColor = RectHoverColor = TitleColor = ButtonForeColor = ButtonFillHoverColor = PrimaryColor = color;
Color[] colors = GDIEx.GradientColors(Color.White, PrimaryColor, 16); Color[] colors = Color.White.GradientColors(PrimaryColor, 16);
Color[] colors1 = GDIEx.GradientColors(PrimaryColor, Color.Black, 16); Color[] colors1 = PrimaryColor.GradientColors(Color.Black, 16);
ButtonFillColor = PlainColor = colors[1]; ButtonFillColor = PlainColor = colors[1];
SecondaryColor = colors[5]; SecondaryColor = colors[5];
RegularColor = colors[10]; RegularColor = colors[10];