#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"
#define COLOR_NULL 0x00FFFFFF
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- current chart ID
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};
//--- disable drawing any attributes of the price chart
ChartSetInteger(chart_id, CHART_SHOW, false);
//--- declare the parameters of the graphical resource
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;
//--- create a graphical resource for text output
if(!CreateResource(chart_id, rc_data, rc_width, rc_height))
return;
//--- loop through font names
for(int i=0; i<(int)font_names.Size(); i++)
{
//--- loop through font flags
for(int j=0; j<(int)flags_array.Size(); j++)
{
//--- draw text with the font and style flag obtained from the array
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);
}
//--- after outputting all texts with different font sizes and styles,
//--- show text with different font width flags
for(int i=0; i<(int)fw_array.Size(); i++)
{
//--- set the font size and name for displaying text with width flags
string font_name="Tahoma";
int size=-140;
TextSetFont(font_name, size, fw_array[i]);
//--- create a drawn text string, output it to the resource pixel array, and update the resource
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);
//--- wait a second
Sleep(1000);
ArrayInitialize(rc_data, COLOR_NULL);
}
//--- wait five seconds, then free the resource and delete the graphical object
Sleep(5000);
ResourceFree(RES_NAME);
ObjectDelete(chart_id, OBJ_NAME);
//--- allow drawing any attributes of the price chart
ChartSetInteger(chart_id, CHART_SHOW, true);
ChartRedraw(chart_id);
/*
Text messages are displayed on the chart as a result of the script execution.
The texts have different fonts, as well as style and width flags
*/
}
//+------------------------------------------------------------------+
//| Display five text strings of different |
//| sizes with the specified font and flags |
//+------------------------------------------------------------------+
void DrawText(const string font_name, uint flags, uint &pixels_array[], uint res_width, uint res_height)
{
//--- output five strings of text with different font sizes
for(int i=0; i<5; i++)
{
//--- calculate the font size and set the font for text output using drawing methods
int size=-140+10*i;
TextSetFont(font_name, size, flags);
//--- create a drawn text string, output it to the resource pixel array, and update the resource
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);
//--- wait a bit
Sleep(800);
}
}
//+------------------------------------------------------------------+
//| Return a description of the font style flags |
//+------------------------------------------------------------------+
string FlagDescription(const uint flag)
{
switch(flag)
{
case FONT_ITALIC : return(" Italic");
case FONT_UNDERLINE : return(" Underline");
case FONT_STRIKEOUT : return(" Strikeout");
}
return("");
}
//+------------------------------------------------------------------+
//| Return the description of the font width flags |
//+------------------------------------------------------------------+
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("");
}
//+------------------------------------------------------------------+
//| Create a graphical resource for the entire chart |
//+------------------------------------------------------------------+
bool CreateResource(const long chart_id, uint &pixel_data[], const uint width, const uint height)
{
//--- set the size of the pixel array
ResetLastError();
uint size=width*height;
if(ArrayResize(pixel_data, size)!=size)
{
PrintFormat("%s: ArrayResize() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- fill the pixel array with a transparent color and create a graphical resource based on it
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);
}
//--- create the Graphic Label object at the coordinates of the upper left corner of the chart
if(!ObjectCreate(0, OBJ_NAME, OBJ_BITMAP_LABEL, 0, 0, 0))
{
PrintFormat("%s: ObjectCreate() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- set the width and height of the created bitmap object equal to the width and height of the graphical resource.
//--- set the object anchor point to its center.
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);
}
//--- specify the previously created graphical resource for the bitmap object as an image file
//--- in this case, in order to indicate the name of the graphical resource used, we need to add "::" before its name
if(!ObjectSetString(chart_id, OBJ_NAME, OBJPROP_BMPFILE, "::"+RES_NAME))
{
PrintFormat("%s: ObjectSetString() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- all is fine
return(true);
}
//+------------------------------------------------------------------+
//| Update graphical resource data |
//+------------------------------------------------------------------+
void Update(const string res_name, const uint &pixel_data[], const uint width, const uint height, const bool redraw)
{
//--- leave if zero dimensions are passed
if(width==0 || height==0)
return;
//--- update resource data and redraw the chart
if(ResourceCreate(res_name, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE) && redraw)
ChartRedraw();
}
|