Page 1 of 1

Looking for a function

Posted: Wed Jun 03, 2020 2:00 pm
by sherlogg
Hello!

Hope, this is the right area for this posting.

I work with VxH and looking for a function where i can get the length/width of a string in pixel. For example a function like LenInPix(cString) or StringWidth(cString).

VxH is very similar to Harbor (or harbor is very similar to VxH) and maybe someone of you can help. Like i know, i can work with Windows API, but i'm not deep in Windows API.

Example:
cString := "Example"

Len(cString) is 7 and LenInPix(cString) ist nn

THX
Dieter

Re: Looking for a function

Posted: Wed Jun 03, 2020 2:30 pm
by gfilatov
sherlogg wrote: Wed Jun 03, 2020 2:00 pm Hello!

Hope, this is the right area for this posting.

I work with VxH and looking for a function where i can get the length/width of a string in pixel. For example a function like LenInPix(cString) or StringWidth(cString).

VxH is very similar to Harbor (or harbor is very similar to VxH) and maybe someone of you can help. Like i know, i can work with Windows API, but i'm not deep in Windows API.

Example:
cString := "Example"

Len(cString) is 7 and LenInPix(cString) ist nn

THX
Dieter
Hello Dieter,

Try the following function
GetTextWidth( NIL, cString, FontHandle )
where FontHandle is a font handle of your Label control.

The above function returns the Width of a string in the pixels.

Hope that helps :idea:

Re: Looking for a function

Posted: Wed Jun 03, 2020 3:49 pm
by sherlogg
Hello Grigory,

thx for your fast Reply. I will give it a try.

Cheers,
Dieter

Re: Looking for a function

Posted: Thu Jun 04, 2020 10:31 am
by sherlogg
I've found a function GetTextWidth(cString), in combination with using the printer object. I've tested, but it don't work fine. I've set Font, Pointsize, and so on, but i get strange values, when i use GetTextWidth(cString).

For example: the output for GetTextWidth("Dieter") is 360

Maybe i forget the use of a special setting, but i will go on with test, but maybe someone have another idea?!?

THX, Dieter

Re: Looking for a function

Posted: Thu Jun 04, 2020 11:06 am
by gfilatov
sherlogg wrote: Thu Jun 04, 2020 10:31 am I've found a function GetTextWidth(cString), in combination with using the printer object. I've tested, but it don't work fine. I've set Font, Pointsize, and so on, but i get strange values, when i use GetTextWidth(cString).

For example: the output for GetTextWidth("Dieter") is 360

Maybe i forget the use of a special setting, but i will go on with test, but maybe someone have another idea?!?
Hello Dieter,

Please be so kind to try the following simple sample:

Code: Select all

/*
 * HMG - Harbour Win32 GUI library Demo
 *
 * Copyright 2002 Roberto Lopez <mail.box.hmg@gmail.com>
 * http://www.hmgforum.com//
*/

#include "hmg.ch"

Function Main

	DEFINE WINDOW Form_Main ;
		AT 0,0 ;
		WIDTH 640 HEIGHT 480 ;
		TITLE 'Main Window' ;
		MAIN 

		@ 200,250 LABEL Label_1 ;
		WIDTH 150 HEIGHT 40 ;
		VALUE 'Click Me !' ;
		ACTION MsgInfo('Label Clicked!!!') ;
		FONT 'Arial' SIZE 24  CENTERALIGN

		@ 10,10 LABEL Label_2 ;
		AUTOSIZE ;
		VALUE '...' ;
		ACTION msgdebug(GetLabelLen())

	END WINDOW

	Form_Main.Label_2.Value := 'Hello All, This Is An AutoSIzable Label!!!'

	CENTER WINDOW Form_Main

	ACTIVATE WINDOW Form_Main

Return 

Function GetLabelLen()

Return GetTextWidth (NIL, Form_Main.Label_2.Value, _HMG_SYSDATA [ 36 ] [ Form_Main.Label_2.Index ] )
:idea:

Re: Looking for a function

Posted: Thu Jun 04, 2020 12:36 pm
by sherlogg
Hello Grigory,

your example works fine with Harbor, but like i'm told in my starting Post, i works with Visual xHarbor. Sorry for the misunderstanding.

I've found nothing comparable in VxH, nevertheless VxH is very near at Harbor. Maybe somebody else have an idea and i will try to go deeper into WINAPI.

Thx, for your help.

Cheer, Dieter

Re: Looking for a function

Posted: Thu Jun 04, 2020 5:43 pm
by AUGE_OHR
hi,

have you look at GetTxtWidth() :?:

Code: Select all

For example: the output for GetTextWidth("Dieter") is 360
you need to pass Font "in" Function

Code: Select all

FUNCTION GetTxtWidth( cText, nFontSize, cFontName )  // get Width of the text
   LOCAL hFont, nWidth
   DEFAULT cText     := REPL('A', 2)        ,  ;
           cFontName := _HMG_DefaultFontName,  ;   // from MiniGUI.Init()
           nFontSize := _HMG_DefaultFontSize       // from MiniGUI.Init()

   IF Valtype(cText) == 'N'
      cText := repl('A', cText)
   ENDIF

   hFont  := InitFont(cFontName, nFontSize)
   nWidth := GetTextWidth(0, cText, hFont)        // text width
   DeleteObject (hFont)                    

RETURN nWidth

Re: Looking for a function

Posted: Fri Jun 05, 2020 5:51 am
by sherlogg
Hallo Jimmy,

danke für Deine Ausführungen, aber wie ich schon geschrieben habe, arbeite ich mit VxH und da gibt es die Funktion GetTextWith nicht als allein stehende Funktion (versteh ich zwar nicht, weil VxH und Harbor ja sehr nahe beieinander sind). GetTextWith wird als Methode beim Printer-Objekt mitgeliefert und so wie es aussieht liefert es da auch falsche Werte. Aktuell zerpflücke ich gerade Win API Funktionen und da speziell "GetTextExtentPoint32A". Laut Beschreibung soll diese das gewünschte Ergebnis liefern.

Grüße, Dieter

Re: Looking for a function

Posted: Sun Jun 07, 2020 8:57 am
by AUGE_OHR
hi,
sherlogg wrote: Fri Jun 05, 2020 5:51 am danke für Deine Ausführungen, aber wie ich schon geschrieben habe, arbeite ich mit VxH und da gibt es die Funktion GetTextWith nicht als allein stehende Funktion (versteh ich zwar nicht, weil VxH und Harbor ja sehr nahe beieinander sind). GetTextWith wird als Methode beim Printer-Objekt mitgeliefert und so wie es aussieht liefert es da auch falsche Werte. Aktuell zerpflücke ich gerade Win API Funktionen und da speziell "GetTextExtentPoint32A". Laut Beschreibung soll diese das gewünschte Ergebnis liefern.
für VxH musst du vermutlich das "Pre-fix" bei den Parametern ändern

HMG

Code: Select all

HB_FUNC( GETTEXTWIDTH )  // returns the Width of a string in pixels
{
   HDC   hDC         = (HDC)    HMG_parnl (1);
   TCHAR *Text       = (TCHAR*) HMG_parc  (2);
   HFONT hFont       = (HFONT)  HMG_parnl (3);
   HWND  hWnd = NULL ;
   HFONT hOldFont = NULL;
   BOOL  bDestroyDC = FALSE;
   SIZE sz;

   if ( hDC == NULL )
   {
      bDestroyDC = TRUE;
      hWnd = GetActiveWindow();
      hDC = GetDC( hWnd );
   }

   if ( hFont )
      hOldFont = ( HFONT ) SelectObject( hDC, hFont );

   GetTextExtentPoint32( hDC, Text, lstrlen (Text), &sz );

   if ( hFont )
      SelectObject( hDC, hOldFont );

   if ( bDestroyDC )
       ReleaseDC( hWnd, hDC );

   hb_retni ((INT) sz.cx );
}
Extended Version

Code: Select all

HB_FUNC( GETTEXTWIDTH ) // returns the width of a string in pixels
{
   HDC   hDC        = ( HDC ) HB_PARNL( 1 );
   HWND  hWnd       = ( HWND ) NULL;
   BOOL  bDestroyDC = FALSE;
   HFONT hFont      = ( HFONT ) HB_PARNL( 3 );
   HFONT hOldFont   = ( HFONT ) NULL;
   SIZE  sz;

   if( ! hDC )
   {
      bDestroyDC = TRUE;
      hWnd       = GetActiveWindow();
      hDC        = GetDC( hWnd );
   }

   if( hFont )
      hOldFont = ( HFONT ) SelectObject( hDC, hFont );

   GetTextExtentPoint32( hDC, hb_parc( 2 ), ( int ) hb_parclen( 2 ), &sz );

   if( hFont )
      SelectObject( hDC, hOldFont );

   if( bDestroyDC )
      ReleaseDC( hWnd, hDC );

   hb_retnl( sz.cx );
}
FiveWin

Code: Select all

HB_FUNC( GETTEXTWIDTH ) //      hDC, szText [,hFont], [@nHeight]    --> nPixelsWidth
{
   HDC   hDC        = ( HDC ) fw_parH( 1 );
   HWND  hWnd;
   DWORD dwSize;
   BOOL  bDestroyDC = FALSE;
   HFONT hFont = ( HFONT ) fw_parH( 3 );
   HFONT hOldFont;
   SIZE  sz;

   if( ! hDC )
   {
      bDestroyDC = TRUE;
      hWnd = GetActiveWindow();
      hDC = GetDC( hWnd );
   }

   if( hFont )
      hOldFont = ( HFONT ) SelectObject( hDC, hFont );

   if ( isutf8( ( LPCSTR ) hb_parc( 2 ), hb_parclen( 2 ) ) )
   {
      LPWSTR pW   = UTF8toUTF16( hb_parc( 2 ) );
      WORD   wLen = MultiByteToWideChar( CP_UTF8, 0, hb_parc( 2 ), -1, 0, 0 );
      GetTextExtentPoint32W( hDC, pW, wLen, &sz );
      hb_xfree( ( void * ) pW );
   }
   else
      GetTextExtentPoint32A( hDC, ( char * ) hb_parc( 2 ), hb_parclen( 2 ), &sz );

   dwSize = sz.cx;

   if ( hb_pcount() > 3 && HB_ISBYREF( 4 ) ) hb_storni( sz.cy, 4 );

   if( hFont )
      SelectObject( hDC, hOldFont );

   if( bDestroyDC )
       ReleaseDC( hWnd, hDC );

   hb_retnl( dwSize );
}

Re: Looking for a function

Posted: Sun Jun 07, 2020 9:57 am
by sherlogg
Hallo Jimmy,

und wieder vielen Dank.

Schau mir das die Tage mal an.

LG, Dieter