Print Windows Form

HMG Samples and Enhancements

Moderator: Rathinagiri

User avatar
mol
Posts: 3031
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Print Windows Form

Post by mol » Fri Jan 12, 2018 6:36 am

You can use this functon to calulate length of printed text:

Code: Select all

function PrintLen( cString,nFontsize,cFontname)
return round(gettextwidth(Nil,cString,cFontname)*0.072/72*25.4*nFontsize,2)
It's not ideal, sometimes does not work enough good.

Code: Select all

 nMaxLength := 20
 cLine := "Code"
 do while PrintLen(cLine + ".", 12, "ARIAL") <= nMaxLength
    cLine += "."
 enddo

User avatar
BeGeS
Posts: 120
Joined: Fri Jul 14, 2017 10:45 am
DBs Used: DBF
Location: La Mancha, Spain

Re: Print Windows Form

Post by BeGeS » Fri Jan 12, 2018 5:17 pm

andyglezl wrote:
Thu Jan 11, 2018 11:51 pm
Hola BeGes
Deberás usar un Font de ancho fijo. ( Times New, Consolas, etc )
Ejemplo: PADR( "Price", 15, "." ) + ":"

NOTA:
Trata de hacer tu pregunta en un nuevo Post para que no se confundan o pierdan las respuestas...
Gracias, Andy.

Tienes razón. Quizá mi post ha irrumpido en mitad de una conversación. La intención era realizar la pregunta en un hilo relacionado, y encontré este que lleva como título "Print Windows Form". Ruego disculpas.

En cuanto a usar una fuente de ancho fijo... precisamente eso es lo que no quiero, porque lo llevo haciendo más de 30 años y va siendo hora de cambiar. :mrgreen:

Un abrazo. ;)

- - - - - - - - - - - - -

Thanks, Andy.

You're right. Maybe my post has burst in the middle of a conversation. The intention was to ask the question in a related thread, and I found this one that bears the title "Print Windows Form". I apologize.

As for using a fixed-width font ... that's exactly what I do not want, because I've been doing it for more than 30 years and it's time to change. :mrgreen:

A hug. ;)
I get by with a little help from my friends

User avatar
BeGeS
Posts: 120
Joined: Fri Jul 14, 2017 10:45 am
DBs Used: DBF
Location: La Mancha, Spain

Re: Print Windows Form

Post by BeGeS » Fri Jan 12, 2018 5:25 pm

mol wrote:
Fri Jan 12, 2018 6:36 am
You can use this functon to calulate length of printed text:

Code: Select all

function PrintLen( cString,nFontsize,cFontname)
return round(gettextwidth(Nil,cString,cFontname)*0.072/72*25.4*nFontsize,2)
It's not ideal, sometimes does not work enough good.

Code: Select all

 nMaxLength := 20
 cLine := "Code"
 do while PrintLen(cLine + ".", 12, "ARIAL") <= nMaxLength
    cLine += "."
 enddo
Thank you very much, Mol. That is exactly what I was looking for.

I was unaware of the existence of GetTextWidth(), as it happens with so many other functions. :oops:

You say that it may not be exact, but the difference is negligible if ":" is placed at the end of the points.

Code: Select all

 nMaxLength := 20
 cLine := "Code"
 do while PrintLen(cLine + ".", 12, "ARIAL") <= nMaxLength
    cLine += "."
 enddo
 
 @Row,Col PRINT cLine
 @Row,Col+nMaxLength PRINT ": "
The way to write the constant has attracted my attention: 0.072/72* 25.4

That is 25.4/1000. That is 0.0254. But I suppose there will be reasons (that I do not know) for you to write it that way.

I use Arial 10, that's why I reduced the constant to 0.025 and it has been perfect! :D

Thanks again for your time. ;)
I get by with a little help from my friends

edk
Posts: 319
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Print Windows Form

Post by edk » Fri Jan 12, 2018 5:52 pm

You can also do it this way:

Code: Select all

#include "hmg.ch"

Function Main()
   DEFINE WINDOW Win_1 ;
      AT 0,0 ;
      WIDTH 400 ;
      HEIGHT 400 ;
      TITLE 'Test' ;
      MAIN 

      @ 50, 50 BUTTON Button_1 CAPTION "Test" ACTION PrintTest()

   END WINDOW
   CENTER WINDOW Win_1
   ACTIVATE WINDOW Win_1
Return

*------------------------------------------------------------------------------*
Procedure PrintTest
LOCAL lSuccess, IsPreview, xTab := 50, i, xRow := 20
LOCAL aList:={ {"Code","000001"} , {"Description","PEPPER MARMALADE"} , {"Price", 2.35} }
  
   SELECT PRINTER DIALOG TO lSuccess PREVIEW

   If lSuccess == .F.
      MsgInfo('Print Error')
      Return
   EndIf 

   START PRINTDOC
         START PRINTPAGE
			FOR i=1 TO LEN(aList)
				@ xRow,20 PRINT aList[i][1] + REPLICATE(".", 100) TO xRow + 5, xTab + .5 ;
				FONT "Arial" ;
				SIZE 12

				@ xRow,xTab PRINT ": " + ALLTRIM(hb_ValToStr( aList [i][2] )) ;
				FONT "Arial" ;
				SIZE 12 
					
				xRow += 5
			Next i
         END PRINTPAGE
      END PRINTDOC
Return

User avatar
BeGeS
Posts: 120
Joined: Fri Jul 14, 2017 10:45 am
DBs Used: DBF
Location: La Mancha, Spain

Re: Print Windows Form

Post by BeGeS » Fri Jan 12, 2018 10:20 pm

:shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock:

Oh my God!

edk, that's the first thing I tried. I thought that PRINT DATA TO would work just like PRINT LINE TO.
But no. That did not print anything. Why?

Well, because I wrote something like this:

cString:="Description"+REPLICATE (".", 50)
@Row,Col PRINT cString TO Row,Col+25

:? :roll:

The solution is... TO Row+5,Col+25 :o

Of course, if you do not tell me it would never have occurred to me.

Thank you very much, edk! :)
I get by with a little help from my friends

edk
Posts: 319
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Print Windows Form

Post by edk » Sat Jan 13, 2018 9:36 am

BeGeS wrote: I wrote something like this:

cString:="Description"+REPLICATE (".", 50)
@Row,Col PRINT cString TO Row,Col+25
You need to create a rectangular box with a height not less than the height of the printed text ;)

User avatar
BeGeS
Posts: 120
Joined: Fri Jul 14, 2017 10:45 am
DBs Used: DBF
Location: La Mancha, Spain

Re: Print Windows Form

Post by BeGeS » Sat Jan 13, 2018 10:52 am

edk wrote:
Sat Jan 13, 2018 9:36 am
You need to create a rectangular box with a height not less than the height of the printed text ;)
...a box that is not necessary with PRINT LINE TO. Thanks.

Out of curiosity, I have continued with this matter. The solutions of mol and edk are equally valid, although that of edk is more direct (fast).

However, if we seek to get closer to perfection, we must develop a little more the solution of mol.

Code: Select all

nMaxLength := 20
cLine := "Code"
DOTSS := " "

do while PrintLen(cLine + DOTSS, 12, "ARIAL") <= nMaxLength
  DOTSS += ”.”
enddo

 @Row,Col PRINT cLine
 @Row,Col+nMaxLength PRINT DOTSS RIGHT 
 @Row,Col+nMaxLength PRINT ":"    // Optional 

I get by with a little help from my friends

Post Reply