📄 drawhelper.cs
字号:
namespace Imps.Client.Utils
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public static class DrawHelper
{
public static GraphicsPath CreateRoundRectPath(Rectangle rect, Size size)
{
GraphicsPath path = new GraphicsPath();
path.AddLine(rect.Left + (size.Width / 2), rect.Top, rect.Right - (size.Width / 2), rect.Top);
path.AddArc(rect.Right - size.Width, rect.Top, size.Width, size.Height, 270f, 90f);
path.AddLine(rect.Right, rect.Top + (size.Height / 2), rect.Right, rect.Bottom - (size.Width / 2));
path.AddArc(rect.Right - size.Width, rect.Bottom - size.Height, size.Width, size.Height, 0f, 90f);
path.AddLine(rect.Right - (size.Width / 2), rect.Bottom, rect.Left + (size.Width / 2), rect.Bottom);
path.AddArc(rect.Left, rect.Bottom - size.Height, size.Width, size.Height, 90f, 90f);
path.AddLine(rect.Left, rect.Bottom - (size.Height / 2), rect.Left, rect.Top + (size.Height / 2));
path.AddArc(rect.Left, rect.Top, size.Width, size.Height, 180f, 90f);
return path;
}
public static GraphicsPath CreateRoundRectPath(Rectangle rect, int size)
{
return CreateRoundRectPath(rect, new Size(size, size));
}
public static void DrawAndFillRect(Graphics g, Rectangle rect, Color brushColor, Color penColor, int shadowDepth)
{
int num = (shadowDepth > 0) ? (penColor.A / (shadowDepth + 1)) : 0;
for (int i = 0; i < shadowDepth; i++)
{
rect.Inflate(-i, -i);
using (Brush brush = new SolidBrush(Color.FromArgb(num * (i + 1), penColor)))
{
g.FillRectangle(brush, rect);
}
}
FillRect(g, rect, brushColor);
}
public static void DrawAndFillRoundRect(Graphics g, Rectangle rect, int roundSize, Color backColor, Color penColor)
{
DrawAndFillRoundRect(g, rect, roundSize, backColor, penColor, 0);
}
public static void DrawAndFillRoundRect(Graphics g, Rectangle rect, int roundSize, Color backColor, Color penColor, int shadowDepth)
{
GraphicsPath path;
int alpha = (shadowDepth > 0) ? (penColor.A / (shadowDepth + 1)) : 0;
Color color = (shadowDepth > 0) ? Color.FromArgb(alpha, penColor) : penColor;
Label_0027:
path = CreateRoundRectPath(rect, roundSize);
using (Pen pen = new Pen(color))
{
g.DrawPath(pen, path);
}
if (shadowDepth-- > 0)
{
color = Color.FromArgb(color.A + alpha, penColor);
rect.Inflate(-1, -1);
goto Label_0027;
}
if (backColor.A != 0)
{
rect.Inflate(-1, -1);
path = CreateRoundRectPath(rect, roundSize);
using (Brush brush = new SolidBrush(backColor))
{
g.FillPath(brush, path);
}
}
}
public static void DrawImage(Graphics g, Rectangle rect, Image image, ImageLayout layout)
{
switch (layout)
{
case 0:
g.DrawImageUnscaledAndClipped(image, rect);
return;
case 1:
{
using (TextureBrush brush = new TextureBrush(image, WrapMode.Tile))
{
g.FillRectangle(brush, rect);
return;
}
}
case 2:
g.DrawImage(image, rect.X + ((rect.Width - image.Width) / 2), rect.Y + ((rect.Height - image.Height) / 2), image.Width, image.Height);
return;
case 3:
case 4:
g.DrawImage(image, rect);
return;
default:
return;
}
}
public static void DrawImageDisabled(Graphics g, Rectangle rect, Image image, ImageLayout layout)
{
switch (layout)
{
case 0:
ControlPaint.DrawImageDisabled(g, image, rect.X, rect.Y, Color.White);
return;
case 2:
ControlPaint.DrawImageDisabled(g, image, rect.X + ((rect.Width - image.Width) / 2), rect.Y + ((rect.Height - image.Height) / 2), Color.White);
return;
}
throw new NotSupportedException("不支持此种填充方式(您确定需要这样做?)。");
}
public static void DrawRect(Graphics g, Rectangle rect, Color color)
{
ControlPaint.DrawBorder(g, rect, color, ButtonBorderStyle.Solid);
}
public static void DrawRectangleShadow(Graphics g, Rectangle rect, int depth, Color clrBase)
{
int num = clrBase.A / depth;
int num2 = 1;
for (int i = clrBase.A; num2 <= depth; i -= num)
{
using (Brush brush = new SolidBrush(Color.FromArgb(i, clrBase)))
{
g.FillRectangle(brush, rect.Left + num2, rect.Bottom + num2, rect.Width, 1);
g.FillRectangle(brush, rect.Right + num2, rect.Top + num2, 1, rect.Height);
}
num2++;
}
}
public static void DrawRoundRect(Graphics g, Rectangle rect, int roundSize, Color penColor)
{
DrawAndFillRoundRect(g, rect, roundSize, Color.Transparent, penColor, 0);
}
public static void DrawString(Graphics g, string s, Rectangle rc, Font font, Brush brush, StringAlignment sa, bool enabled)
{
using (StringFormat format = new StringFormat(StringFormatFlags.NoWrap))
{
format.Alignment = sa;
format.LineAlignment = StringAlignment.Center;
format.Trimming = StringTrimming.EllipsisCharacter;
if (enabled)
{
g.DrawString(s, font, brush, rc, format);
}
else
{
ControlPaint.DrawStringDisabled(g, s, font, Color.White, rc, format);
}
}
}
public static void DrawTriangle(Graphics g, Rectangle rect, Direction direct, Color clr)
{
if ((rect.Width != 0) && (rect.Height != 0))
{
using (Brush brush = new SolidBrush(clr))
{
float num;
float y;
float num3;
float x;
float top;
float bottom;
switch (direct)
{
case Direction.Right:
num = ((float) rect.Height) / ((float) (rect.Width * 2));
y = (rect.Top + rect.Bottom) / 2;
num3 = y;
x = rect.Right;
goto Label_0090;
case Direction.Up:
num = ((float) rect.Width) / ((float) (rect.Height * 2));
y = (rect.Left + rect.Right) / 2;
num3 = y;
top = rect.Top;
goto Label_00FA;
case Direction.Down:
num = ((float) rect.Width) / ((float) (rect.Height * 2));
y = (rect.Left + rect.Right) / 2;
num3 = y;
bottom = rect.Bottom;
goto Label_0164;
default:
goto Label_0172;
}
Label_006C:
g.FillRectangle(brush, x, y, 1f, num3 - y);
y -= num;
num3 += num;
x -= 1f;
Label_0090:
if (x >= rect.Left)
{
goto Label_006C;
}
return;
Label_00D6:
g.FillRectangle(brush, y, top, num3 - y, 1f);
y -= num;
num3 += num;
top += 1f;
Label_00FA:
if (top <= rect.Bottom)
{
goto Label_00D6;
}
return;
Label_0140:
g.FillRectangle(brush, y, bottom, num3 - y, 1f);
y -= num;
num3 += num;
bottom -= 1f;
Label_0164:
if (bottom >= rect.Top)
{
goto Label_0140;
}
return;
Label_0172:
num = ((float) rect.Height) / ((float) (rect.Width * 2));
y = (rect.Top + rect.Bottom) / 2;
num3 = y;
for (float i = rect.Left; i <= rect.Right; i += 1f)
{
g.FillRectangle(brush, i, y, 1f, num3 - y);
y -= num;
num3 += num;
}
}
}
}
public static void FillRect(Graphics g, Rectangle rect, Color color)
{
using (SolidBrush brush = new SolidBrush(color))
{
g.FillRectangle(brush, rect);
}
}
public static GraphicsPath GetRoundPath(Rectangle rect, int roundSize)
{
GraphicsPath path = new GraphicsPath();
path.AddLine(rect.X, rect.Y + roundSize, rect.X, rect.Bottom - roundSize);
path.AddArc(rect.X, rect.Y, roundSize * 2, roundSize * 2, 180f, 45f);
path.AddLine(rect.X + roundSize, rect.Y, rect.Right - roundSize, rect.Y);
path.AddArc(rect.Right - (roundSize * 2), rect.Y, roundSize * 2, roundSize * 2, 270f, 90f);
path.AddLine(rect.Right, rect.Y + roundSize, rect.Right, rect.Bottom - roundSize);
path.AddArc((int) (rect.Right - (roundSize * 2)), (int) (rect.Bottom - (roundSize * 2)), (int) (roundSize * 2), (int) (roundSize * 2), 0f, 90f);
path.AddLine(rect.X + roundSize, rect.Bottom, rect.Right - roundSize, rect.Bottom);
path.AddArc(rect.X, rect.Bottom - (roundSize * 2), roundSize * 2, roundSize * 2, 90f, 90f);
path.CloseAllFigures();
return path;
}
public static void RoundRect(Graphics g, Rectangle rect, int roundSize, Brush brush, Pen pen)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
using (GraphicsPath path = GetRoundPath(rect, roundSize))
{
g.FillPath(brush, path);
g.DrawPath(pen, path);
}
}
public static void RoundRect(Graphics g, Rectangle rect, int roundSize, Color brushColor, Color penColor)
{
using (SolidBrush brush = new SolidBrush(brushColor))
{
using (Pen pen = new Pen(penColor))
{
RoundRect(g, rect, roundSize, brush, pen);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -