Chances are that the MT terminal internally uses GDI+ for font rendering. You can successfully use that to obtain string dimentions, it requires a couple of lines but it's pretty straightforward once you know how it works. GDI+ is a complement to the Win32/Win64 API and it's the native method for font rendering under Windows. Its execution time is the fastest as it communicates directly with device drivers for displaying or obtaining info. With GDI+ you'll be doing the same thing as the terminal but with no need to display the label nor extra overhead.
- msdn.microsoft.com
Hi again, i found this old thread regarding your same question: https://www.mql5.com/en/forum/4627
See if that formula works for you, but i believe that is font dependant. If you know that the font can be changed somehow then better stick to GDI+ to be always sure it's going to work.

- www.mql5.com
May be the GDI+ will be the solution, the other is not good for Calibri font.
Thanx for help!
How can I import GetTextExtentPoint32 and CreateFont functions?
The following code is not working:
#import "gdi32.dll" int GetTextExtentPoint32(int hdc,string str,int len,int &size); int CreateFont(int nHeight,int nWidth,int nEscapement,int nOrientation,int fnWeight,int fdwItalic,uint fdwUnderline,uint fdwStrikeOut, uint fdwCharSet,uint fdwOutputPrecision,uint fdwClipPrecision,uint fdwQuality,uint fdwPitchAndFamily,string lpszFace); #import
- msdn.microsoft.com
Don't know if you get any messages or it doesn't work at all with a silent result.
I would try first using unsigned types for windows handles "hdc -> uint" (win32) or "hdc -> ulong" (win64). A void pointer type (void*) would be perfect but it's not possible in MQL5. You just need to find the smallest type that can span to fit a memory address, there's no defined type in the MQL5 spec for this kind of work (such as "size_t" in C++) so stick to uint then move to ulong if it doesn't work.
As for the SIZE structure used in GetTextExtentPoint32, try using your own instead of an int:
struct tagSIZE
{
int cx;
int cy;
} SIZE;
int GetTextExtentPoint32(uint hdc, string str, int len, SIZE &size);
Second, according to the MQL5 reference, strings are in UNICODE format (the same format used by Windows' Unicode functions, UTF-16LE). So, i would suggest to explicitly import the Unicode "W" version of GetTextExtentPoint32:
int GetTextExtentPoint32W(uint hdc, string str, int len, SIZE &size);
And finally in CreateFont, replace the return type with an unsigned one and fdwItalic should be of type DWORD (uint). Remember to also import the unicode version of CreateFont:
uint CreateFontW(int nHeight,int nWidth,int nEscapement,int nOrientation,int fnWeight,uint fdwItalic,uint fdwUnderline,uint fdwStrikeOut,
uint fdwCharSet,uint fdwOutputPrecision,uint fdwClipPrecision,uint fdwQuality,uint fdwPitchAndFamily,string lpszFace);
That's it. If the functions are used correctly then all should be allright.
[UPDATE]: According to an article on the internet, the Windows SDK defines device context handles (HDC) and other handles (HFONT) as PVOID type (wich is finally int) so it should make no interference the signed and unsigned thing unless Windows need a signed range for certain functions. Try playing with both (int / uint).

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
How can I get a string width and height in pixel with a specific font and size without creating and label object?
DLL usage is not problem for me, if it's needed.
Thanx for help!