<< Click to Display Table of Contents >> Navigation: Programming > Objects > TWPTextObjectClasses - customize TWPTextObj > Example: Set fixed width for edit field |
This example extends the one above. It uses an event handler to make edit fields use a minimum of space in the document.
We extend the event handler for the OnObjectClassCalcSize event:
procedure TForm1.WPTextObjectClasses1Items0ObjectClassCalcSize(
Sender: TWPCustomRTFControl;
Memo: TWPRTFEngineBasis;
ReferenceCanvas : TCanvas;
CallMode: TWPMeasureObjectMode;
ObjClass: TWPTextObjectClass;
TextObj: TWPTextObj;
XOff,
XRes, YRes: Integer;
var Distance, Width, ExtraWidth, Height,
ExtraHeight: Integer);
var emTextWidth, emTextLength, ReservedCharCount : Integer;
begin
ExtraWidth := Xres div 5;
Width := 0;
ReservedCharCount := 30;
TextObj.EmbeddedTextWidthAndLength(emTextWidth, emTextLength);
if emTextLength>=ReservedCharCount then
Distance := 0
else
if XOff>emTextWidth then
Distance := Distance + (ReferenceCanvas.TextWidth('Aa') div 2 ) * ReservedCharCount - emTextWidth
else Distance := Distance + (ReferenceCanvas.TextWidth('Aa') div 2 ) * ReservedCharCount - XOFF;
end;
ReservedCharCount is a variable we used to hold the virtual length of the field.
We can use the TWPTextObj function EmbeddedTextWidthAndLength to retrieve the length of the text inside the merge field.
The XOff parameter is the offset of the object during the reformat routine. If this is value is larger than the text width, the field has been started and finished on one line of the editor, otherwise it was started on a previous line.
The parameter ReferenceCanvas can be used to measure text. It has been initialized with the character properties of the object, which are not necessarily the properties of the field text (which also do not have to be uniform for all characters in the field!).