/// <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); }
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="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); } }
/// <summary> /// 根据普通矩形得到圆角矩形的路径 /// </summary> /// <param name="rect">原始矩形</param> /// <param name="radius">半径</param> /// <param name="corners">圆角的方向</param> /// <returns>图形路径</returns> public static GraphicsPath GetRoundedRectPath(this Rectangle rect, int radius, CcCtrlCorner corners = CcCtrlCorner.All) { var diameter = 2 * radius; // 把圆角矩形分成八段直线、弧的组合,依次加到路径中 var path = new GraphicsPath(); if ((corners & CcCtrlCorner.TopLeft) == CcCtrlCorner.TopLeft) { //左上角 path.AddArc(rect.X, rect.Y, diameter, diameter, 180F, 90F); } else { //顶部直线(从左往右画) path.AddLine(rect.X, rect.Y, rect.Right - diameter, rect.Y); } if ((corners & CcCtrlCorner.TopRight) == CcCtrlCorner.TopRight) { //右上角 path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270F, 90F); } else { //右边直线(从上往下画) path.AddLine(rect.Right, rect.Y, rect.Right, rect.Bottom - diameter); } if ((corners & CcCtrlCorner.BottomRight) == CcCtrlCorner.BottomRight) { //右下角 path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0F, 90F); } else { //底部直线(从右往左画) path.AddLine(rect.Right, rect.Bottom, rect.X + diameter, rect.Bottom); } if ((corners & CcCtrlCorner.BottomLeft) == CcCtrlCorner.BottomLeft) { //左下角 path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90F, 90F); } else { //左边直线(从下往上) path.AddLine(rect.X, rect.Bottom, rect.X, rect.Y + diameter); } path.CloseAllFigures(); return(path); }