# 使用Graphic画圆角矩形和填充圆角矩形的方法

2008-01-05 21:57
 /**   Copyright (c) 2004-2007 D8HOME.CN *   All rights reserved.* * http://www.d8home.cn/* * This library is only free for any non commercial usage. In the case of* modifying and/or redistributing the code, it's obligate to retain* the original copyright notice.* */using System;using System.Windows.Forms;using System.Drawing; namespace D8Drawing{/// /// RoundRect 的摘要说明。/// public class RoundRect{   ///    /// 最大圆角半径   ///    protected const int MaxRoundRadius = 3;   ///    /// 最小矩形边长，用于自动处理圆角大小   ///    protected const int MinBorderLength = 20;   ///    /// 绘制一个圆角矩形.   ///    /// 当前屏幕的图形对象   /// 矩形线条的颜色   /// 矩形左上角X坐标   /// 矩形左上角Y坐标   /// 矩形右下角X坐标   /// 矩形右下角Y坐标   /// 圆角的半径长度   public static void DrawRoundRect(System.Drawing.Graphics currentGraphicObject,Color lineColor,int nLeft,int nTop,int nRight,int nBottom,int round)    {    if(round > MaxRoundRadius)    {     round = MaxRoundRadius;    }    else if( round < 0)    {     round = 0;    }    if(Math.Abs(nRight - nLeft) < MinBorderLength && Math.Abs(nBottom - nTop) < MinBorderLength)    {     round = 1;    }     Point Polygon1 = new Point (nLeft+round,nTop);    Point Polygon2 = new Point (nRight-round+1,nTop);     Point Polygon3 = new Point (nLeft,nTop+round);    Point Polygon4 = new Point (nRight+1,nTop+round);     Point Polygon5 = new Point (nLeft,nBottom-round);    Point Polygon6 = new Point (nRight+1,nBottom-round);     Point Polygon7 = new Point (nLeft+round,nBottom+1);    Point Polygon8 = new Point (nRight-round,nBottom+1);     //四条主线(上下左右)    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon1.X,Polygon1.Y,Polygon2.X,Polygon2.Y);    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon7.X,Polygon7.Y,Polygon8.X,Polygon8.Y);    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon3.X,Polygon3.Y,Polygon5.X,Polygon5.Y);    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon4.X,Polygon4.Y,Polygon6.X,Polygon6.Y);     //四个边角    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon1.X,Polygon1.Y,Polygon3.X,Polygon3.Y);    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon2.X,Polygon2.Y,Polygon4.X,Polygon4.Y);    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon5.X,Polygon5.Y,Polygon7.X,Polygon7.Y);    currentGraphicObject.DrawLine(new System.Drawing.Pen(lineColor),Polygon6.X,Polygon6.Y,Polygon8.X,Polygon8.Y);    }   ///    /// 绘制一个圆角矩形.   ///    /// 当前屏幕的图形对象   /// 矩形线条的颜色   /// 要绘制的矩形对象   /// 圆角的半径长度   public static void DrawRoundRect(System.Drawing.Graphics currentGraphicObject,Color lineColor,Rectangle rect,int round)   {    DrawRoundRect(currentGraphicObject,lineColor,rect.Left,rect.Top,rect.Right,rect.Bottom,round);   }   ///    /// 绘制一个圆角矩形.   ///    /// 当前屏幕的图形对象   /// 矩形线条的颜色   /// 要绘制的矩形对象   public static void DrawRoundRect(System.Drawing.Graphics currentGraphicObject,Color lineColor,Rectangle rect)   {    DrawRoundRect(currentGraphicObject,lineColor,rect.Left,rect.Top,rect.Right,rect.Bottom,2);   }    ///    /// 填充一个圆角矩形.   ///    /// 当前屏幕的图形对象   /// 矩形线条的颜色   /// 矩形左上角X坐标   /// 矩形左上角Y坐标   /// 矩形右下角X坐标   /// 矩形右下角Y坐标   /// 圆角的半径长度   public static void FillRoundRect(System.Drawing.Graphics currentGraphicObject,Color fillColor,int nLeft,int nTop,int nRight,int nBottom,int round)    {    if(round > MaxRoundRadius)    {     round = MaxRoundRadius;    }    else if( round < 0)    {     round = 0;    }    if(Math.Abs(nRight - nLeft) < MinBorderLength && Math.Abs(nBottom - nTop) < MinBorderLength)    {     round = 1;    }     Point Polygon1 = new Point (nLeft+round,nTop);    Point Polygon2 = new Point (nRight-round+1,nTop);     Point Polygon3 = new Point (nLeft,nTop+round);    Point Polygon4 = new Point (nRight+1,nTop+round);     Point Polygon5 = new Point (nLeft,nBottom-round);    Point Polygon6 = new Point (nRight+1,nBottom-round);     Point Polygon7 = new Point (nLeft+round,nBottom+1);    Point Polygon8 = new Point (nRight-round,nBottom+1);      currentGraphicObject.FillPolygon(new System.Drawing.SolidBrush(fillColor),new Point[]{   Polygon1,                      Polygon3,                      Polygon5,                      Polygon7,                      Polygon8,                      Polygon6,                      Polygon4,                      Polygon2});   }   ///    /// 填充一个圆角矩形.   ///    /// 当前屏幕的图形对象   /// 矩形线条的颜色   /// 要填充的矩形   /// 填充区域针对矩形的缩进距离   /// 圆角的半径长度   public static void FillRoundRect(System.Drawing.Graphics currentGraphicObject,Color lineColor,Rectangle rect,int indentSize,int round)   {    FillRoundRect(currentGraphicObject,lineColor,rect.Left+indentSize,rect.Top+indentSize,rect.Right-indentSize+1,rect.Bottom-indentSize+1,round);   }   ///    /// 填充一个圆角矩形.   ///    /// 当前屏幕的图形对象   /// 矩形线条的颜色   /// 要填充的矩形   public static void FillRoundRect(System.Drawing.Graphics currentGraphicObject,Color lineColor,Rectangle rect)   {    FillRoundRect(currentGraphicObject,lineColor,rect,0,2);   }    }}

#### GDI+ /GDI Plus 绘制圆角矩形 Draw retangle round

2014-09-09 14:52:06

#### drawRoundRect方法：绘制圆角矩形

2014-08-25 23:23:31

#### GDI+绘制圆角矩形

2014-07-08 23:05:49

#### MFC绘制圆角矩形

2014-08-20 22:50:56

#### C# GDI+ 绘制圆角实心矩形

2010-07-12 13:42:00

#### android中圆角矩形的绘制

2017-04-17 14:44:45

#### Canvas---Canvas版画图、圆角矩形、圆形、矩形、图形填充、mvc模式重新整合代码版

2015-01-27 15:08:33

#### 详述Canvas(五)/绘制圆角矩形

2016-04-27 10:34:28

#### GDI+ 如何绘制圆角矩形(vc++)

2016-06-08 21:14:09

#### C#绘画圆角矩形的两种方式

2017-01-13 14:04:18