How to create custom draw marker

    • Offizieller Beitrag

    Instances of the TWPTextObj class with ObjType set to wpobjTextObj are used to display page numbers, time and similar fields. Unlike mail merge fields those objects are represented using just one character. So it is impossible to have a line wrap in the text.

    Normally the objects are painted using an internal routine, so for objects with the name 'PAGE' the current page number is inserted.

    The text objects are created using
    WPRichText1.InputTextFieldName('CHANGEME');

    To have a different text you can provide an event handler for the OnTextObjGetTextEx event:

    Code
    procedure TForm1.WPRichText1TextObjGetTextEx(RefCanvas: TCanvas;  TXTObject: TWPTextObj; var PrintString: WideString; var WidthInPix,  HeightInPix: Integer; var PaintObject: TWPTextObj; Xres, YRes: Integer);begin  if TXTObject.Name='CHANGEME' then  begin      PrintString := 'more...';    end;end;

    This will display the text object with the text "more..." using the attributes defined for that object.

    It is very easy to create a different background color:

    Code
    procedure TForm1.WPRichText1TextObjGetTextEx(RefCanvas: TCanvas;  TXTObject: TWPTextObj; var PrintString: WideString; var WidthInPix,  HeightInPix: Integer; var PaintObject: TWPTextObj; Xres, YRes: Integer);begin  if TXTObject.Name='CHANGEME' then  begin      PrintString := 'more...';        RefCanvas.Brush.Color := clYellow;  end;end;

    Now, if you want to make that object clickable you can add an event handler to the OnTextObjectClick event:

    Code
    procedure TForm1.WPRichText1TextObjectClick(Sender: TWPCustomRtfEdit;  pobj: TWPTextObj; obj: TWPObject; var ignore: Boolean);begin  if (pobj.ObjType=wpobjTextObject) and     (pobj.Name='CHANGEME') then  begin     // locate next data record or similar ...     ShowMessage('Object ' + pobj.Name + ' was clicked!');  end;end;

    But what if you want to display some kind of graphic instead of the text?
    Do not use the OnTextObjectPaint event for this - this event is used to draw embedded images only. Instead create an event handler for the objects own paint event:

    Code
    procedure TForm1.OnPaintMarker(Sender : TWPTextObj;    OutCanvas  : TCanvas;    XRes, YRes : Integer;    X, Y, W, H, BASE: Integer );

    Assign the address of this paint event to the property TXTObject.OnPaint in the event OnTextObjGetTextEx and don't forget to also specify a width. The width is only used if the variable PaintObject has been set since otherwise always the PrintString is evaluated.

    This example will create the yellow maker: