Welcome

首页 / 软件开发 / C# / C#矩形圆角绘制改进版

C#矩形圆角绘制改进版2013-11-13一、前言

上一篇绘制矩形圆角的方式不够完善,感觉写的太过于复杂,将简单的问题复杂化了,本文对此进行了相应的改进,增强对各个圆角的半径的控制。绘制后的圆角效果图如下:

二、圆角半径设计

对于矩行而言,圆角分为左上角、右上角、左下角和右下角。每一个角都会存在相应的半径,用于控制每一个圆角的绘制。设计如下:

public struct ArcRadius{private int _rightBottom;private int _rightTop;private int _leftBottom;private int _leftTop;public static readonly ArcRadius Empty =new ArcRadius(0);public ArcRadius(int radiusLength){if (radiusLength < 0){radiusLength = 0;}this._rightBottom = this._rightTop = this._leftBottom = this._leftTop = radiusLength;}public ArcRadius(int leftTop, int rightTop, int leftBottom, int rightBottom){this._rightBottom = rightBottom < 0 ? 0 : rightBottom;this._rightTop = rightTop < 0 ? 0 : rightTop;this._leftBottom = leftBottom < 0 ? 0 : leftBottom;this._leftTop = leftTop < 0 ? 0 : leftTop;}private bool IsAllEqual(){return ((this.RightBottom == this.RightTop) && (this.RightBottom == this.LeftBottom)) && (this.RightBottom == this.LeftTop);}public int All{get{if (!IsAllEqual()){return -1;}return this.RightBottom;}set{if (value < 0){value = 0;}this.RightBottom = this.RightTop = this.LeftBottom = this.LeftTop = value;}}public int LeftTop{get{return this._leftTop;}set{if (value < 0){value = 0;}this._leftTop = value;}} public int RightTop{get{return this._rightTop;}set{if (value < 0){value = 0;}this._rightTop = value;}} public int LeftBottom{get{return this._leftBottom;}set{if (value < 0){value = 0;}this._leftBottom = value;}}public int RightBottom{get{return this._rightBottom;}set{if (value < 0){value = 0;}this._rightBottom = value;}}public static bool operator ==(ArcRadius p1, ArcRadius p2){return ((((p1.RightTop == p2.RightTop) && (p1.RightBottom == p2.RightBottom))&& (p1.LeftBottom == p2.LeftBottom)) && (p1.LeftTop == p2.LeftTop));}public static bool operator !=(ArcRadius p1, ArcRadius p2){return !(p1 == p2);}public override string ToString(){return LeftTop + ", " + RightTop + ", " + LeftBottom + ", " + RightBottom;}}