#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()
{
//--- preparare tre righe di testo per l'output sul grafico
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[]; // array per ottenere l'insieme di parole dalla stringa 1
string text_array2[]; // array per ottenere l'insieme di parole dalla stringa 2
string text_array3[]; // array per ottenere l'insieme di parole dalla stringa 3
//--- riempire tre array di parole
if(!SplitTextIntoWords(text1, text_array1) || !SplitTextIntoWords(text2, text_array2) || !SplitTextIntoWords(text3, text_array3))
return;
//--- ID del grafico corrente
long chart_id= ChartID();
//--- dichiarare i parametri della risorsa grafica
uint rc_width =(int)ChartGetInteger(chart_id, CHART_WIDTH_IN_PIXELS);
uint rc_height=(int)ChartGetInteger(chart_id, CHART_HEIGHT_IN_PIXELS);
uint rc_data[];
uint rc_size=rc_width*rc_height;
//--- creare una risorsa grafica per l'output di testo
if(!CreateResource(chart_id, rc_data, rc_width, rc_height))
return;
//--- ottenere la dimensione del carattere spazio in base alla larghezza e all'altezza
int space_w=0, space_h=0;
if(!TextGetSize(" ", space_w, space_h))
{
PrintFormat("%s: TextGetSize() failed. Error code %d",__FUNCTION__, GetLastError());
return;
}
//--- aumentare di 2 l'indentazione verticale tra le stringhe e traccia i testi dei tre array sul grafico
space_h+=2;
TextArrayToChart(1, text_array1, COORD_X, COORD_Y+space_h*0, space_w, rc_data, rc_width, rc_height);
TextArrayToChart(2, text_array2, COORD_X, COORD_Y+space_h*1, space_w, rc_data, rc_width, rc_height);
TextArrayToChart(3, text_array3, COORD_X, COORD_Y+space_h*2, space_w, rc_data, rc_width, rc_height);
//--- dopo che tutti i testi sono stati visualizzati, aggiornare i dati delle risorse
Update(RES_NAME, rc_data, rc_width, rc_height, true);
//--- attendere cinque secondi, quindi libera la risorsa ed elimina l'oggetto grafico
Sleep(5000);
ResourceFree(RES_NAME);
ObjectDelete(chart_id, OBJ_NAME);
/*
tre stringhe di testo vengono visualizzate sul grafico come risultato dell'esecuzione dello script
ogni singola parola in ogni stringa viene visualizzata ad una distanza dalla parola precedente,
uguale alla larghezza del testo della parola precedente ottenuta utilizzando la funzione TextGetSize();
il diario conterrà tutte le parole di ogni stringa con le loro dimensioni:
Text array 1:
[0] word: "This", width=29, height=18
[1] word: "is", width=12, height=18
[2] word: "the", width=21, height=18
[3] word: "first", width=25, height=18
[4] word: "line", width=24, height=18
[5] word: "of", width=13, height=18
[6] word: "text", width=24, height=18
Text array 2:
[0] word: "The", width=26, height=18
[1] word: "second", width=51, height=18
[2] word: "line", width=24, height=18
[3] word: "also", width=29, height=18
[4] word: "contains", width=58, height=18
[5] word: "text", width=24, height=18
Text array 3:
[0] word: "Each", width=36, height=18
[1] word: "word", width=34, height=18
[2] word: "in", width=12, height=18
[3] word: "each", width=34, height=18
[4] word: "line", width=24, height=18
[5] word: "has", width=25, height=18
[6] word: "its", width=16, height=18
[7] word: "own", width=28, height=18
[8] word: "size", width=28, height=18
*/
}
//+---------------------------------------------------------------------------------------------------------+
//| Dividi una stringa in un array di parole utilizzando il separatore di spazio (" ") |
//+---------------------------------------------------------------------------------------------------------+
bool SplitTextIntoWords(const string text, string &array[])
{
ResetLastError();
if(StringSplit(text, StringGetCharacter(" ", 0), array)<0)
{
PrintFormat("%s: StringSplit() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
return(true);
}
//+----------------------------------------------------------------------+
//| Visualizzare il testo da un array su un grafico |
//+----------------------------------------------------------------------+
void TextArrayToChart(int array_num, string &array[], const int text_x, const int text_y, int space_w, uint &pixel_data[], const uint res_width, const uint res_height)
{
int width=0, height=0; // larghezza e altezza del testo
int x=text_x; // Coordinata X del testo di output
//--- stampa un'intestazione con il nome dell'array di parole elaborato
Print("Text array ", array_num,":");
//--- in un ciclo tramite l'array di parole
int total=(int)array.Size();
for(int i=0; i<total; i++)
{
//--- ottenere la parola successiva e inviarla al grafico (la disegniamo nell'array di pixel della risorsa)
string word=array[i];
TextOut(word, x, text_y, ANCHOR_LEFT_UPPER, pixel_data, res_width, res_height, ColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
//--- ottenere la dimensione del testo della parola corrente
ResetLastError();
if(!TextGetSize(word, width, height))
{
PrintFormat("%s: TextGetSize(\"%s\") failed. Error code %d",__FUNCTION__, word, GetLastError());
continue;
}
//--- stampare i dati di testo nel journal - la parola, la sua larghezza e altezza,
//--- quindi aumentare la coordinata X della parola successiva di (larghezza della parola) + (larghezza dello spazio)
PrintFormat("[%d] word: \"%s\", width=%d, height=%d",i, word, width, height);
x+=width+space_w;
}
}
//+------------------------------------------------------------------+
//| Creare una risorsa grafica per l'intero grafico |
//+------------------------------------------------------------------+
bool CreateResource(const long chart_id, uint &pixel_data[], const uint width, const uint height)
{
//--- impostare la dimensione dell'array di pixel
ResetLastError();
uint size=width*height;
if(ArrayResize(pixel_data, size)!=size)
{
PrintFormat("%s: ArrayResize() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- riempire l'array di pixel con un colore trasparente e creare una risorsa grafica basata su di esso
ArrayInitialize(pixel_data, 0x00FFFFFF);
if(!ResourceCreate(RES_NAME, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE))
{
PrintFormat("%s: ResourceCreate() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- creare l'oggetto Etichetta Grafica alle coordinate dell'angolo in alto a sinistra del grafico
if(!ObjectCreate(0, OBJ_NAME, OBJ_BITMAP_LABEL, 0, 0, 0))
{
PrintFormat("%s: ObjectCreate() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- impostare la larghezza e l'altezza dell'oggetto bitmap creato uguali alla larghezza e all'altezza della risorsa grafica.
//--- impostare il punto di ancoraggio dell'oggetto al suo centro.
if(!ObjectSetInteger(chart_id, OBJ_NAME, OBJPROP_XSIZE, width))
{
PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
if(!ObjectSetInteger(chart_id, OBJ_NAME, OBJPROP_YSIZE, height))
{
PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- specificare la risorsa grafica creata in precedenza per l'oggetto bitmap come file immagine
//--- in questo caso, per indicare il nome della risorsa grafica utilizzata, dobbiamo aggiungere "::" prima del suo nome
if(!ObjectSetString(chart_id, OBJ_NAME, OBJPROP_BMPFILE, "::"+RES_NAME))
{
PrintFormat("%s: ObjectSetString() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- tutto è andato bene
return(true);
}
//+------------------------------------------------------------------+
//| Aggiornare i dati della risorsa grafica |
//+------------------------------------------------------------------+
void Update(const string res_name, const uint &pixel_data[], const uint width, const uint height, const bool redraw)
{
//--- esci se vengono passate dimensioni zero
if(width==0 || height==0)
return;
//--- aggiorna i dati delle risorse e ridisegna il grafico
if(ResourceCreate(res_name, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE) && redraw)
ChartRedraw();
}
|