Looking for a function

HMG en Español

Moderator: Rathinagiri

Post Reply
User avatar
sherlogg
Posts: 11
Joined: Wed Feb 15, 2017 6:14 pm
Contact:

Looking for a function

Post 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
User avatar
gfilatov
Posts: 1068
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Looking for a function

Post 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:
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
sherlogg
Posts: 11
Joined: Wed Feb 15, 2017 6:14 pm
Contact:

Re: Looking for a function

Post by sherlogg »

Hello Grigory,

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

Cheers,
Dieter
User avatar
sherlogg
Posts: 11
Joined: Wed Feb 15, 2017 6:14 pm
Contact:

Re: Looking for a function

Post 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
User avatar
gfilatov
Posts: 1068
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Looking for a function

Post 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:
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
sherlogg
Posts: 11
Joined: Wed Feb 15, 2017 6:14 pm
Contact:

Re: Looking for a function

Post 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
User avatar
AUGE_OHR
Posts: 2063
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Looking for a function

Post 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
have fun
Jimmy
User avatar
sherlogg
Posts: 11
Joined: Wed Feb 15, 2017 6:14 pm
Contact:

Re: Looking for a function

Post 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
User avatar
AUGE_OHR
Posts: 2063
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Looking for a function

Post 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 );
}
have fun
Jimmy
User avatar
sherlogg
Posts: 11
Joined: Wed Feb 15, 2017 6:14 pm
Contact:

Re: Looking for a function

Post by sherlogg »

Hallo Jimmy,

und wieder vielen Dank.

Schau mir das die Tage mal an.

LG, Dieter
Post Reply