Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

StringGrid.Paintto size prob

Posted on 2005-04-21
6
Medium Priority
?
739 Views
Last Modified: 2010-04-04
Hello, I am trying to do something which I though would be very simple but I can't get it to work..

I have a stringgrid about 500w by 150h, 7 rows and 3 cols but I don't think this is relevant.  I am trying to send it to the printer using the following, Data being the TStringGrid...

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
    with Printer do
    begin
      BeginDoc;
        Printer.PageWidth := 400;
        Data.PaintTo(Handle, 10, 10);
      EndDoc;
    end;
end;

My problem is that this prints it very very small in the top left corner, the scale is all wrong, is this something to do with the DPI setting of the printer?  how do I change the output size? is this done with TPrinter or the TStringGrid?

Thanks for any help!
JustinWillis.
0
Comment
Question by:JustinWillis
  • 3
  • 2
6 Comments
 
LVL 1

Author Comment

by:JustinWillis
ID: 13832213
Points increased
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13832359
This will print data from the columns and rows, but do you need gridlines as well?
Are you looking for a screenshot print really?

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;
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 1000 total points
ID: 13832437
This one will do gridlines as well

Procedure TForm1.PrtGrid(Grid:TStringGrid; PrtTitle, PrtText:string);
 Type
  TCols=Array[0..20] of integer;
 Var
  Rapport:Integer;
  r,c:longint;
  cols:^TCols;
  y:integer;
  MargeLeft,Margetop,MargeRight:integer;
  StrDate:String;
  TextDown:integer;
  Procedure VerticalLines;
   Var
    c:LongInt;
   begin
     With Printer.Canvas do begin
      For c:=0 to Grid.ColCount-1 do begin
       MoveTo(Cols^[c],MargeTop+TextDown);
       LineTo(Cols^[c],y);
      end;
      MoveTo(MargeRight,MargeTop+TextDown);
      LineTo(MargeRight,y);
     end;
   end;
  Procedure PrintRow(r:longint);
   Var
    c:longint;
   begin
    With Printer.Canvas do begin
     For c:=0 to Grid.ColCount do TextOut(Cols^[c]+10,y+10,Grid.Cells[c,r]);
     inc(y,TextDown);
     MoveTo(MargeLeft,y); LineTo(MargeRight,y);
    end;
   end;
  Procedure Entete;
   begin
    With Printer.Canvas do begin
     Font.Style:=[fsBold];

     TextOut(MargeLeft,MargeTop,PrtText);
     TextOut(MargeRight-TextWidth(StrDate),MargeTop,StrDate);
     y:=MargeTop+TextDown;

     Brush.Color:=clSilver;
     FillRect(Rect(MargeLeft,y,MargeRight,y+TextDown));
     MoveTo(MargeLeft,y); LineTo(MargeRight,y);
     PrintRow(0);
     Brush.Color:=clWhite;

     Font.Style:=[];
    end;
   end;
 begin
  Screen.Cursor:=crHourGlass;
  GetMem(Cols,Grid.ColCount*SizeOf(Integer));
  StrDate:='Imprimé le '+DateToStr(Date);
  With Printer do begin
   Rapport:=PageWidth div Grid.Width;
   Title:=PrtTitle;
   BeginDoc;
   With Canvas do begin
    Font:=Grid.Font;
    TextDown:=TextHeight(StrDate)*3 div 2;
   end;
   MargeLeft:=PageWidth div 25;
   MargeTop :=PageHeight div 25;
   MargeRight:=PageWidth-MargeLeft;
   { calcul des Cols }
   Cols^[0]:=MargeLeft;
   For c:=1 to Grid.ColCount-1 do begin
     Cols^[c]:=Cols^[c-1]+Grid.ColWidths[c-1]*Rapport;
   end;
   Entete;
   For r:=1 to Grid.RowCount-1 do begin
    PrintRow(r);
    if y>=(PageHeight-MargeTop) then begin
     VerticalLines;
     NewPage;
     Entete;
    end;
   end;
   VerticalLines;
   EndDoc;
  end;
  FreeMem(Cols,Grid.ColCount*SizeOf(Integer));
  Screen.Cursor:=crDefault;
 end;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:JustinWillis
ID: 13833373
Thanks a lot Mike, thats great.  Didn't print in colour though, not really a requirement but may be nice to know how to do this in future, if I figure that out I will post amended code.  The Paintto method I was trying to use seems to print wysiwyg which is perfect but the scale is just all wrong, oh well I guess that would just be too easy huh?

Thanks again!
JustinWillis.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13833948
hmm,

i would use paintto to paint on a temporary bitmap-object,
and then do a stretchdraw or bitblt to the printercanvas

just as an alternative

meikl ;-)
0
 
LVL 1

Author Comment

by:JustinWillis
ID: 13834185
Oh of course, why didn't I think of that, seems so obvious now.

Many thanks guys, will come in handy.
JustinWillis.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

569 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