📄 drawhelper.cs
字号:
namespace Imps.Client.Pc
{
using Imps.Client.Utils;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
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)
{
rect.Inflate(-shadowDepth, -shadowDepth);
FillRect(g, rect, brushColor);
DrawRectangleShadow(g, rect, shadowDepth, penColor);
}
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)
{
DrawAndFillRoundRect(g, rect, roundSize, backColor, penColor, shadowDepth, true);
}
public static void DrawAndFillRoundRect(Graphics g, Rectangle rect, int roundSize, Color backColor, Color penColor, int shadowDepth, bool darkInner)
{
GraphicsPath path;
int alpha = (shadowDepth > 0) ? (penColor.A / (shadowDepth + 1)) : 0;
Color color = penColor;
if (darkInner)
{
if (shadowDepth > 0)
{
color = Color.FromArgb(alpha, penColor);
}
}
else
{
alpha *= -1;
}
Label_0030:
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_0030;
}
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 DrawBackgroundImage(Graphics g, Image backgroundImage, Rectangle rect)
{
DrawBackgroundImage(g, backgroundImage, WrapMode.TileFlipXY, rect);
}
public static void DrawBackgroundImage(Graphics g, Image backgroundImage, WrapMode mode, Rectangle rect)
{
if (backgroundImage != null)
{
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetWrapMode(mode);
g.DrawImage(backgroundImage, rect, 0, 0, backgroundImage.Width, backgroundImage.Height, GraphicsUnit.Pixel, imageAttr);
}
}
public static void DrawBorder(Graphics g, Rectangle bounds, Color baseColor, Color lightColor, ButtonBorderStyle style)
{
if ((style == ButtonBorderStyle.Inset) || (style == ButtonBorderStyle.Outset))
{
int x = bounds.Left;
int num2 = bounds.Right - 1;
int y = bounds.Top;
int num4 = bounds.Bottom - 1;
System.Drawing.Point[] points = new System.Drawing.Point[] { new System.Drawing.Point(x + 1, num4), new System.Drawing.Point(num2, num4), new System.Drawing.Point(num2, y + 1) };
System.Drawing.Point[] pointArray2 = new System.Drawing.Point[] { new System.Drawing.Point(x, num4), new System.Drawing.Point(x, y), new System.Drawing.Point(num2, y) };
using (Pen pen = new Pen(baseColor))
{
using (Pen pen2 = new Pen(lightColor))
{
if (style == ButtonBorderStyle.Outset)
{
g.DrawLines(pen, points);
g.DrawLines(pen2, pointArray2);
}
else
{
g.DrawLines(pen2, points);
g.DrawLines(pen, pointArray2);
}
}
}
}
}
public static void DrawFocusRect(Graphics g, Rectangle rect, Color penColor)
{
rect.Width++;
rect.Height++;
ControlPaint.DrawBorder(g, rect, penColor, ButtonBorderStyle.Dashed);
}
public static void DrawImage(Graphics g, Image image, Rectangle rect)
{
try
{
g.DrawImage(image, rect);
}
catch
{
Debugger.Break();
}
}
public static void DrawImage(Graphics g, Image image, Rectangle rect, Color transColor)
{
try
{
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(transColor, transColor);
g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
}
catch
{
Debugger.Break();
}
}
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 DrawImageList(Graphics g, ImageList image, int index, Rectangle rect)
{
using (Image image2 = image.Images[index])
{
DrawImage(g, image2, new Rectangle(rect.Location, image2.Size));
}
}
public static void DrawRect(Graphics g, Rectangle rect, Color color)
{
ControlPaint.DrawBorder(g, rect, color, ButtonBorderStyle.Solid);
}
public static void DrawRectangle(Graphics g, Rectangle rect, Color backColor, Color borderColor, ButtonBorderStyle borderStyle)
{
using (Brush brush = new SolidBrush(backColor))
{
g.FillRectangle(brush, rect);
}
ControlPaint.DrawBorder(g, rect, borderColor, borderStyle);
}
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -