Download MetaTrader 5

the size of text object label

To add comments, please log in or register
Irwan Adnan
925
Irwan Adnan 2014.08.19 16:06 

Hi, if I create an object text label, how can I get the size (length) of that text in pixels. It similars like TextGetSize function. I have try with OBJPROP_XDISTANCE but it didn't work. Appreciate if you can explain clearly.

Thanks. 

Alain Verleyen
Moderator
31085
Alain Verleyen 2014.08.19 16:21  
belido:

Hi, if I create an object text label, how can I get the size (length) of that text in pixels. It similars like TextGetSize function. I have try with OBJPROP_XDISTANCE but it didn't work. Appreciate if you can explain clearly.

Thanks. 

OBJPROP_XSIZE
Irwan Adnan
925
Irwan Adnan 2014.08.19 17:00  
angevoyageur:
OBJPROP_XSIZE
I have try it, but the result is 0. 
Alain Verleyen
Moderator
31085
Alain Verleyen 2014.08.19 21:06  
belido:
I have try it, but the result is 0. 
What are you trying to achieve exactly ?
Eugeniy Lugovoy
1991
Eugeniy Lugovoy 2014.08.20 02:12  
belido:
I have try it, but the result is 0. 

Yes, I've got same problem when I'm trying to get this value right after creation. But when the chart is rendered and show object, this value become to be right.

 objectCreate(oID+"SZ", 0, 0, "XXX", FontSize, FontName, clrBlack); // object creation function

 dy = (int)ObjectGetInteger(0,oID+"SZ",OBJPROP_YSIZE,0)*2;
 dx = (int)ObjectGetInteger(0,oID+"SZ",OBJPROP_XSIZE,0);

 On first time running (OnInit) this code the dx & dy equal to 0.

But on next tick they have right values. Even WindowRedraw() cannot help.

So, my guess is chart should be rendered by an internal MT4 routine and then we can get such values.

Alain Verleyen
Moderator
31085
Alain Verleyen 2014.08.20 12:08  
elugovoy:

Yes, I've got same problem when I'm trying to get this value right after creation. But when the chart is rendered and show object, this value become to be right.

 On first time running (OnInit) this code the dx & dy equal to 0.

But on next tick they have right values. Even WindowRedraw() cannot help.

So, my guess is chart should be rendered by an internal MT4 routine and then we can get such values.

WindowRedraw() ? do you mean ChartRedraw() ?
Irwan Adnan
925
Irwan Adnan 2014.08.20 14:21  
angevoyageur:
What are you trying to achieve exactly ?

After I created text object label (Label1), I want to put another object label (Label2) after the first label based on first text size (in pixels).

Alain Verleyen
Moderator
31085
Alain Verleyen 2014.08.20 16:22  
belido:

After I created text object label (Label1), I want to put another object label (Label2) after the first label based on first text size (in pixels).

Try this :

//--- input parameters
input   string font="Arial";
input   int fontsize=28;
//+------------------------------------------------------------------+
//| Indicator initialization function                                |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   uint w,h;
   ChartSetInteger(0,CHART_EVENT_OBJECT_CREATE,true);
   TextSetFont(font,-fontsize*10);
   string label1_text="First label.";
   string label2_text="Second label.";
//---
   CreateLabel("test1",label1_text,100,100);
   TextGetSize(label1_text,w,h);
   printf("Label %s width=%u","test1",w);
//---
   CreateLabel("test2",label2_text,100+w,100);
   TextGetSize(label2_text,w,h);
   printf("Label %s width=%u","test2",w);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Create a label                                                   |
//+------------------------------------------------------------------+
bool CreateLabel(string name,string text,int x,int y)
  {
   if(ObjectCreate(0,name,OBJ_LABEL,0,0,0))
     {
      //ObjectSetString(0,name,OBJPROP_NAME
      ObjectSetString(0,name,OBJPROP_FONT,font);
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,fontsize);
      ObjectSetString(0,name,OBJPROP_TEXT,text);
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CREATE)
     {
      printf("Object create : name %s size=%i",sparam,ObjectGetInteger(0,sparam,OBJPROP_XSIZE));
     }
  }
//+------------------------------------------------------------------+
//| Indicator deinitialization function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0,-1,OBJ_LABEL);
  }
//+------------------------------------------------------------------+
Irwan Adnan
925
Irwan Adnan 2014.08.20 16:57  
angevoyageur:

Try this :

Thanks for quick response. Appreciate that. So I conclude that we must use same fonttype and fontsize both in TextSetFont() and fonttype and fontsize in ObjectSetInteger(font/fontsize).
Regards.
To add comments, please log in or register