Looking for a function
Moderator: Rathinagiri
Looking for a function
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
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
Hello Dieter,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
Try the following function
where FontHandle is a font handle of your Label control.GetTextWidth( NIL, cString, FontHandle )
The above function returns the Width of a string in the pixels.
Hope that helps
Kind Regards,
Grigory Filatov
"Everything should be made as simple as possible, but no simpler." Albert Einstein
Grigory Filatov
"Everything should be made as simple as possible, but no simpler." Albert Einstein
Re: Looking for a function
Hello Grigory,
thx for your fast Reply. I will give it a try.
Cheers,
Dieter
thx for your fast Reply. I will give it a try.
Cheers,
Dieter
Re: Looking for a function
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
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
Hello Dieter,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?!?
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 ] )
Kind Regards,
Grigory Filatov
"Everything should be made as simple as possible, but no simpler." Albert Einstein
Grigory Filatov
"Everything should be made as simple as possible, but no simpler." Albert Einstein
Re: Looking for a function
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
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
- AUGE_OHR
- Posts: 2064
- Joined: Sun Aug 25, 2019 3:12 pm
- DBs Used: DBF, PostgreSQL, MySQL, SQLite
- Location: Hamburg, Germany
Re: Looking for a function
hi,
have you look at GetTxtWidth()
have you look at GetTxtWidth()
you need to pass Font "in" FunctionCode: Select all
For example: the output for GetTextWidth("Dieter") is 360
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
Jimmy
Re: Looking for a function
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
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
- AUGE_OHR
- Posts: 2064
- Joined: Sun Aug 25, 2019 3:12 pm
- DBs Used: DBF, PostgreSQL, MySQL, SQLite
- Location: Hamburg, Germany
Re: Looking for a function
hi,
HMG
Extended Version
FiveWin
für VxH musst du vermutlich das "Pre-fix" bei den Parametern ändernsherlogg 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.
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 );
}
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 );
}
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
Jimmy
Re: Looking for a function
Hallo Jimmy,
und wieder vielen Dank.
Schau mir das die Tage mal an.
LG, Dieter
und wieder vielen Dank.
Schau mir das die Tage mal an.
LG, Dieter