[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Print a grid and its objects.

Posted on 2000-01-03
4
Medium Priority
?
161 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:gaelle
4 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 400 total points
ID: 2320474
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
 

Expert Comment

by:retep
ID: 2320702
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
 
LVL 2

Expert Comment

by:florisb
ID: 2325468
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
 

Author Comment

by:gaelle
ID: 2325559
Thanks a lot Barry
Your answer is very good.
                                         Regards
                                                      Gaëlle
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Suggested Courses
Course of the Month7 days, 15 hours left to enroll

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question