找了很久也找不到:C# 圆角矩形 画法,请告知

解决方案 »

  1.   

    using System.Drawing.Drawing2D;
    protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
    {
    GraphicsPath oPath = new GraphicsPath();
    int x=0;
    int y=0;
    int w=Width;
    int h=Height;
    int a=40;
    Graphics g=CreateGraphics();
    oPath.AddArc(x,y,a,a,180,90);
    oPath.AddArc(w-a,y,a,a,270,90);
    oPath.AddArc(w-a/2,h-a/2,a/2,a/2,0,90);
    oPath.AddArc(x,h-a,a,a,90,90);
    oPath.CloseAllFigures();
    Region=new Region(oPath);
    }
      

  2.   

    谢谢,
    又学会一样东西.
    但我想知道怎样在Graphics加入方法
     public void DrawRoundRect(Pen pen,int x,int y,int width,int height,int arcHeight,int arcWidth)
      

  3.   

    我所给出的方法只是窗体的外形进行了重绘。显示的界面是一个有4个圆角的窗口。其实上面的代码同样适用于在载体的表面上绘图。我的代码在结束部分用的是修改窗体的Region。如果你想在载体的表面绘图。只需要更改事件或写到一个自定义的函数里。然后用载体的DrawPath输出就行了~~~
      

  4.   

    public GraphicsPath DrawRoundRect(int x,int y,int width,int height,int arcWidth)
    {
    GraphicsPath gp=new GraphicsPath();
    gp.AddArc(x,y,arcWidth,arcWidth,180,90);
    gp.AddArc(width-arcWidth,y,arcWidth,arcWidth,270,90);
    gp.AddArc(width -arcWidth,height-arcWidth,arcWidth,arcWidth,0,90);
    gp.AddArc(x,height-arcWidth,arcWidth,arcWidth,90,90);
    gp.CloseAllFigures();
    return gp;
    }这对你的要求的函数进行了一些修改。返回值是一个GraphicsPath。同时,你的要求是的圆角其实是椭圆,我简化成了正圆。你可以在上面代码的基础上改成椭圆。但要改的不仅仅是arcWidth一个参数。还要修改后面的角度(有点麻烦)。
      

  5.   

    我用的方法是先绘制出窗体的四个圆角,然后使用GraphicsPath 的CloseAllFigures()属性,将四个圆角首尾相连(一定要注意每个圆角的起点和终点的位置。不然绘制出来的图形会乱掉),也就是所谓的封合。CloseAllFigures()的作用是用直线将GraphicsPath 中非封合的部分连接起来。最后设置载体的Region。