Printing a String Grid D4

What I am trying to do is to exactly replicate what I see in a String Grid on paper, including the spacing as used in the grid eg. the column widths/heights are all exact. Basically the grid will consist of a few tiny squares (Found in the Fixedsys font). I need to position the squares in the cells which I have done but the hard part is being able to get it to print how it looked on the form/grid. Another thing is, should I be using the Draw Grid instead of a String Grid as all I need on the grid are tiny squares to put in a few cells. Are there any Free String\Draw grids available that enables you to print the grid using the correct column spacing etc.
joshordanAsked:
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.

florisbCommented:
Suggestion: Use Q(uick)report (Delphi standard). There, what you see is what you print and the user gets a standard windows print dialog.

Once I also wanted to print a dbgrid, I did found a dbgrid component with good print-functionality on Torry's pages if I remember correctly.

Floris.
0
florisbCommented:
Suggestion: Use Q(uick)report (Delphi standard). There, what you see is what you print and the user gets a standard windows print dialog.

Once I also wanted to print a dbgrid, I did found a dbgrid component with good print-functionality on Torry's pages if I remember correctly.

Floris.
0
intheCommented:
hi,
heres a couple of ways.
You can print the visible client area of a grid directly:
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Printer.BeginDoc;
  try
    printer.canvas.moveto(100,100);
    SetMapMode( printer.canvas.handle, MM_ANISOTROPIC );
    SetWindowExtEx(printer.canvas.handle,
                  GetDeviceCaps(canvas.handle, LOGPIXELSX),
                  GetDeviceCaps(canvas.handle, LOGPIXELSY),
                  Nil);
    SetViewportExtEx(printer.canvas.handle,
                  GetDeviceCaps(printer.canvas.handle, LOGPIXELSX),                   GetDeviceCaps(printer.canvas.handle, LOGPIXELSY),                   Nil);
    stringgrid1.PaintTo( printer.canvas.handle, 100, 100 );
  finally
    printer.enddoc;
  end;
end;
 
The mapping mode instructions rescale the printer canvas to the same resolution as the screen.




and I got this way from a delphi tip site:
There are several times when I need to send out a quick printout of a
TStringGrid without having to worry
about writting a report for it. There are a couple different ways of
approaching this, we will be looking at
the Delphi function AssignPrn(). Using AssignPrn() we can write to the
printer as if we are writting to a file
using WriteLn(). In this tip we will have to change the printer font to
a fixed width font so our printout will
have straight even columns. We will be using the Delphi functions
Format(), AssignPrn(), WriteLn(),
Rewrite(), and CloseFile().

    For this tip we will need a TButton with a TButton.OnClick event and
a TForm.OnCreate event along
    with a TStringGrid component. You can easily create these events
through the Events tab of the Object
    Inspector.
    First we will need to make sure we add Printers to our Units uses
clause.
    In our TForm.OnCreate event we will be setting up our TStringGrid
headers which will be the first
    fixed row of the TStringGrid.
    In our TButton.OnClick event we will first set our
TPrinter.PrinterIndex to default printer by setting
    this value to -1.
    Next we will be saving our printers font and resetting it to
'Courier New', a fixed width font. This will
    make our columns print in a straight even line using the Format()
Delphi function.
    Next we will assign our printer textfile to our printer using
AssignPrn().
    After assigning our file we need to initialize it using Rewrite().
    Before we loop through the TStringGrid rows, we will print out our
header and a row of dashes to
    seperate the header in our printout from the rest of the rows.
    We will use the Delphi function WriteLn() to printout each row to
our assigned printer file. To format
    the rows properly we will use the Delphi function Format() and input
all our columns into a formatted
    string.
    Next we will loop through our TStringGrid and printout each row in
the sam fashion as we did with the
    header row.
    Finally we will need to close the file handle we created with
AssignPrn() and rest our printer font to
    its original value..
    Don't forget to type in some text into the TStringGrid before trying
to print :-)
    Format() returns a string formated according to arguments that you
send in. Format() can get quite
    complex. Format looks to add the arguments in the order arranged
into the next position in order
    according to the percent sign "%" in your format string. In other
words, you need as many arguments as
    you have percent signs in your format string parameter. The number
after the percent sign indicates the
    max number of characters that will display. When you add a number, a
period, and a number then this
    indicates that you want to force that number of characters as
percision. In other words if you want two
    characters and your argument only returns one then the return forces
in a leading character whether it be
    a space for a string or a zero "0" for a numeric value. The "s" at
the end of our width and percision
    indicates that our argument will be passing in a string value to be
formated into that position. Take a
    look at the Delphi help file to see all the other values you can
pass in through the argument parameter.
    AssignPrn() assigns a textfile variable to the printer. The only
parameter is the textfile variable.
    Rewrite() creates a new file and opens it. The only parameter is the
assigned textfile variable.
    WriteLn() writes a line of text to our textfile variable. The first
parameter is the string to write. The
    second parameter is the textfile variable to write to.
    CloseFile() gets rid of the association between our textfile and the
printer file. The only parameter is
    our assigned textfile variable.

        Example 1


        {...}

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

        {...}

        procedure TForm1.FormCreate(Sender: TObject);
        begin
          { set our stringgrid to allow editing }
          StringGrid1.Options := StringGrid1.Options +
                                [goEditing];

          { set stringgrid1 to a better height then it's
            normal default }
          StringGrid1.DefaultRowHeight := 20;

          { remove fixed cols }
          StringGrid1.FixedCols := 0;

          { set column count }
          StringGrid1.ColCount := 3;

          { set column headers }
          StringGrid1.Cells[0,0] := 'First Name';
          StringGrid1.Cells[1,0] := 'Last Name';
          StringGrid1.Cells[2,0] := 'Department';
        end;

        procedure TForm1.Button1Click(Sender: TObject);
        var
          MyTextFile    : TextFile;
          tmpStr,OldFont : String;
          x              : Integer;
        begin
          { set to default printer }
          Printer.PrinterIndex := -1;

          { save the printers old font }
          OldFont := Printer.Canvas.Font.Name;

          { set the printers font name to a fixed
            width font so the printout will have
            even looking columns }
          Printer.Canvas.Font.Name := 'Courier New';

          { assign our textfile to the printer }
          AssignPrn(MyTextFile);

          try
            { initialize our textfile }
            Rewrite(MyTextFile);

            { format and print out our header as setup
              in TForm.OnCreate }
            tmpStr := Format('%-20.20s %-20.20s %-20.20s',
                            [StringGrid1.Cells[0,0],
                              StringGrid1.Cells[1,0],
                              StringGrid1.Cells[2,0]]);
            Writeln(MyTextFile, tmpStr);

            { format and print out a seperator between
              the header and rows }
            tmpStr := Format('%-20.20s %-20.20s %-20.20s',
                            ['--------------------',
                              '--------------------',
                              '--------------------']);
            Writeln(MyTextFile, tmpStr);

            for x := 1 to StringGrid1.RowCount - 1 do
            begin
              { format each row and print them out }
              tmpStr := Format('%-20.20s %-20.20s %-20.20s',
                              [StringGrid1.Cells[0,x],
                                StringGrid1.Cells[1,x],
                                StringGrid1.Cells[2,x]]);
              Writeln(MyTextFile, tmpStr);
            end;
          finally
            { close our textfile }
            CloseFile(MyTextFile);

            { reset the printers font name }
            Printer.Canvas.Font.Name := OldFont;
          end;
        end;

hope that helps
btw:
you could also try TAdvStringGrid from:
http://www.tmssoftware.com/
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
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.