如何在TcxImage Canvas上绘制Direct2D画布?

时间:2011-05-01 23:10:16

标签: delphi devexpress direct2d

我有一个创建GridPatternBitmapBrush的Direct2D演示,然后在paint方法中绘制TForm上的网格图案。你如何让Direct2D GridPatternBitmapBrush出现在表格的TcxImage.Canvas(DeveloperExpress TImage)内容中?

procedure TFormAdvGeometries.Create_FRadialGradientBrush;
var
//  aGradientStops: array of TD2D1GradientStop;
//  aGradBrushProps: TD2D1RadialGradientBrushProperties;
//  aGradStopsCollection: ID2D1GradientStopCollection;

  gradColors: array of TColor;
begin
  SetLength(gradColors, 3);
  gradColors[0] := TColor($00D7FF); // Gold (D2D1Helper.h)
  gradColors[1] := TColor($00A5FF); // Orange (D2D1Helper.h)
  gradColors[2] := TColor($0045FF); // OrangeRed (D2D1Helper.h)


  // this is a place-holder.
  // Code below assumes equal spread for positions in gradient stops
  FRadialGradientBrush := d2dCanvas.CreateBrush(
    gradColors,
    D2D1PointF(330, 330),
    D2D1PointF(140, 140),
    140,
    140
    );
end;

procedure TFormAdvGeometries.Create_FGridPatternBitmapBrush;
var
  gridBrush: ID2D1SolidColorBrush;
  bmpBrushProps: D2D1_BITMAP_BRUSH_PROPERTIES;
  bitmapRenderTarget: ID2D1BitmapRenderTarget;
  bmpSize: D2D_SIZE_F;
  gridBitmap: ID2D1Bitmap;
begin
  bmpSize.width := 10;
  bmpSize.height := 10;
  d2dCanvas.RenderTarget.CreateCompatibleRenderTarget(
    @bmpSize, nil, nil, 0, bitmapRenderTarget);
  bitmapRenderTarget.CreateSolidColorBrush(
    D2D1ColorF(0.93, 0.94, 0.96, 1), nil, gridBrush);
  bitmapRenderTarget.BeginDraw;
  bitmapRenderTarget.FillRectangle(Rect(0, 0, 10, 1), gridBrush);
  bitmapRenderTarget.FillRectangle(Rect(0, 0, 1, 10), gridBrush);
  bitmapRenderTarget.EndDraw;
  bitmapRenderTarget.GetBitmap(gridBitmap);
  bmpBrushProps.extendModeX := D2D1_EXTEND_MODE_WRAP;
  bmpBrushProps.extendModeY := D2D1_EXTEND_MODE_WRAP;
  bmpBrushProps.interpolationMode := 0; // could be 1
  d2dCanvas.RenderTarget.CreateBitmapBrush(
    gridBitmap, @bmpBrushProps, nil, FGridPatternBitmapBrush);
end;

procedure TFormAdvGeometries.CreateDeviceResources;
begin
  Create_FRadialGradientBrush;
  Create_FGridPatternBitmapBrush;
end;

procedure TFormAdvGeometries.Paint;
var defMatrix: TD2DMatrix3x2F;
begin
  inherited;

  CreateDeviceResources;

  d2dCanvas.BeginDraw;
  try
    d2dCanvas.RenderTarget.GetTransform (defMatrix);

    // fill with white color the whole window
    d2dCanvas.RenderTarget.Clear(D2D1ColorF(clWhite));

    // fill canvas with little blue rectangles
    d2dCanvas.Brush.Handle := FGridPatternBitmapBrush;
    d2dCanvas.Rectangle(0, 0, ClientWidth + 50, ClientHeight + 50);

    // reset standard transformation
    d2dCanvas.RenderTarget.SetTransform (defMatrix);
  finally
    d2dCanvas.EndDraw;
  end;
end;

1 个答案:

答案 0 :(得分:0)

您需要更改d2dCanvas的构造函数。

目前正是

  d2dCanvas := TDirect2DCanvas.Create(Handle);

传递表单句柄以创建画布。

我没有Delphi 2010(它似乎附带了演示所需的单位)但我认为

  d2dCanvas := TDirect2DCanvas.Create(MyTcxImage.Canvas.Canvas, Rect(0,0, MyTcxImage.Width,MyTcxImage.Height));

应该做的伎俩

你需要将Canvas.Canvas传递给这个构造函数,因为cx组件(至少是我的版本)使用包含TCanvas的TcxCanvas。

您可以将TcxImage控件的窗口句柄传递给Direct2DCanvas构造函数。