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

使用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
{
/// <summary>
/// RoundRect 的摘要说明。
/// </summary>
public class RoundRect
{
   /// <summary>
   /// 最大圆角半径
   /// </summary>
   protected const int MaxRoundRadius = 3;
   /// <summary>
   /// 最小矩形边长,用于自动处理圆角大小
   /// </summary>
   protected const int MinBorderLength = 20;
   /// <summary>
   /// 绘制一个圆角矩形.
   /// </summary>
   /// <param name="currentGraphicObject">当前屏幕的图形对象</param>
   /// <param name="lineColor">矩形线条的颜色</param>
   /// <param name="nLeft">矩形左上角X坐标</param>
   /// <param name="nTop">矩形左上角Y坐标</param>
   /// <param name="nRight">矩形右下角X坐标</param>
   /// <param name="nBottom">矩形右下角Y坐标</param>
   /// <param name="round">圆角的半径长度</param>
   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);
   }
   /// <summary>
   /// 绘制一个圆角矩形.
   /// </summary>
   /// <param name="currentGraphicObject">当前屏幕的图形对象</param>
   /// <param name="lineColor">矩形线条的颜色</param>
   /// <param name="rect">要绘制的矩形对象</param>
   /// <param name="round">圆角的半径长度</param>
   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);
   }
   /// <summary>
   /// 绘制一个圆角矩形.
   /// </summary>
   /// <param name="currentGraphicObject">当前屏幕的图形对象</param>
   /// <param name="lineColor">矩形线条的颜色</param>
   /// <param name="rect">要绘制的矩形对象</param>
   public static void DrawRoundRect(System.Drawing.Graphics currentGraphicObject,Color lineColor,Rectangle rect)
   {
    DrawRoundRect(currentGraphicObject,lineColor,rect.Left,rect.Top,rect.Right,rect.Bottom,2);
   }

   /// <summary>
   /// 填充一个圆角矩形.
   /// </summary>
   /// <param name="currentGraphicObject">当前屏幕的图形对象</param>
   /// <param name="lineColor">矩形线条的颜色</param>
   /// <param name="nLeft">矩形左上角X坐标</param>
   /// <param name="nTop">矩形左上角Y坐标</param>
   /// <param name="nRight">矩形右下角X坐标</param>
   /// <param name="nBottom">矩形右下角Y坐标</param>
   /// <param name="round">圆角的半径长度</param>
   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});
   }
   /// <summary>
   /// 填充一个圆角矩形.
   /// </summary>
   /// <param name="currentGraphicObject">当前屏幕的图形对象</param>
   /// <param name="lineColor">矩形线条的颜色</param>
   /// <param name="rect">要填充的矩形</param>
   /// <param name="indentSize">填充区域针对矩形的缩进距离</param>
   /// <param name="round">圆角的半径长度</param>
   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);
   }
   /// <summary>
   /// 填充一个圆角矩形.
   /// </summary>
   /// <param name="currentGraphicObject">当前屏幕的图形对象</param>
   /// <param name="lineColor">矩形线条的颜色</param>
   /// <param name="rect">要填充的矩形</param>
   public static void FillRoundRect(System.Drawing.Graphics currentGraphicObject,Color lineColor,Rectangle rect)
   {
    FillRoundRect(currentGraphicObject,lineColor,rect,0,2);
   }    
}
}

使用 echarts 中的 graphic 绘制矩形并可拖拽,可以参考以下步骤: 1. 在 echarts 中添加一个 graphic 组件,并设置其类型为 rect。 ```javascript { type: 'rect', id: 'draggableRect', draggable: true, shape: { x: 100, y: 100, width: 50, height: 50 } } ``` 2. 给 graphic 组件添加拖拽事件。 ```javascript myChart.getZr().on('mousemove', function (e) { if (dragging) { var target = graphic.get('draggableRect'); var pos = myChart.convertFromPixel({ seriesIndex: 0 }, [e.offsetX, e.offsetY]); target.attr('position', [pos[0] - startX, pos[1] - startY]); } }); ``` 3. 监听鼠标按下和释放事件,记录拖拽起始位置。 ```javascript var startX, startY, dragging = false; myChart.getZr().on('mousedown', function (e) { var target = graphic.get('draggableRect'); if (target) { var pos = myChart.convertFromPixel({ seriesIndex: 0 }, [e.offsetX, e.offsetY]); var rectShape = target.shape; if (pos[0] >= rectShape.x && pos[0] <= rectShape.x + rectShape.width && pos[1] >= rectShape.y && pos[1] <= rectShape.y + rectShape.height) { startX = pos[0] - target.position[0]; startY = pos[1] - target.position[1]; dragging = true; } } }); myChart.getZr().on('mouseup', function (e) { dragging = false; }); ``` 完整代码如下: ```javascript var myChart = echarts.init(document.getElementById('main')); myChart.setOption({ graphic: { elements: [{ type: 'rect', id: 'draggableRect', draggable: true, shape: { x: 100, y: 100, width: 50, height: 50 }, style: { fill: '#fff', stroke: '#000' } }] }, xAxis: { type: 'value' }, yAxis: { type: 'value' }, series: [{ type: 'scatter', data: [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]] }] }); var startX, startY, dragging = false; myChart.getZr().on('mousedown', function (e) { var target = myChart.getZr().handler.findHover(e.offsetX, e.offsetY).target; if (target && target.id === 'draggableRect') { var pos = myChart.convertFromPixel({ seriesIndex: 0 }, [e.offsetX, e.offsetY]); var rectShape = target.shape; if (pos[0] >= rectShape.x && pos[0] <= rectShape.x + rectShape.width && pos[1] >= rectShape.y && pos[1] <= rectShape.y + rectShape.height) { startX = pos[0] - target.position[0]; startY = pos[1] - target.position[1]; dragging = true; } } }); myChart.getZr().on('mouseup', function (e) { dragging = false; }); myChart.getZr().on('mousemove', function (e) { if (dragging) { var target = myChart.getZr().handler.findHover(e.offsetX, e.offsetY).target; if (target && target.id === 'draggableRect') { var pos = myChart.convertFromPixel({ seriesIndex: 0 }, [e.offsetX, e.offsetY]); target.attr('position', [pos[0] - startX, pos[1] - startY]); myChart.setOption({ graphic: { elements: [{ type: 'rect', id: 'draggableRect', draggable: true, shape: { x: pos[0] - startX, y: pos[1] - startY, width: 50, height: 50 }, style: { fill: '#fff', stroke: '#000' } }] } }); } } }); ``` 这样就能在 echarts 中绘制一个可拖拽的矩形了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值