Print a grid and its objects.

I want print a portion of my screen.
I've got a stringGrid on a PageControl and I want only print the grid.
has someone got an idea? Thanks.
                                                  Gaëlle
gaelleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
Hi

this is a bit guess work but it may work.an easy way to print a form is to put a panel on the form first before anything else and align it to client.
then you can do the following to print the entire panel and any components etc that are on it,so maybe you can cheat a bit and put the stringgrid on a panel then try the code below:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,Printers, ExtCtrls;

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

var
  Form1: TForm1;

implementation

{$R *.DFM}
procedure PrintBitmap(Bitmap: TBitmap; printrect: TRect);
var
  Info: PBitmapInfo;
  InfoSize: DWORD;
  Image: Pointer;
  ImageSize: DWORD;
begin
  with Bitmap do
  begin
    GetDIBSizes(Handle, InfoSize, ImageSize);
    Info := AllocMem(InfoSize);
    try
      Image := AllocMem(ImageSize);
      try
        GetDIB(Handle, Palette, Info^, Image^);
        with Info^.bmiHeader, printrect do
          StretchDIBits(Printer.Canvas.Handle, Left, Top, Right-Left,             Bottom-Top, 0, 0, biWidth, biHeight, Image, Info^,
            DIB_RGB_COLORS, SRCCOPY);
      finally
        FreeMem(Image, ImageSize);
      end;
    finally
      FreeMem(Info, InfoSize);
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
  var
    Bitmap      :   TBitmap;
    FromLeft,
    FromTop,
    PrintedWidth,
    PrintedHeight : Integer;
begin
  Printer.BeginDoc;
  Try
    Bitmap := TBitmap.Create;
    Try
      Bitmap.Width  := Panel1.Width;
      Bitmap.Height := Panel1.Height;
      Bitmap.PixelFormat := pf24bit;
      Bitmap.Canvas.CopyRect(Rect(0,0, Bitmap.Width,Bitmap.Height),
                          Form1.Canvas,Rect(Panel1.Left, Panel1.Top,
                                  Panel1.Left + Panel1.Width-1,
                                  Panel1.Top  + Panel1.Height-1) );
     PrintedWidth  := MulDiv(Printer.PageWidth, 80,100);
     PrintedHeight := MulDiv(PrintedWidth, Bitmap.Height, Bitmap.Width);
     FromLeft      := MulDiv(Printer.PageWidth, 10,100);
     FromTop      := MulDiv(Printer.PageHeight,10,100);
     PrintBitmap(Bitmap,Rect(FromLeft, FromTop,FromLeft + PrintedWidth,FromTop  + PrintedHeight));
    Finally
      Bitmap.Free
    End;
  Finally
    Printer.EndDoc
  End;
 end;
end.


try it and see how it goes ,let me know the results ,cheers
Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
retepCommented:
Hi Gaelle :-)

I've found the code below on a delphi-tips-page... I haven't tested it, but I thought you might want to give it a try??! Otherwise ignore this comment...

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;

Regards
Retep
0
florisbCommented:
I tried what you want with a dbgrid. Finally I added a quickreport to my project and display my data there if the user wants to print. Printing from a qreport results in a nice lay-out and standard print functiolity.

Greetings,
Floris.
0
gaelleAuthor Commented:
Thanks a lot Barry
Your answer is very good.
                                         Regards
                                                      Gaëlle
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.