【问题标题】:How to use draw text on the canvas of listbox using DirectWrite in Delphi?如何在Delphi中使用DirectWrite在列表框的画布上绘制文本?
【发布时间】:2016-02-23 13:51:19
【问题描述】:

我正在寻找一个简单的示例来使用 TDirect2DCanvas 为所有者绘制列表框的每个项目。谷歌搜索 DirectWrite 将结果提供给示例示例以在表单上呈现文本。作为一名学生,我的 Delphi 技能无法正确掌握教程。一个简单的例子或在画布上绘制文本的参考对我来说是一个很好的开始。

这是代码(旧的经典方法),我正在尝试使用 DirectWrite 来实现:

procedure TForm2.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  LB: TListBox;
begin
  LB := TListBox(Control);

  if odSelected in State then begin
    LB.Canvas.Brush.Color := clPurple;

  end;

  LB.Canvas.FillRect(Rect);
  LB.Canvas.TextOut(Rect.Left + 10, Rect.Top + 5, LB.Items[Index]);
end;

【问题讨论】:

标签: delphi canvas vcl delphi-xe8 directwrite


【解决方案1】:

您发布的代码将转换为如下内容:

var
  Direct2DCanvas: TDirect2DCanvas;
  LB: TListBox;
begin
  LB := TListBox(Control);

  Direct2DCanvas := TDirect2DCanvas.Create(LB.Canvas, LB.ClientRect);
  Direct2DCanvas.BeginDraw;
   try
    if odSelected in State then begin
      Direct2DCanvas.Brush.Color := clPurple;
    end;

    Direct2DCanvas.FillRect(Rect);
    Direct2DCanvas.TextOut(Rect.Left + 10, Rect.Top + 5, LB.Items[Index]);
  finally
    Direct2DCanvas.EndDraw;
    Direct2DCanvas.Free;
  end;
end;

但是,请记住,TDirect2DCanvas 实例的构造和销毁会大大减慢速度。正如 David 指出的那样,最终它可能会比 GDI 慢。

这表示如果在较低级别完成并且如果发生大量绘图或者如果您依赖抗锯齿绘图(GDI 无法提供),它会更快。

要在较低级别实现绘图,您必须派生自定义 TListBox 组件并实现处理调整大小和绘图的附加 TDirect2DInstance(如果可用)。这在 David 的(已经提供的)link 中进行了解释。

【讨论】:

    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2019-10-23
    • 2017-05-21
    • 2021-10-27
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多