I've been able to make it work in my Project but still the code has three weaknesses;
1. Print Orientation - It only prints in Portrait form. I need an option to print in Landscape.
v6 creates a print preview as a bitmap which can be displayed easily in a TImage embedded in a TScrollbox and enhanced by a toolbar with print and page navigation buttons as well as zoom factor combobox and zooming by mouse-wheel.
I moved development to my github repository now: https://github.com/wp-xyz/LazSamples/tree/master/grids/gridprinter.
I think I have found a problem: if Grid has "FixedCols = 0", both preview and print are wrong!"FixedCol = 0" issue fixed in last commit.
Maybe there is also a problem when the columns have the width equal to 0 (I use this trick to hide some "service" columns), but this I am still checking
You're right - forgotten to publish. Done now.
You apply the TextStyle to the Canvas of the ChapterStringGrid only. Move the evaluation of the Sender to the top of the event handler and apply the TextStyle to lCanvas.
Additionally a simplification for cell text extraction: Since grid and printer access the same cells it is enough to read the the cell text from the grid directly; there is no need to distinguish the Sender here - sorry, one of my previous sample codes was a bit too complicated here.
procedure TFormAKJV.ChapterStringGridPrepareCanvas(Sender: TObject; aCol, aRow: integer; aState: TGridDrawState); var lCanvas: TCanvas; s: String; ts: TTextStyle; begin if Sender is TStringGrid then lCanvas := TStringGrid(Sender).Canvas else if Sender = ChapterGridPrinter then lCanvas := ChapterGridPrinter.Canvas else exit; ts := lCanvas.TextStyle; ts.SingleLine := False; ts.Wordbreak := True; lCanvas.TextStyle := ts; s := ChapterStringGrid.Cells[4, ARow]; if s = 'AntiqueWhite' then begin lCanvas.Brush.Color := TColor($FAEBD7); end else if s = 'Aqua' then begin lCanvas.Brush.Color := TColor($00FFFF); end else if s = 'Aquamarine' then begin lCanvas.Brush.Color := TColor($7FFFD4); end else if s = 'Azure' then begin lCanvas.Brush.Color := TColor($F0FFFF); end else if s = 'Beige' then begin lCanvas.Brush.Color := TColor($F5F5DC); end; end;
Wordwrap requires the following conditions:
- Canvas.TextStyle.WordWrap must be true, of course.
- Canvas.TextStyle.SingleLine must be false
- Canvas.TextStyle.EndEllipsis must be false. BUT: The grid Option goCellEllipsis when active sets this to true. This is happening in your grid. Therefore wordwrap is not working. --> Remove it from the Options, and the wordwrap will work.
The other issue is that the row colors in your grid do not appear in the preview. This is because you do not assign the StringGrid1PrepareCanvas event handler to GridPrinter1.OnPrepareCanvas: Select the GridPrinter1 component in the form, find the OnPrepareCanvas event and select StringGrid1PrepareCanvas from the dropdown list.