zoukankan      html  css  js  c++  java
  • Graphics 绘制圆角矩形

    参考: http://www.cnblogs.com/xujh/archive/2007/04/17/717433.html

    使用 GraphicsPath -- 指定一系列的 Arc, Line

    先补充下 Arc 的用法:

    //...
    Graphics g = e.Graphics;
    int radius = 50;    //圆角半径
    Rectangle rectArc = new Rectangle(200, 200, 2*radius, 2*radius);  // 弧度区域
    
    g.DrawRectangle(Pens.Purple, rectArc);  //显示区域边框
    g.DrawArc(new Pen(Color.Red, 2), rectArc, 180, 90);  // 绘制弧线
    g.DrawArc(Pens.Blue, rectArc, 270, 90);

    生成的效果:

    Arc

    由此可以推测出圆角矩形中的弧线与直线的关系:

    Rounded_Rct

    所以最终代码为:

    // 要实现 圆角化的 矩形
    Rectangle rect = new Rectangle(180, 180, 200, 160);
    int cRadius = 15;  // 圆角半径
    
    // 指定图形路径, 有一系列 直线/曲线 组成
    GraphicsPath myPath = new GraphicsPath();
    myPath.StartFigure();
    myPath.AddArc(new Rectangle(new Point(rect.X, rect.Y), new Size(2 * cRadius, 2 * cRadius)), 180, 90);
    myPath.AddLine(new Point(rect.X + cRadius, rect.Y), new Point(rect.Right - cRadius, rect.Y));
    myPath.AddArc(new Rectangle(new Point(rect.Right - 2 * cRadius, rect.Y), new Size(2 * cRadius, 2 * cRadius)), 270, 90);
    myPath.AddLine(new Point(rect.Right, rect.Y + cRadius), new Point(rect.Right, rect.Bottom - cRadius));
    myPath.AddArc(new Rectangle(new Point(rect.Right - 2 * cRadius, rect.Bottom - 2 * cRadius), new Size(2 * cRadius, 2 * cRadius)), 0, 90);
    myPath.AddLine(new Point(rect.Right - cRadius, rect.Bottom), new Point(rect.X + cRadius, rect.Bottom));
    myPath.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * cRadius), new Size(2 * cRadius, 2 * cRadius)), 90, 90);
    myPath.AddLine(new Point(rect.X, rect.Bottom - cRadius), new Point(rect.X, rect.Y + cRadius));
    myPath.CloseFigure();
    g.DrawPath(new Pen(Color.Red, 1), myPath);
  • 相关阅读:
    视图和同义词的区别
    【MooTools】自定义滚动条小插件
    有理想的程序员必须知道的15件事
    革新:.NET 2.0的自定义配置文件体系初探
    我的2006年学习计划
    为ASP.NET 2.0配置数据源
    通用异常处理框架
    泛型的序列化问题
    实战SVN For Apache2(二)
    LightningFramework系列(一、初步总架构图)
  • 原文地址:https://www.cnblogs.com/ccding13/p/2987919.html
Copyright © 2011-2022 走看看