TextGetSize

Rend la largeur et la hauteur de la ligneaux réglages courants de la fonte.

bool  TextGetSize(
   const string       text,          // la ligne du texte
   uint&               width,        // la largeur du tampon en points
   uint&               height        // la hauteur du tampon en points
   );

Réglages

text

[in]  La ligne pour laquelle on reçoit la longueur et la largeur.

width

[out]  Le paramètre d'entrée pour la réception de la largeur.

height

[out]  Le paramètre d'entrée pour la réception de la hauteur.

La valeur rendue

Rend true en cas de l'exécution réussie, autrement rend false. Les codes possibles des erreurs:

  • ERR_INTERNAL_ERROR(4001) - en cas de l'erreur du système d'exploitation.

 

Exemple :

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   COORD_X    200
#define   COORD_Y    100
#define   OBJ_NAME   "TestTextGetSizeBitmapLabel"
#define   RES_NAME   "TestTextGetSizeResource"
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- prépare 3 lignes de texte pour l'affichage sur le graphique
   string text1="This is the first line of text";
   string text2="The second line also contains text";
   string text3="Each word in each line has its own size";
   
   string text_array1[];   // tableau pour récupérer les mots de text1
   string text_array2[];   // tableau pour récupérer les mots de text2
   string text_array3[];   // tableau pour récupérer les mots de text3
   
//--- remplit les 3 tableaux de mots
   if(!SplitTextIntoWords(text1text_array1) || !SplitTextIntoWords(text2text_array2) || !SplitTextIntoWords(text3text_array3))
      return;
      
//--- Identifiant du graphique courant
   long   chart_idChartID();
   
//--- déclare les paramètres de la ressource graphique
   uint   rc_width =(int)ChartGetInteger(chart_idCHART_WIDTH_IN_PIXELS); 
   uint   rc_height=(int)ChartGetInteger(chart_idCHART_HEIGHT_IN_PIXELS); 
   uint   rc_data[]; 
   uint   rc_size=rc_width*rc_height;
  
//--- crée une ressource graphique pour l'affichage du texte
   if(!CreateResource(chart_idrc_datarc_widthrc_height))
      return;
   
//--- récupère la taille du caractère espace (largeur et hauteur)
   int space_w=0space_h=0;
   if(!TextGetSize(" "space_wspace_h))
     {
      PrintFormat("%s: TextGetSize() failed. Error code %d",__FUNCTION__GetLastError()); 
      return
     }
   
//--- augmente l'indentation verticale entre les chaînes de caractères de 2 et trace les textes des 3 tableaux sur le graphique
   space_h+=2;
   TextArrayToChart(1text_array1COORD_XCOORD_Y+space_h*0space_wrc_datarc_widthrc_height);
   TextArrayToChart(2text_array2COORD_XCOORD_Y+space_h*1space_wrc_datarc_widthrc_height);
   TextArrayToChart(3text_array3COORD_XCOORD_Y+space_h*2space_wrc_datarc_widthrc_height);
   
//--- une fois que tous les textes ont été affichés, met à jour les données de la ressource
   Update(RES_NAMErc_datarc_widthrc_heighttrue);
   
//--- attends 5 secondes, puis libère la ressource et supprime l'objet graphique
   Sleep(5000);
   ResourceFree(RES_NAME);
   ObjectDelete(chart_idOBJ_NAME);
   /*
   Trois chaînes de texte s'affichent sur le graphique suite à l'exécution du script.
   Dans chaque chaîne de caractères, chaque mot est affiché à une certaine distance du mot précédent,
   égale à la largeur du texte du mot précédent obtenue à l'aide de la fonction TextGetSize();
   le journal contiendra tous les mots de chaque chaîne de caractères avec leur taille :
   Text array 1:
   [0word"This"width=29height=18
   [1word"is"width=12height=18
   [2word"the"width=21height=18
   [3word"first"width=25height=18
   [4word"line"width=24height=18
   [5word"of"width=13height=18
   [6word"text"width=24height=18
   Text array 2:
   [0word"The"width=26height=18
   [1word"second"width=51height=18
   [2word"line"width=24height=18
   [3word"also"width=29height=18
   [4word"contains"width=58height=18
   [5word"text"width=24height=18
   Text array 3:
   [0word"Each"width=36height=18
   [1word"word"width=34height=18
   [2word"in"width=12height=18
   [3word"each"width=34height=18
   [4word"line"width=24height=18
   [5word"has"width=25height=18
   [6word"its"width=16height=18
   [7word"own"width=28height=18
   [8word"size"width=28height=18
   */ 
  }
//+----------------------------------------------------------------------------+
//| Découpe une chaîne en un tableau de mots séparés par un espace (" ")      |
//+----------------------------------------------------------------------------+
bool SplitTextIntoWords(const string textstring &array[])
  {
   ResetLastError();
   if(StringSplit(textStringGetCharacter(" "0), array)<0)
     {
      PrintFormat("%s: StringSplit() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Affiche un texte depuis un tableau sur un graphique              |
//+------------------------------------------------------------------+
void TextArrayToChart(int array_numstring &array[], const int text_xconst int text_yint space_wuint &pixel_data[], const uint res_widthconst uint res_height)
  {
   int width=0height=0;  // largeur et hauteur du texte
   int x=text_x;           // coordonnée X du texte affiché
   
//--- affiche un en-tête contenant le nom du tableau de mots traité
   Print("Text array "array_num,":");
   
//--- dans une boucle sur le tableau de mots
   int total=(int)array.Size();
   for(int i=0i<totali++)
     {
      //--- récupère le mot suivant et l'envoie au graphique (il est dessiné dans le tableau de pixels)
      string word=array[i];
      TextOut(wordxtext_yANCHOR_LEFT_UPPERpixel_datares_widthres_heightColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
      
      //--- récupère la taille du texte du mot en cours
      ResetLastError();
      if(!TextGetSize(wordwidthheight))
        {
         PrintFormat("%s: TextGetSize(\"%s\") failed. Error code %d",__FUNCTION__wordGetLastError()); 
         continue
        }
      //--- écrit le texte dans le journal - le mot, sa largeur et sa hauteur,
      //--- augmente ensuite la coordonnée X du mot suivant par (largeur du mot) + (largeur de l'espace)
      PrintFormat("[%d] word: \"%s\", width=%d, height=%d",iwordwidthheight);
      x+=width+space_w;
     }
  }
//+------------------------------------------------------------------+
//| Crée une ressource graphique pour le graphique entier            |
//+------------------------------------------------------------------+
bool CreateResource(const long chart_iduint &pixel_data[], const uint widthconst uint height)
  {
//--- définit la taille du tableau de pixels
   ResetLastError(); 
   uint size=width*height;
   if(ArrayResize(pixel_datasize)!=size
     { 
      PrintFormat("%s: ArrayResize() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     } 
//--- remplit le tableau de pixels avec une couleur transparente et crée une ressource graphique basée sur ce tableau
   ArrayInitialize(pixel_data0x00FFFFFF); 
   if(!ResourceCreate(RES_NAMEpixel_datawidthheight000COLOR_FORMAT_ARGB_NORMALIZE)) 
     { 
      PrintFormat("%s: ResourceCreate() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     } 
  
//--- crée un objet Etiquette Graphique (Graphic Label) aux coordonnées du coin supérieur gauche du graphique
   if(!ObjectCreate(0OBJ_NAMEOBJ_BITMAP_LABEL000)) 
     { 
      PrintFormat("%s: ObjectCreate() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     } 
//--- définit la largeur et la hauteur de l'objet bitmap créé égales à la largeur et à la hauteur de la ressource graphique
//--- définit le point d'ancrage au centre
   if(!ObjectSetInteger(chart_idOBJ_NAMEOBJPROP_XSIZEwidth))
     {
      PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
   if(!ObjectSetInteger(chart_idOBJ_NAMEOBJPROP_YSIZEheight))
     {
      PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
//--- spécifie la ressource graphique précédemment créée pour l'objet bitmap sous forme de fichier image
//--- dans ce cas, pour indiquer le nom de la ressource graphique utilisée, il faut ajouter "::" avant son nom
   if(!ObjectSetString(chart_idOBJ_NAMEOBJPROP_BMPFILE"::"+RES_NAME))
     {
      PrintFormat("%s: ObjectSetString() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
    
//--- tout va bien
   return(true);
  }
//+------------------------------------------------------------------+ 
//| Met à jour les données de la ressource graphique                 |
//+------------------------------------------------------------------+ 
void Update(const string res_nameconst uint &pixel_data[], const uint widthconst uint heightconst bool redraw
  { 
//--- passe si aucune dimension n'est passée
   if(width==0 || height==0
      return
//--- met à jour les données de la ressource et redessine le graphique
   if(ResourceCreate(res_namepixel_datawidthheight000COLOR_FORMAT_ARGB_NORMALIZE) && redraw
      ChartRedraw(); 
  } 

 

Voir aussi

Les ressources, ResourceCreate(), ResourceSave(), TextSetFont(), TextOut()