Delphi:RoundRect with Gradient

时间:2012-01-05 06:43:10

标签: delphi graphics gradient rounding

如何使用GDI或GDI +在Custom Draw Item的列表视图中绘制圆形矩形并用渐变填充?

例如,要绘制渐变,我们可以使用GradientFillCanvas

中的GraphUtil.pas

2 个答案:

答案 0 :(得分:1)

GDI

我在Gradient v.2.71

中找到了一种方法
procedure TForm1.FormPaint(Sender: TObject);
var
  r: trect;
  bm: tbitmap;
  X, Y, W, H, S: Integer;
  Rgn: THandle;
  Org: TPoint;
begin
  bm := tbitmap.Create;
  bm.Width := 1;
  bm.Height := 255;
  r.Create(0, 0, 1, 255);
  GradientFillCanvas(bm.Canvas, clred, clblue, r, gdVertical);
  r.Empty;

  X := 50;
  Y := 50;
  W := 200;
  H := 40;

  Rgn := CreateRoundRectRgn(X, Y, X + W, Y + H, 3, 3);
  GetWindowOrgEx(Canvas.Handle, Org);
  OffsetRgn(Rgn, -Org.X, -Org.Y);
  SelectClipRgn(Canvas.Handle, Rgn);
  Canvas.StretchDraw(Rect(X, Y, X + W, Y + H), bm);
  SelectClipRgn(Canvas.Handle, 0);
  DeleteObject(Rgn);

  bm.Free;
end;

GDI +

function CreateRoundRectangle(rectangle: TGPRect;
  radius: integer): TGPGraphicsPath;
var
  path : TGPGraphicsPath;
  l, t, w, h, d : integer;
begin
  path := TGPGraphicsPath.Create;
  l := rectangle.X;
  t := rectangle.y;
  w := rectangle.Width;
  h := rectangle.Height;
  d := radius div 2; // divide by 2

  // the lines beween the arcs are automatically added by the path
  path.AddArc(l, t, d, d, 180, 90); // topleft
  path.AddArc(l + w - d, t, d, d, 270, 90); // topright
  path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
  path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
  path.CloseFigure();
  result := path;
end;



path:=CreateRoundRectangle(R,20);
FillPath(lingrbrush,path) 

答案 1 :(得分:0)

    var
      r : TRect;
      b : TBitmap;
      begin
      r.Left := 0;
      r.Top := 0;
      r.Height := 100;
      r.Width := 150;

        b := TBitmap.Create;
          pb2.Canvas.Brush.Bitmap := TBitmap.Create;
          pb2.Canvas.Pen.Color := clBlack;

          b.Width := 150;
          b.Height := 100;
                        GradientFillCanvas(b.Canvas, clred, clblue, r, gdVertical);
          pb2.Canvas.Brush.Bitmap.Assign(b);
          r.Height := 70; // I don't know why that not works with 100
          pb2.Canvas.RoundRect(r , 25, 25);

          pb2.Canvas.Brush.Bitmap.Free;
             b.Free;
           end;

我的话Delphi XE7