Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Get length in MM of text to be printed?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Jul 22, 2017 12:47 AM Last Post By: Peter Below
Chris LeFebvre

Posts: 65
Registered: 10/24/99
Get length in MM of text to be printed?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 21, 2017 2:40 PM
I use a lightweight reporting tool called TPages by Angus Johnson, it works really well for my needs but one problem that I've been having is that I have to make sure that the title for a group of columns will fit in the space I'm allocating. TPages sets column width in millimeters so I have to get the width of the text I'm writing in the font and point size and turn that into millimeters.

I found info on various sites and got these:
Function TextWidthMM( sText: String ): Integer;
 
var
  DPI,
  Pixels,
  mm : Integer;
  PPerMMX, PPerMMY, MMLength: Single;
 
Begin
  DPI := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
  Printer.Canvas.Font.Name := 'Arial';
  Printer.Canvas.Font.Size := 10;
 
  PixelsPerMM( Printer.Canvas, PPerMMX, PPerMMY );
 
  Pixels := Printer.Canvas.TextWidth(sText);
 
  MMLength := Pixels / PPerMMX;
 
  MM := Round(MMLength);
 
  Result := MM;
End;
 
procedure PixelsPerMM( Canvas: TCanvas; Var X, Y: Single);
 
var
    H:HDC;
    hres,vres,
    hsiz,vsiz:integer;
 
begin
    H:=canvas.handle;
    hres := GetDeviceCaps(H,HORZRES) ;   {display width in pixels}
    vres := GetDeviceCaps(H,VERTRES) ;   {display height in pixels}
    hsiz := GetDeviceCaps(H,HORZSIZE) ;  {display width in mm}
    vsiz := GetDeviceCaps(H,VERTSIZE) ;  {display height in mm}
    x := hres/hsiz;
    y := vres/vsiz;
end;


The pages seem to indicate that this should work however in the PixelsPerMM procedure when I call "H := canvas.handle;" I get an exception "Printer is not currently printing". How do I correct this and get it to work?
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Get length in MM of text to be printed?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 22, 2017 12:47 AM   in response to: Chris LeFebvre in response to: Chris LeFebvre
Chris LeFebvre wrote:

I use a lightweight reporting tool called TPages by Angus Johnson, it
works really well for my needs but one problem that I've been having
is that I have to make sure that the title for a group of columns
will fit in the space I'm allocating. TPages sets column width in
millimeters so I have to get the width of the text I'm writing in the
font and point size and turn that into millimeters.

I found info on various sites and got these:
Function TextWidthMM( sText: String ): Integer;
 
var
  DPI,
  Pixels,
  mm : Integer;
  PPerMMX, PPerMMY, MMLength: Single;
 
Begin
  DPI := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
  Printer.Canvas.Font.Name := 'Arial';
  Printer.Canvas.Font.Size := 10;
 
  PixelsPerMM( Printer.Canvas, PPerMMX, PPerMMY );
 
  Pixels := Printer.Canvas.TextWidth(sText);
 
  MMLength := Pixels / PPerMMX;
 
  MM := Round(MMLength);
 
  Result := MM;
End;
 
procedure PixelsPerMM( Canvas: TCanvas; Var X, Y: Single);
 
var
    H:HDC;
    hres,vres,
    hsiz,vsiz:integer;
 
begin
    H:=canvas.handle;
    hres := GetDeviceCaps(H,HORZRES) ;   {display width in pixels}
    vres := GetDeviceCaps(H,VERTRES) ;   {display height in pixels}
    hsiz := GetDeviceCaps(H,HORZSIZE) ;  {display width in mm}
    vsiz := GetDeviceCaps(H,VERTSIZE) ;  {display height in mm}
    x := hres/hsiz;
    y := vres/vsiz;
end;


The pages seem to indicate that this should work however in the
PixelsPerMM procedure when I call "H := canvas.handle;" I get an
exception "Printer is not currently printing". How do I correct this
and get it to work?

Use Printer.Handle, not printer.canvas.handle. The former is an
information context handle (hIC) while the printer has not been set up
for printing, and that can be used in place of a device context handle
for GetDeviceCaps.


--
Peter Below
TeamB

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02