Jeg skal printe fra et stringgrid. Jeg har fundet en kode men den virker ikke helt efter hensigten. Når jeg printer ud med den, så skriver den først den 1. kolonne (hvis der fx. er 50 rows), og på næste side skriver den de sidste og så skriver den næste kolonne. HJÆLP!!!!
Her er koden jeg bruger:
procedure PrintGrid(sGrid: TStringGrid; sTitle: string); var X1,X2,Y1,Y2,TmpI : Integer; TR : TRect; begin Printer.Title := sTitle; Printer.BeginDoc; Printer.Canvas.Pen.Color := 0; Printer.Canvas.Font.Name := 'Times New Roman'; Printer.Canvas.Font.Size := 12; Printer.Canvas.Font.Style := [fsBold, fsUnderline]; Printer.Canvas.TextOut(0, 100, Printer.Title); for F := 1 to sGrid.ColCount - 1 do begin X1 := 0; for TmpI := 1 to (F - 1) do X1 := X1 + 5 * (sGrid.ColWidths[TmpI]); Y1 := 300; X2 := 0; for TmpI := 1 to F do X2 := X2 + 5 * (sGrid.ColWidths[TmpI]); Y2 := 450; TR := Rect(X1, Y1, X2 - 30, Y2); Printer.Canvas.Font.Style := [fsBold]; Printer.Canvas.Font.Size := 7; Printer.Canvas.TextRect(TR, X1 + 50, 350, sGrid.Cells[F, 0]); Printer.Canvas.Font.Style := []; for TmpI := 1 to sGrid.RowCount - 1 do begin Y1 := 150 * TmpI + 300; Y2 := 150 * (TmpI + 1) + 300; TR := Rect(X1, Y1, X2 - 30, Y2); Printer.Canvas.TextRect(TR, X1 + 50, Y1 + 50, sGrid.Cells[F, TmpI]); end; end; Printer.EndDoc; end;
procedure PrintStringGrid(Source: TStringGrid; MarginsMM: TRect); {todo: gestire gli header (righe Fixed) in modo che siano bold} {todo: gestire gli header (righe Fixed) in modo che siano ripetuti sulle pagine successive alla prima} var ColW: array of integer; // ampiezza colonne (unità stampante) NewPageCols: array of integer;// colonna da cui iniziare nuova pagina PrintableRect: TRect; // area da stampare VPos, // posizione verticale corrente e LineH: integer; // altezza linea VertPageCount: integer; // num. pagine necessarie per 1 riga
procedure InitLocalVars; const teststr = ' abcdef ghijklm opqrst uvwxyz 012 3456 789 '; var i: integer; lp: integer; scrwid, prtwid: integer; begin // Inizializza area stampabile, posizione verticale PrintableRect := Printer.Canvas.ClipRect; PrintableRect.Left := PrintableRect.Left + MulDiv(MarginsMM.Left * GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX), 10, 254); PrintableRect.Right := PrintableRect.Right - MulDiv(MarginsMM.Right * GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX), 10, 254); PrintableRect.Top := PrintableRect.Top + MulDiv(MarginsMM.Top * GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY), 10, 254); PrintableRect.Bottom := PrintableRect.Bottom - MulDiv(MarginsMM.Bottom * GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY), 10, 254); LineH := MulDiv(Printer.Canvas.TextHeight(teststr), 112, 100); VPos := PrintableRect.Top + LineH; // Calcola larghezza colonne scrWid := Source.Canvas.TextWidth(teststr); prtWid := Printer.Canvas.TextWidth(teststr); SetLength(ColW, Source.ColCount); for i := 0 to High(ColW) do ColW[i] := MulDiv(Source.ColWidths[i], prtWid, scrWid); // Calcola numero pagine necessarie alla stampa in orizzontale {todo: gestire le colonne Fixed in modo che le stampi anche sulle pagine successive} VertPageCount := 1; lp := PrintableRect.Left; SetLength(NewPageCols, VertPageCount); NewPageCols[High(NewPageCols)] := Source.ColCount; for i := 0 to High(ColW) do begin if lp + ColW[i] > PrintableRect.Right then begin inc(VertPageCount); lp := PrintableRect.Left; NewPageCols[High(NewPageCols)] := i; SetLength(NewPageCols, VertPageCount); NewPageCols[High(NewPageCols)] := Source.ColCount; end else lp := lp + ColW[i]; end; end;
procedure PrintRow(index: integer; VertPage: integer); {todo: allineamento a sx per testo, a dx per numeri: serve probabilmente una proprietà associata alle colonne di Source} var FirstCol: integer; c: integer; r: TRect; begin FirstCol := 0; if VertPage > 0 then FirstCol := NewPageCols[VertPage - 1]; r := Rect(PrintableRect.Left, VPos, PrintableRect.Left, VPos + LineH); for c := FirstCol to NewPageCols[VertPage]-1 do begin r.Right := r.Left + ColW[c]; DrawText(Printer.Canvas.Handle, PChar(Source.Cells[c, index]), -1, r, DT_VCENTER + DT_SINGLELINE + DT_RIGHT); r.Left := r.Right; end; VPos := VPos + LineH; if VPos > PrintableRect.Bottom then begin Printer.NewPage; VPos := PrintableRect.Top + LineH; end; end;
procedure FinitLocalVars; begin Finalize(ColW); Finalize(NewPageCols); end;
var vp, r: integer; begin Printer.BeginDoc; try InitLocalVars; try for vp := 0 to VertPageCount-1 do begin if vp > 0 then begin Printer.NewPage; VPos := PrintableRect.Top + LineH; end; { PrintColHeaders here } for r := 0 to Pred(Source.RowCount) do PrintRow(r, vp); end; finally FinitLocalVars; end; finally Printer.EndDoc; end; end;
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.