#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link ""
#property version "1.00"
#define COORD_X 200
#define COORD_Y 100
#define OBJ_NAME "TestTextGetSizeBitmapLabel"
#define RES_NAME "TestTextGetSizeResource"
#define COLOR_NULL 0x00FFFFFF
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Identifiant du graphique courant
long chart_id= ChartID();
string font_names[] ={"Arial", "Tahoma", "Calibri"};
uint flags_array[]={0, FONT_ITALIC, FONT_UNDERLINE, FONT_STRIKEOUT};
uint fw_array[]={FW_DONTCARE, FW_THIN, FW_EXTRALIGHT, FW_ULTRALIGHT, FW_LIGHT,
FW_NORMAL, FW_REGULAR, FW_MEDIUM, FW_SEMIBOLD, FW_DEMIBOLD,
FW_BOLD, FW_EXTRABOLD, FW_ULTRABOLD, FW_HEAVY, FW_BLACK};
//--- désactive l'affichage de tous les attributs du graphique des prix
ChartSetInteger(chart_id, CHART_SHOW, false);
//--- déclare les paramètres de la ressource graphique
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;
//--- crée une ressource graphique pour l'affichage du texte
if(!CreateResource(chart_id, rc_data, rc_width, rc_height))
return;
//--- boucle sur les noms des polices de caractères
for(int i=0; i<(int)font_names.Size(); i++)
{
//--- boucle sur les flags de la police de caractères
for(int j=0; j<(int)flags_array.Size(); j++)
{
//--- dessine le texte avec la police de caractères et le style obtenus du tableau
DrawText(font_names[i], flags_array[j], rc_data, rc_width, rc_height);
Sleep(800);
ArrayInitialize(rc_data, COLOR_NULL);
}
Sleep(800);
ArrayInitialize(rc_data, COLOR_NULL);
}
//--- après avoir affiché tous les textes avec différentes tailles et styles de police,
//--- affiche le texte avec différentes tailles
for(int i=0; i<(int)fw_array.Size(); i++)
{
//--- définit la taille et le nom de la police de caractères pour afficher le texte
string font_name="Tahoma";
int size=-140;
TextSetFont(font_name, size, fw_array[i]);
//--- crée une chaîne de texte dessinée, l'affiche dans le tableau de pixels de la ressource et met à jour la ressource
string text=StringFormat("Text%d: Font name: \"%s%s\", size: %d (%d)", i+1, font_name, FlagWidthDescription(fw_array[i]), size, size/-10);
TextOut(text, COORD_X, COORD_Y, ANCHOR_LEFT_UPPER, rc_data, rc_width, rc_height, ColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
Update(RES_NAME, rc_data, rc_width, rc_height, true);
//--- attend 1 seconde
Sleep(1000);
ArrayInitialize(rc_data, COLOR_NULL);
}
//--- attends 5 secondes, puis libère la ressource et supprime l'objet graphique
Sleep(5000);
ResourceFree(RES_NAME);
ObjectDelete(chart_id, OBJ_NAME);
//--- permet de dessiner n'importe quel attribut sur le graphique des prix
ChartSetInteger(chart_id, CHART_SHOW, true);
ChartRedraw(chart_id);
/*
Les messages texte s'affichent sur le graphique suite à l'exécution du script.
Les textes utilisent des polices différentes, et leurs styles et largeurs varient également.
*/
}
//+------------------------------------------------------------------+
//| Affiche 5 chaînes de caractères de tailles différentes |
//| avec la police et les flags spécifiés |
//+------------------------------------------------------------------+
void DrawText(const string font_name, uint flags, uint &pixels_array[], uint res_width, uint res_height)
{
//--- affiche 5 chaînes de texte avec des tailles de police différentes
for(int i=0; i<5; i++)
{
//--- calcule la taille et définit la police de caractère pour l'affichage du texte avec les méthodes de dessin
int size=-140+10*i;
TextSetFont(font_name, size, flags);
//--- crée une chaîne de texte dessinée, l'affiche dans le tableau de pixels de la ressource et met à jour la ressource
string text=StringFormat("Text%d: Font name: \"%s%s\", size: %d (%d)", i+1, font_name, FlagDescription(flags), size, size/-10);
TextOut(text, COORD_X, COORD_Y+22*i, ANCHOR_LEFT_UPPER, pixels_array, res_width, res_height, ColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
Update(RES_NAME, pixels_array, res_width, res_height, true);
//--- attends un peu
Sleep(800);
}
}
//+------------------------------------------------------------------+
//| Retourne une description des flags de style de la police |
//+------------------------------------------------------------------+
string FlagDescription(const uint flag)
{
switch(flag)
{
case FONT_ITALIC : return(" Italic");
case FONT_UNDERLINE : return(" Underline");
case FONT_STRIKEOUT : return(" Strikeout");
}
return("");
}
//+------------------------------------------------------------------+
//| Retourne une description des flags de largeurs de la police |
//+------------------------------------------------------------------+
string FlagWidthDescription(const uint flag)
{
switch(flag)
{
case FW_DONTCARE : return(" Dontcare");
case FW_THIN : return(" Thin");
case FW_EXTRALIGHT: return(" Extralight");
case FW_ULTRALIGHT: return(" Ultralight");
case FW_LIGHT : return(" Light");
case FW_NORMAL : return(" Normal");
case FW_REGULAR : return(" Regular");
case FW_MEDIUM : return(" Medium");
case FW_SEMIBOLD : return(" Semibold");
case FW_DEMIBOLD : return(" Demibold");
case FW_BOLD : return(" Bold");
case FW_EXTRABOLD : return(" Extrabold");
case FW_ULTRABOLD : return(" Ultrabold");
case FW_HEAVY : return(" Heavy");
case FW_BLACK : return(" Black");
}
return("");
}
//+------------------------------------------------------------------+
//| Crée une ressource graphique pour le graphique entier |
//+------------------------------------------------------------------+
bool CreateResource(const long chart_id, uint &pixel_data[], const uint width, const uint height)
{
//--- définit la taille du tableau de pixels
ResetLastError();
uint size=width*height;
if(ArrayResize(pixel_data, size)!=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_data, COLOR_NULL);
if(!ResourceCreate(RES_NAME, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE))
{
PrintFormat("%s: ResourceCreate() failed. Error code ",__FUNCTION__, GetLastError());
return(false);
}
//--- crée un objet Etiquette Graphique (Graphic Label) aux coordonnées du coin supérieur gauche du graphique
if(!ObjectCreate(0, OBJ_NAME, OBJ_BITMAP_LABEL, 0, 0, 0))
{
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_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);
}
//--- 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_id, OBJ_NAME, OBJPROP_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_name, const uint &pixel_data[], const uint width, const uint height, const 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_name, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE) && redraw)
ChartRedraw();
}
|