the size of text object label

To add comments, please log in or register
Irwan Adnan
970
Irwan Adnan  

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
38376
Alain Verleyen  
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
970
Irwan Adnan  
angevoyageur:
OBJPROP_XSIZE
I have try it, but the result is 0. 
Alain Verleyen
38376
Alain Verleyen  
belido:
I have try it, but the result is 0. 
What are you trying to achieve exactly ?
Eugeniy Lugovoy
1976
Eugeniy Lugovoy  
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
38376
Alain Verleyen  
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
970
Irwan Adnan  
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
38376
Alain Verleyen  
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
970
Irwan Adnan  
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.
Ricardo Gunawan
31
Ricardo Gunawan  
Alain Verleyen:

Try this :

How to write these codes to Custom indicator?
To add comments, please log in or register