Ir para conteúdo
Fórum Script Brasil
  • 0

(Resolvido) Imprimir Stringgrid Zebrado


JPMADEIRA

Pergunta

olá achei esse código na internet, e conseguir fazer ele imprimir mais ele imprimiu só a partir da segunda coluna e não consegui imprimir a primeira coluna, mudei o código de pé a rabo e não consegui fazer imprimir a primeira coluna, o código é esse aqui:

procedure PrintGrid(sGrid: TStringGrid; sTitle: string);
var
X1, X2: Integer;
Y1, Y2: Integer;
TmpI: Integer;
F: 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;

se alguém tiver alguma dica pra me ajudar, agradeço muito...

Link para o comentário
Compartilhar em outros sites

11 respostass a esta questão

Posts Recomendados

  • 0

como eu disse o indice da coluna começa no zero ....

fiz um teste aqui com as mudanças no código, e esta imprimindo corretamente

for F := 0 to sGrid.ColCount do

for TmpI := 0 to (F - 1) do

for TmpI := 0 to F do

for TmpI := 0 to sGrid.RowCount do

OBS: nas propriedades FixedCols e FixedRows = 0

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Printers;

procedure PrintGrid(sGrid: TStringGrid; sTitle: string);
var
  X1, X2: Integer;
  Y1, Y2: Integer;
  TmpI: Integer;
  F: 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 := 0 to sGrid.ColCount do
  begin
    X1 := 0;
    for TmpI := 0 to (F - 1) do
      X1 := X1 + 5 * (sGrid.ColWidths[TmpI]);
    Y1 := 300;
    X2 := 0;
    for TmpI := 0 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 := 0 to sGrid.RowCount 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 TForm1.Button1Click(Sender: TObject);
begin
  PrintGrid(StringGrid1, 'Print Stringgrid');
end;


end.

outra solução:

http://www.edudelphipage.com.br/down/componen/prtstrgr.zip

abraço

Link para o comentário
Compartilhar em outros sites

  • 0

<script type='text/javascript'>window.mod_pagespeed_start = Number(new Date());</script>

como eu disse o indice da coluna começa no zero ....

fiz um teste aqui com as mudanças no código, e esta imprimindo corretamente

for F := 0 to sGrid.ColCount do

for TmpI := 0 to (F - 1) do

for TmpI := 0 to F do

for TmpI := 0 to sGrid.RowCount do

OBS: nas propriedades FixedCols e FixedRows = 0

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Printers;

procedure PrintGrid(sGrid: TStringGrid; sTitle: string);
var
  X1, X2: Integer;
  Y1, Y2: Integer;
  TmpI: Integer;
  F: 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 := 0 to sGrid.ColCount do
  begin
    X1 := 0;
    for TmpI := 0 to (F - 1) do
      X1 := X1 + 5 * (sGrid.ColWidths[TmpI]);
    Y1 := 300;
    X2 := 0;
    for TmpI := 0 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 := 0 to sGrid.RowCount 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 TForm1.Button1Click(Sender: TObject);
begin
  PrintGrid(StringGrid1, 'Print Stringgrid');
end;


end.

outra solução:

http://www.edudelphipage.com.br/down/componen/prtstrgr.zip

abraço

Opa funcionou mesmo Jhonas vlw pela ajuda, tinha esquecido de mudar os "for TmpI := 0 to F do"... o resto deixa comigo.. vlw mesmo

Link para o comentário
Compartilhar em outros sites

  • 0

o código fica dessa maneira

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Printers;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin

  If (arow > 0)  then
     if (odd(arow)) then
     begin
        stringgrid1.Canvas.Font.Color:= clBlack;
        stringgrid1.Canvas.Brush.Color:= clBtnFace;
     end
     else
     begin
        stringgrid1.Canvas.Font.Color:= clBlack;
        stringgrid1.Canvas.Brush.Color:= clWhite;
     end;
  stringgrid1.Canvas.FillRect(Rect);
  stringgrid1.Canvas.TextOut(Rect.Left+2,Rect.Top,stringgrid1.Cells[acol,arow]);
end;

procedure PrintGrid(sGrid: TStringGrid; sTitle: string);
var
  X1, X2: Integer;
  Y1, Y2: Integer;
  TmpI: Integer;
  F: 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 := 0 to sGrid.ColCount do
  begin
    X1 := 0;
    for TmpI := 0 to (F - 1) do
      X1 := X1 + 5 * (sGrid.ColWidths[TmpI]);
    Y1 := 300;
    X2 := 0;
    for TmpI := 0 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 := 0 to sGrid.RowCount do
    begin
      Y1 := 150 * TmpI + 300;
      Y2 := 150 * (TmpI + 1) + 300;

      if Odd(TmpI) then
         Printer.Canvas.Brush.Color := clBtnFace
      else
         Printer.Canvas.Brush.Color := clWhite;

      TR := Rect(X1, Y1, X2 - 30, Y2);
      Printer.Canvas.TextRect(TR, X1 + 50, Y1 + 50, sGrid.Cells[F, TmpI]);

      Printer.Canvas.Brush.Color := clBlack;
      Printer.Canvas.FrameRect(TR);
      Printer.Canvas.Brush.Color := clWhite;

    end;
  end;
  Printer.EndDoc;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintGrid(StringGrid1, 'Print Stringgrid');
end;

end.

abraço

Link para o comentário
Compartilhar em outros sites

  • 0

o código fica dessa maneira

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Printers;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin

  If (arow > 0)  then
     if (odd(arow)) then
     begin
        stringgrid1.Canvas.Font.Color:= clBlack;
        stringgrid1.Canvas.Brush.Color:= clBtnFace;
     end
     else
     begin
        stringgrid1.Canvas.Font.Color:= clBlack;
        stringgrid1.Canvas.Brush.Color:= clWhite;
     end;
  stringgrid1.Canvas.FillRect(Rect);
  stringgrid1.Canvas.TextOut(Rect.Left+2,Rect.Top,stringgrid1.Cells[acol,arow]);
end;

procedure PrintGrid(sGrid: TStringGrid; sTitle: string);
var
  X1, X2: Integer;
  Y1, Y2: Integer;
  TmpI: Integer;
  F: 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 := 0 to sGrid.ColCount do
  begin
    X1 := 0;
    for TmpI := 0 to (F - 1) do
      X1 := X1 + 5 * (sGrid.ColWidths[TmpI]);
    Y1 := 300;
    X2 := 0;
    for TmpI := 0 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 := 0 to sGrid.RowCount do
    begin
      Y1 := 150 * TmpI + 300;
      Y2 := 150 * (TmpI + 1) + 300;

      if Odd(TmpI) then
         Printer.Canvas.Brush.Color := clBtnFace
      else
         Printer.Canvas.Brush.Color := clWhite;

      TR := Rect(X1, Y1, X2 - 30, Y2);
      Printer.Canvas.TextRect(TR, X1 + 50, Y1 + 50, sGrid.Cells[F, TmpI]);

      Printer.Canvas.Brush.Color := clBlack;
      Printer.Canvas.FrameRect(TR);
      Printer.Canvas.Brush.Color := clWhite;

    end;
  end;
  Printer.EndDoc;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintGrid(StringGrid1, 'Print Stringgrid');
end;

end.

abraço

Funcionou direitinho jhonas, só que ele aparece uma coluna a mais sendo que no striggrid não tem essa coluna, mais de resto funcionou direitinho, valeu mesmo, nunca tinha mexido com striggrid primeira vez e já peguei o jeito...

Link para o comentário
Compartilhar em outros sites

  • 0

só que ele aparece uma coluna a mais sendo que no striggrid não tem essa coluna,

isso é simples de resolver

Printer.Canvas.TextOut(0, 100, Printer.Title);

for F := 0 to sGrid.ColCount-1 do

begin

X1 := 0;

OBS: O mesmo vale para a linha a mais que aparece na impressão

Printer.Canvas.Font.Style := [];

for TmpI := 0 to sGrid.RowCount-1 do

begin

Y1 := 150 * TmpI + 300;

abraço

Link para o comentário
Compartilhar em outros sites

  • 0

só que ele aparece uma coluna a mais sendo que no striggrid não tem essa coluna,

isso é simples de resolver

Printer.Canvas.TextOut(0, 100, Printer.Title);

for F := 0 to sGrid.ColCount-1 do
begin
X1 := 0;

abraço

é eu tinha feito depois que formulei a pergunta e deu certo... muito obrigado pela a dica.. :)

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,3k
    • Posts
      652,3k
×
×
  • Criar Novo...