如何将 Direct2D 画布绘制到 TcxImage 画布上?

发布于 2024-11-04 03:33:55 字数 2658 浏览 1 评论 0原文

我有一个 Direct2D 演示,它创建 GridPatternBitmapBrush,然后在 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;

I have an Direct2D demo that creates a GridPatternBitmapBrush then draws the grid pattern on a TForm in a paint method. How do you get the Direct2D GridPatternBitmapBrush to appear on a TcxImage.Canvas( DeveloperExpress TImage) insead of the form?

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里泪两行 2024-11-11 03:33:56

您需要更改 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 构造函数。

You need to change the constructor of the d2dCanvas.

Currently it's

  d2dCanvas := TDirect2DCanvas.Create(Handle);

which is passing the forms handle to create the canvas.

I don't have Delphi 2010 (Which it seems is shipped with required units for the demo) but I think

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

should do the trick

You'll need to pass Canvas.Canvas to this constructor because the cx components (at least my version of) use a TcxCanvas which contains a TCanvas.

You may be able to pass the Window Handle of the TcxImage Control to the Direct2DCanvas constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文