Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Delphi中使用DirectWrite在listbox的画布上绘制文本?_Delphi_Canvas_Vcl_Delphi Xe8_Directwrite - Fatal编程技术网

如何在Delphi中使用DirectWrite在listbox的画布上绘制文本?

如何在Delphi中使用DirectWrite在listbox的画布上绘制文本?,delphi,canvas,vcl,delphi-xe8,directwrite,Delphi,Canvas,Vcl,Delphi Xe8,Directwrite,我正在寻找一个简单的例子,使用TDirect2DCanvas为所有者绘制列表框的每个项目。Google for DirectWrite将结果提供给示例,以便在表单上呈现文本。作为一名学生,我的德尔菲技能无法正确地理解教程。一个简单的例子或者一个在画布上绘制文本的参考对我来说是一个很好的开始 下面是代码(旧的经典方法),我尝试使用DirectWrite实现: procedure TForm2.ListBox1DrawItem(Control: TWinControl; Index: Integer

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

下面是代码(旧的经典方法),我尝试使用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;

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

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(已提供)在中对此进行了解释。

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

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(已提供)在中对此进行了解释。

您似乎希望我们为您翻译代码。如果您无法理解教程和文档,那么您将无法生成任何代码。你考虑过雇一个程序员吗?你为什么要停止使用GDI呢?我正在努力克服这些问题:我希望D2D不会比GDIIt快,甚至比GDIIt慢。看起来你希望我们为你翻译代码。如果您无法理解教程和文档,那么您将无法生成任何代码。你考虑过雇一个程序员吗?你为什么要停止使用GDI呢?我正在努力克服这些问题:我希望D2D不会比GDI快,甚至比GDI慢