1

I am looking for a simple example to use TDirect2DCanvas for owner drawing each item of a listbox. Googling for DirectWrite gives the result to sample example to render text on the form. Being a student, my Delphi skill could not catch the tutorial properly. A simple example or a reference to draw text on a canvas would be a great start for me.

Here is the code(old classic method), I am trying to implement using 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;
Rabi Jayasawal
  • 441
  • 1
  • 9
  • 18
  • It seems like you want us to translate your code for you. If you can't manage to make any sense of the tutorials and docs, then you won't be able to produce any code. Have you considered hiring a programmer? Why do you want to stop using GDI anyway? – David Heffernan Nov 20 '15 at 23:05
  • I am trying to overcome these problems: http://stackoverflow.com/questions/33789279/non-english-text-size-too-small-in-windows-7 and http://stackoverflow.com/questions/33149592/drawing-unicode-text-on-listbox-canvas-is-too-slow – Rabi Jayasawal Nov 20 '15 at 23:16
  • http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_the_Direct2D_Canvas – David Heffernan Nov 20 '15 at 23:17
  • 1
    Expect D2D to be no faster, if not slower than GDI – David Heffernan Nov 20 '15 at 23:18

1 Answers1

0

The code you posted would translate to something like the following:

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;

However, keep in mind that the construction and destruction of a TDirect2DCanvas instance will slow things down a lot. In the end it might probably be slower than GDI as David pointed out.

This said it can be faster if done at a lower level and if a lot of drawing takes place or if you rely on anti-aliased drawing (which GDI can not offer).

To implement drawing at a lower level you must derive a custom TListBox component and implement handling resizing and drawing for an additional TDirect2DInstance (if this is available). This is explained in the (already provided) link by David.

CWBudde
  • 1,783
  • 1
  • 24
  • 28