Question
“How can I vertically center text in a memo?”
Solution
Report output consists of a collection of Page.DrawCommands[]. Each time the Memo, generates, it creates a DrawCommand that describes the location and content to be rendered. We can use the Memo.OnDrawCommandCreate event to modify the DrawCommand. We can vetically center the wrapped text by inserting some empty lines.
Download: VeticallyCenterMemoText.zip
Sample Delphi code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
uses ppDrwCmd; procedure TForm1.ppDBMemo1DrawCommandCreate(Sender, aDrawCommand: TObject); var lDrawText: TppDrawText; liMaxLines: Integer; liAddLines: Integer; liIndex: Integer; begin lDrawText := TppDrawText(aDrawCommand); // max lines = DBMemo screen pixel height - font height liMaxLines := ppDBMemo1.spHeight div Abs(ppDBMemo1.Font.Height); // lines to add = (max lines - wrapped lines) div 2 liAddLines := (liMaxLines - lDrawText.WrappedText.Count) div 2; // insert lines for liIndex := 0 to liAddLines-1 do lDrawText.WrappedText.Insert(0,''); end; |