AUGE_OHR wrote: ↑Sat Dec 11, 2021 1:09 am
hi,
carlazza wrote: ↑Fri Dec 10, 2021 9:27 pm
Could you please explain it to me better, using my example?
try this
Code: Select all
LOCAL cNAME := "Carlo"
LOCAL cFont := 'calibri'
LOCAL nSize := 12
@030,050 PRINT 'My name is ' Font cFont Size nSize
// correction
// nCol := CalcWide(cName,cFont , nSize)
nCol := CalcWide('My name is ',cFont , nSize)
@030,nCol PRINT cNAME Font cFont SIZE nSize
FUNCTION CalcWidth(cName,cFont,nSize, lBold)
LOCAL hDC := GetDC(0)
// HMG_CreateFont ( [ hDC ] , cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeOut ) --> hFont
LOCAL hFont := HMG_CreateFont( hDC, cFont , nSize, IF(lBold,.T.,.F.), .F., .F., .F. )
nPixelX := GetTextWidth( hDC, cName, hFont )
RETURN nPixelX
Unfortunately, I can't quite agree with what you wrote.
First, there is a logical error in the placement of the second phrase in the printout. It should be
Code: Select all
@030,050 + nCol PRINT cNAME Font cFont SIZE nSize BOLD
Secondly, your CalcWidth function returns a value expressed in pixels, and we use millimeters to define a printout.
Third, you refer to the object hDC: = GetDC (0), which is the desktop / screen and its DPI. The DPI of the screen has nothing to do with the DPI of the printer.
So, we need some additional information:
DPI of the printer, converting pixels to millimeters.
I would do it this way

:
Code: Select all
cTextToPrint := 'My name is '
cName := "Carlo"
cPrintFontName := "Calibri"
nPrintFontSize := 12
nLenTextToPrint := GetTxtLen ( cTextToPrint, cPrintFontName, nPrintFontSize )
@030,050 PRINT cTextToPrint Font cPrintFontName Size nPrintFontSize
@030,050 + nLenTextToPrint PRINT cName Font cPrintFontName SIZE nPrintFontSize BOLD
***************************************************************************************************
FUNCTION GetTxtLen ( cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout )
RETURN HMG_GethDCTextWidthinMM (OpenPrinterGetPageDC(), cText, cPrintFont, nFontSize, lBold, lItalic, lUnderline, lStrikeout )
***************************************************************************************************
FUNCTION HMG_GethDCTextWidthInMM (hDC, cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout)
RETURN (HMG_GethDCTextWidthInPx (hDC, cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout) * 25.4 ) / HMG_GetPrinterResolution( hDC )[1]
***************************************************************************************************
FUNCTION HMG_GethDCTextWidthInPx (hDC, cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout)
Local hFont
Default cText:=""
Default cFontName := _HMG_SYSDATA [ 342 ]
Default nFontSize := _HMG_SYSDATA [ 343 ]
Default lItalic := .F.
Default lUnderline := .F.
Default lStrikeout := .F.
Default lBold := .F.
hFont := HMG_CreateFont (hDC, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeOut )
RETURN GetTextWidth( hDC, cText, hFont )
***********************************************************************
#pragma begindump
#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"
#include <windows.h>
#include <hbapi.h>
HB_FUNC ( HMG_GETPRINTERRESOLUTION )
{
hb_reta (2);
hb_storvni ( GetDeviceCaps( (HDC) hb_parnl (1), LOGPIXELSX ), -1, 1 );
hb_storvni ( GetDeviceCaps( (HDC) hb_parnl (1), LOGPIXELSY ), -1, 2 );
}
#pragma ENDDUMP
************************************************************************************************************
It seems to me that this problem has probably already been discussed on the forum, but I cannot recall the topic.