/// <summary> /// 画按钮的方角矩形 /// </summary> /// <param name="graphics"></param> /// <param name="rect">矩形区域</param> /// <param name="fillBrush">填充画刷</param> /// <param name="borderPen">边框画笔</param> /// <param name="borderSize">边框大小</param> /// <param name="borderOpen">边框开放的方向</param> /// <param name="openPen">开放画笔</param> public static void DrawButtonCornerRect(this Graphics graphics, Rectangle rect, Brush fillBrush, Pen borderPen, int borderSize, CcCtrlPosition borderOpen = CcCtrlPosition.None, Pen openPen = null) { if (null == graphics) { return; } //不抗锯齿 graphics.SmoothingMode = SmoothingMode.None; var drawBorder = (borderSize > 0 && borderPen != null); var offset = (drawBorder && 1 == borderSize) ? Win32.SIZE_SYSTEM : 0; var drawRect = new Rectangle { X = rect.X, Y = rect.Y, Width = rect.Width - offset, Height = rect.Height - offset }; if (fillBrush != null) { graphics.FillRectangle(fillBrush, drawRect); } if (drawBorder) { graphics.DrawRectangle(borderPen, drawRect); graphics.DrawOpenLine(drawRect, fillBrush, borderSize, 0, borderOpen, openPen); } }
/// <summary> /// 画按钮 /// </summary> /// <param name="graphics"></param> /// <param name="rect">矩形区域</param> /// <param name="fillBrush">填充画刷</param> /// <param name="borderPen">边框画笔</param> /// <param name="borderSize">边框大小</param> /// <param name="radius">圆角半径</param> /// <param name="radiusCorners">圆角数量</param> /// <param name="font">字体</param> /// <param name="textColor">文字颜色</param> /// <param name="text">文字内容</param> /// <param name="borderOpen">边框开放的方向</param> /// <param name="openPen">开放画笔</param> public static void DrawButton(this Graphics graphics, Rectangle rect, Brush fillBrush , Pen borderPen, int borderSize, int radius, CcCtrlCorner radiusCorners , Font font, Color textColor, string text, CcCtrlPosition borderOpen = CcCtrlPosition.None, Pen openPen = null) { //画矩形 graphics.DrawButtonRect(rect, fillBrush, borderPen, borderSize, radius, radiusCorners, borderOpen, openPen); //画文字 graphics.DrawButtonText(rect, text, font, textColor); }
/// <summary> /// 画按钮的圆角矩形 /// </summary> /// <param name="graphics"></param> /// <param name="rect">矩形区域</param> /// <param name="fillBrush">填充画刷</param> /// <param name="borderPen">边框画笔</param> /// <param name="borderSize">边框大小</param> /// <param name="radius">圆角半径</param> /// <param name="radiusCorners">圆角数量</param> /// <param name="borderOpen">边框开放的方向</param> /// <param name="openPen">开放画笔</param> public static void DrawButtonRoundRect(this Graphics graphics, Rectangle rect, Brush fillBrush , Pen borderPen, int borderSize, int radius, CcCtrlCorner radiusCorners = CcCtrlCorner.All , CcCtrlPosition borderOpen = CcCtrlPosition.None, Pen openPen = null) { if (null == graphics || radius <= 0) { return; } //抗锯齿 graphics.SmoothingMode = SmoothingMode.AntiAlias; var drawBorder = (borderSize > 0 && borderPen != null); var delta = Win32.SIZE_SYSTEM; //抗锯齿模式下,系统有1个像素的渐变 var drawRect = new Rectangle { X = rect.X, Y = rect.Y, Width = rect.Width - delta, Height = rect.Height - delta }; if (fillBrush != null) { if (drawBorder) { var offset = (int)Math.Floor(borderSize / 2.0F); var fillRect = new Rectangle { X = rect.X + offset, Y = rect.Y + offset, Width = rect.Width - borderSize, Height = rect.Height - borderSize }; graphics.FillPath(fillBrush, fillRect.GetRoundedRectPath(radius, radiusCorners)); } else { graphics.FillPath(fillBrush, drawRect.GetRoundedRectPath(radius, radiusCorners)); } } if (drawBorder) { graphics.DrawPath(borderPen, drawRect.GetRoundedRectPath(radius, radiusCorners)); graphics.DrawOpenLine(rect, fillBrush, borderSize, radius, borderOpen, openPen); } }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (null == value) { return(new CcBorder()); } //从字符串解析 if (value is string stringValue) { if (stringValue.Length <= 0) { return(new CcBorder()); } int size = 0, radius = 0; CcCtrlPosition direction = CcCtrlPosition.None; CcCtrlCorner corners = CcCtrlCorner.None; stringValue.Split(Separator).ForEach(s => { if (s.StartsWith(KeySize)) { size = (int)IntConverter.ConvertFromString(context, culture, s.Substring(KeySize.Length)); } else if (s.StartsWith(KeyDirection)) { direction = (CcCtrlPosition)PositionConverter.ConvertFromString(context, culture, s.Substring(KeyDirection.Length)); } else if (s.StartsWith(KeyRadius)) { radius = (int)IntConverter.ConvertFromString(context, culture, s.Substring(KeyRadius.Length)); } else if (s.StartsWith(KeyCorners)) { corners = (CcCtrlCorner)CornerConverter.ConvertFromString(context, culture, s.Substring(KeyCorners.Length)); } }); return(new CcBorder { Size = size, OpenDirection = direction, Radius = radius, RadiusCorners = corners }); } return(base.ConvertFrom(context, culture, value)); }
/// <summary> /// 画按钮的矩形 /// </summary> /// <param name="graphics"></param> /// <param name="rect">矩形区域</param> /// <param name="fillBrush">填充画刷</param> /// <param name="borderPen">边框画笔</param> /// <param name="borderSize">边框大小</param> /// <param name="radius">圆角半径</param> /// <param name="borderOpen">边框开放的方向</param> /// <param name="borderOpen">边框开放的方向</param> /// <param name="openPen">开放画笔</param> public static void DrawButtonRect(this Graphics graphics, Rectangle rect, Brush fillBrush , Pen borderPen, int borderSize, int radius, CcCtrlCorner radiusCorners = CcCtrlCorner.All , CcCtrlPosition borderOpen = CcCtrlPosition.None, Pen openPen = null) { if (null == graphics) { return; } if (radius > 0) { graphics.DrawButtonRoundRect(rect, fillBrush, borderPen, borderSize, radius, radiusCorners, borderOpen, openPen); } else { graphics.DrawButtonCornerRect(rect, fillBrush, borderPen, borderSize, borderOpen, openPen); } }
/// <summary> /// 获取圆角的位置 /// </summary> /// <param name="openPosition">边框开放的位置</param> /// <returns></returns> public static CcCtrlCorner GetCorners(CcCtrlPosition openPosition) { switch (openPosition) { case CcCtrlPosition.Left: return(CcCtrlCorner.Right); case CcCtrlPosition.Top: return(CcCtrlCorner.Bottom); case CcCtrlPosition.Right: return(CcCtrlCorner.Left); case CcCtrlPosition.Bottom: return(CcCtrlCorner.Top); case CcCtrlPosition.None: default: return(CcCtrlCorner.All); } }
/// <summary> /// 获取开放的线条 /// </summary> /// <param name="rect">原始矩形</param> /// <param name="open">开放的方向</param> /// <param name="borderSize">边框大小</param> /// <param name="radius">圆角半径</param> /// <returns>Item1:起始点;Item2:终结点</returns> public static Tuple <Point, Point> GetOpenLine(this Rectangle rect, CcCtrlPosition open, int borderSize, int radius) { if (null == rect) { return(null); } var delta = (radius > 0) ? Win32.SIZE_SYSTEM : 0; var leftX_H = rect.X + borderSize - (borderSize == 1 ? delta : 0); var leftX_V = rect.X + borderSize / 2; var rightX_H = rect.Right - borderSize - (borderSize == 1 ? 0 : delta); var rightX_V = rect.Right - borderSize / 2 - delta; var topY_H = rect.Y + borderSize / 2; var topY_V = rect.Y + borderSize - (borderSize == 1 ? delta : 0); var bottomY_H = rect.Bottom - borderSize / 2 - delta; var bottomY_V = rect.Bottom - borderSize - (borderSize == 1 ? 0 : delta); switch (open) { case CcCtrlPosition.Top: return(new Tuple <Point, Point>(new Point(leftX_H, topY_H), new Point(rightX_H, topY_H))); case CcCtrlPosition.Left: return(new Tuple <Point, Point>(new Point(leftX_V, topY_V), new Point(leftX_V, bottomY_V))); case CcCtrlPosition.Right: return(new Tuple <Point, Point>(new Point(rightX_V, topY_V), new Point(rightX_V, bottomY_V))); case CcCtrlPosition.Bottom: return(new Tuple <Point, Point>(new Point(leftX_H, bottomY_H), new Point(rightX_H, bottomY_H))); default: return(null); } }
/// <summary> /// 画开放边框的直线 /// </summary> /// <param name="graphics"></param> /// <param name="rect"></param> /// <param name="fillBrush"></param> /// <param name="borderSize"></param> /// <param name="radius">圆角半径</param> /// <param name="borderOpen"></param> /// <param name="openPen"></param> private static void DrawOpenLine(this Graphics graphics, Rectangle rect, Brush fillBrush, int borderSize, int radius, CcCtrlPosition borderOpen, Pen openPen = null) { var points = rect.GetOpenLine(borderOpen, borderSize, radius); if (null == points) { return; } //画圆角时留一个像素给系统渐变 var offset = radius > 0 ? Win32.SIZE_SYSTEM : 0; var newPen = (null == openPen); if (newPen) { if (fillBrush != null) { openPen = new Pen(fillBrush, borderSize + offset); } else { openPen = new Pen(Color.White, borderSize + offset); } } if (radius > 0) { //graphics.SmoothingMode = SmoothingMode.AntiAlias; } else { graphics.SmoothingMode = SmoothingMode.None; } graphics.DrawLine(openPen, points.Item1, points.Item2); if (newPen) { openPen.Dispose(); } }