📄 treedrawhelper.cs
字号:
namespace Imps.Client.Pc.BizControls
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class TreeDrawHelper
{
public static void DrawImage(Graphics g, Rectangle rect, Image image, bool bOriginalSize)
{
if (image != null)
{
try
{
if (bOriginalSize)
{
int x;
int y;
if (rect.Width > image.Width)
{
x = rect.Left + ((rect.Width - image.Width) / 2);
}
else
{
x = rect.Left;
}
if (rect.Height > image.Height)
{
y = rect.Top + ((rect.Height - image.Height) / 2);
}
else
{
y = rect.Top;
}
Rectangle rectangle = new Rectangle(new Point(x, y), image.Size);
g.DrawImage(image, rectangle);
}
else
{
g.DrawImage(image, rect);
}
}
catch
{
}
}
}
public static void DrawRectangle(Graphics g, Rectangle border, Color borderColor)
{
using (Pen pen = new Pen(borderColor))
{
g.DrawRectangle(pen, border);
}
}
public static void DrawRectangle(Graphics g, Rectangle border, Color borderColor, Rectangle filler, Color fillerColor)
{
DrawRectangle(g, border, borderColor);
FillRectangle(g, filler, fillerColor);
}
public static void DrawString(Graphics g, Font font, Color color, Rectangle rect, string text, StringFormat format)
{
using (SolidBrush brush = new SolidBrush(color))
{
g.DrawString(text, font, brush, (float) rect.Left, (float) rect.Top, format);
}
}
public static void DrawString(Graphics g, Font font, SolidBrush brush, Rectangle rect, string text, StringFormat sf)
{
if (rect.Width != 0)
{
g.DrawString(text, font, brush, rect, sf);
}
}
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 FillRectangle(Graphics g, Rectangle filler, Color fillerColor)
{
using (SolidBrush brush = new SolidBrush(fillerColor))
{
g.FillRectangle(brush, filler);
}
}
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, 90f);
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 + -