using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using Microsoft.Drawing;
namespace Microsoft.Windows.Forms
{
public static partial class RenderEngine
{
///
/// 获取线段与水平X轴夹角(角度表示)
///
/// 起点
/// 终点
/// 夹角
public static float GetLineDegrees(Point pt1, Point pt2)
{
return (float)MathEx.ToDegrees(Math.Atan(((double)pt2.Y - (double)pt1.Y) / ((double)pt2.X - (double)pt1.X)));
}
///
/// 获取包含线段的最小矩形(非空)
///
/// 起点
/// 终点
/// 矩形
public static Rectangle GetLineRect(Point pt1, Point pt2)
{
Rectangle rect = Rectangle.FromLTRB(Math.Min(pt1.X, pt2.X), Math.Min(pt1.Y, pt2.Y), Math.Max(pt1.X, pt2.X), Math.Max(pt1.Y, pt2.Y));
rect.Inflate(1, 1);
return rect;
}
///
/// 创建画刷,渲染背景和边框使用
///
/// 画刷区域
/// 基色
/// 基色位置1
/// 基色位置2
/// 是否反转
/// 渐变模式
/// 样式
/// 画刷
public static Brush CreateBrush(Rectangle rect, Color baseColor, float pos1, float pos2, bool reverse, LinearGradientMode mode, BlendStyle style)
{
Brush brush = null;
RectangleEx.MakeNotEmpty(ref rect);
switch (style)
{
case BlendStyle.Solid:
{
SolidBrush brushTmp = new SolidBrush(baseColor);
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.Gradient:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosGradient(baseColor, pos1, pos2, reverse, false, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.FadeIn:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosFadeIn(baseColor, pos1, pos2, reverse, false, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.FadeOut:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosFadeOut(baseColor, pos1, pos2, reverse, false, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.FadeInFadeOut:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosFadeInFadeOut(baseColor, pos1, pos2, reverse, false, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
default:
break;
}
return brush;
}
///
/// 创建画刷,渲染线段使用
///
/// 起点
/// 终点
/// 基色
/// 样式
/// 画刷
public static Brush CreateBrush(Point pt1, Point pt2, Color baseColor, BlendStyle style)
{
Brush brush = null;
switch (style)
{
case BlendStyle.Solid:
{
SolidBrush brushTmp = new SolidBrush(baseColor);
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.Gradient:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosGradient(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.FadeIn:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosFadeIn(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.FadeOut:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosFadeOut(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
case BlendStyle.FadeInFadeOut:
{
LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
//画刷设置
ColorBlend blendTmp = new ColorBlend();
Color[] colors;
float[] positions;
RenderEngine.GetColorPosFadeInFadeOut(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
blendTmp.Colors = colors;
blendTmp.Positions = positions;
//
brushTmp.InterpolationColors = blendTmp;
brush = brushTmp;
brushTmp = null;
}
break;
default:
break;
}
return brush;
}
///
/// 创建画笔
///
/// 画刷
/// 直线宽度
/// 虚线样式
/// 点线长度数组
/// 每个线段键帽样式
/// 直线的起点到短划线图案起始处的距离
/// 画笔
public static Pen CreatePen(Brush brush, int width, DashStyle style, float[] pattern, DashCap cap, float offset)
{
Pen pen = new Pen(brush);
pen.Width = width;
pen.DashStyle = style;
if (style == DashStyle.Custom && pattern != null && pattern.Length > 0)
pen.DashPattern = pattern;
pen.DashCap = cap;
pen.DashOffset = offset;
return pen;
}
}
}