MQL4绘制一个带有文本的动态Rectangle_Label

时间:2022-06-01 12:20:37

I am trying to draw a Rectangle Label with a text in it every tick.. I want a text to fit exactly in to a Rectangle_Label.. As a text i am using Label.. But cant get it to work exactly.. It is not correctly situated..

我试图在每个刻度线中绘制一个带有文本的矩形标签。我想要一个文本完全适合Rectangle_Label ..作为文本我正在使用Label ..但是不能让它完全正常工作..它是没有正确的位置..

In Fact i would like to create a class that would do it all in one... Just like a rectangle with text in it that would be always having same co ordinance and size etc..

事实上,我想创建一个可以一体化的类......就像一个带有文本的矩形,它总是具有相同的条件和大小等。

Any help would be greatly appreciated...

任何帮助将不胜感激...

 bool createRectangleLabel(long chart_ID,string name,string labelName,int shift,double price,string text,double xSize,double ySize,double xOffSet,double yOffSet,double xDistance,double yDistance)
     {
      if(ObjectCreate(chart_ID,labelName,OBJ_RECTANGLE_LABEL,0,TimeCurrent()-shift,price))
    {
     Print(xDistance+"  "+yDistance);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_BGCOLOR,clrBlack);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_XDISTANCE,xDistance);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_YDISTANCE,yDistance);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_YSIZE,ySize);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_XSIZE,xSize);
     ObjectSetString(chart_ID,labelName,OBJPROP_TEXT,text);
     ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,ANCHOR_CENTER);

     return true;
    }
  else
    {
     Print("createRectangleLabel return error code: ",GetLastError());
     Print("+--------------------------------------------------------------+");
     return false;
    }
 }
   bool createLineText(long chart_ID,string name,string labelName,int shift,double price,string text)
 {
  int xDistance=0;
  int yDistance=0;
  int xSize,xOffSet;
  int ySize,yOffSet;
  bool i=ChartTimePriceToXY(chart_ID,0,TimeCurrent(),price,xDistance,yDistance);

  if(ObjectCreate(chart_ID,name,OBJ_LABEL,0,TimeCurrent()-shift,price))
    {
     ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,clrWhite);
     ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,xDistance);
     ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,yDistance);
     ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
     ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,ANCHOR_CENTER);
     ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clrWhite);
     ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,10);
     xSize = ObjectGet(name,OBJPROP_XSIZE);
     ySize = ObjectGet(name,OBJPROP_YSIZE);
     xOffSet = ObjectGet(name,OBJPROP_XOFFSET);
     yOffSet = ObjectGet(name,OBJPROP_YOFFSET);
     TextGetSize(name,xSize,ySize);
     createRectangleLabel(chart_ID,name,labelName,shift,price,text,xSize,ySize,xOffSet,yOffSet,xDistance,yDistance);
     return true;
    }
  else
    {
     Print("createLineText return error code: ",GetLastError());
     Print("+--------------------------------------------------------------+");
     return false;
    }
 }

2 个解决方案

#1


0  

You're thinking along the right lines when you say that you'd like to create a class. Fortunately for you, the standard library already includes all the classes you need to make chart objects. Documentation

当你说你想要创建一个班级时,你正在考虑正确的思路。幸运的是,标准库已经包含了制作图表对象所需的所有类。文档

Example Indicator:

#property strict
#property indicator_chart_window

#include <ChartObjects\ChartObjectsTxtControls.mqh>

class MyRectLabel : public CChartObjectRectLabel
{
   CChartObjectLabel m_label;
public:
   bool Create(long chart, const string name, const int window, 
               const int X, const int Y, const int sizeX, const int sizeY)
   {
      if(!CChartObjectRectLabel::Create(chart,name,window,X,Y,sizeX,sizeY))
         return false;
      return m_label.Create(chart, name + "_", window, X + 8, Y + 12);
   }
   bool Color(const color clr){
      return m_label.Color(clr);
   }
   bool Description(const string text){
      return m_label.Description(text);
   }
   bool FontSize(const int size){
      return m_label.FontSize(size);
   }
   bool ToolTip(const string text){
      return (this.ToolTip(text) && m_label.Tooltip(text));
   }
};
//+------------------------------------------------------------------+
MyRectLabel rect_label;
//+------------------------------------------------------------------+
int OnInit()
{
   if(!rect_label.Create(0, "rlabel", 0, 5, 25, 100, 50)
      || !rect_label.BackColor(clrWhiteSmoke)
      || !rect_label.Description("LABEL!")
      || !rect_label.Tooltip("I am a rectangle label")
      || !rect_label.Color(clrBlack)
      || !rect_label.FontSize(18)
   )
      return INIT_FAILED;
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
int start()
{ 
   static double last_price = 0.;
   rect_label.Description(DoubleToString(Bid, _Digits));
   if(Bid > last_price)
      rect_label.Color(clrLimeGreen);
   else
      rect_label.Color(clrRed);
   last_price = Bid;
   return 0; 
}

#2


0  

You cannot call ObjectCreate() every tick - it would return an error 4200. If you check the object exists before creating, that would help. Alternative approach would be to try to create the object and assign it with some necessary properties (e.g., color of the object, anchor etc) in one block, and move it in another.

你不能每次打勾都调用ObjectCreate() - 它会返回错误4200.如果在创建之前检查对象是否存在,那将有所帮助。替代方法是尝试创建对象并在一个块中为其分配一些必要的属性(例如,对象的颜色,锚等),并将其移动到另一个块中。

if(ObjectFind(chart_id,labelName)<0){
   if(ObjectCreate(chart_ID,labelName,OBJ_RECTANGLE_LABEL,0,TimeCurrent()-shift,price)){
      ObjectSetInteger(chart_ID,labelName,OBJPROP_BGCOLOR,clrBlack);//etc.
   }
   ObjectSetInteger(chart_ID,labelName,OBJPROP_XDISTANCE,xDistance);
   ObjectSetInteger(chart_ID,labelName,OBJPROP_YDISTANCE,yDistance);//if you need to move the object or take other steps each tick, e.g. update text - do it here
}

#1


0  

You're thinking along the right lines when you say that you'd like to create a class. Fortunately for you, the standard library already includes all the classes you need to make chart objects. Documentation

当你说你想要创建一个班级时,你正在考虑正确的思路。幸运的是,标准库已经包含了制作图表对象所需的所有类。文档

Example Indicator:

#property strict
#property indicator_chart_window

#include <ChartObjects\ChartObjectsTxtControls.mqh>

class MyRectLabel : public CChartObjectRectLabel
{
   CChartObjectLabel m_label;
public:
   bool Create(long chart, const string name, const int window, 
               const int X, const int Y, const int sizeX, const int sizeY)
   {
      if(!CChartObjectRectLabel::Create(chart,name,window,X,Y,sizeX,sizeY))
         return false;
      return m_label.Create(chart, name + "_", window, X + 8, Y + 12);
   }
   bool Color(const color clr){
      return m_label.Color(clr);
   }
   bool Description(const string text){
      return m_label.Description(text);
   }
   bool FontSize(const int size){
      return m_label.FontSize(size);
   }
   bool ToolTip(const string text){
      return (this.ToolTip(text) && m_label.Tooltip(text));
   }
};
//+------------------------------------------------------------------+
MyRectLabel rect_label;
//+------------------------------------------------------------------+
int OnInit()
{
   if(!rect_label.Create(0, "rlabel", 0, 5, 25, 100, 50)
      || !rect_label.BackColor(clrWhiteSmoke)
      || !rect_label.Description("LABEL!")
      || !rect_label.Tooltip("I am a rectangle label")
      || !rect_label.Color(clrBlack)
      || !rect_label.FontSize(18)
   )
      return INIT_FAILED;
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
int start()
{ 
   static double last_price = 0.;
   rect_label.Description(DoubleToString(Bid, _Digits));
   if(Bid > last_price)
      rect_label.Color(clrLimeGreen);
   else
      rect_label.Color(clrRed);
   last_price = Bid;
   return 0; 
}

#2


0  

You cannot call ObjectCreate() every tick - it would return an error 4200. If you check the object exists before creating, that would help. Alternative approach would be to try to create the object and assign it with some necessary properties (e.g., color of the object, anchor etc) in one block, and move it in another.

你不能每次打勾都调用ObjectCreate() - 它会返回错误4200.如果在创建之前检查对象是否存在,那将有所帮助。替代方法是尝试创建对象并在一个块中为其分配一些必要的属性(例如,对象的颜色,锚等),并将其移动到另一个块中。

if(ObjectFind(chart_id,labelName)<0){
   if(ObjectCreate(chart_ID,labelName,OBJ_RECTANGLE_LABEL,0,TimeCurrent()-shift,price)){
      ObjectSetInteger(chart_ID,labelName,OBJPROP_BGCOLOR,clrBlack);//etc.
   }
   ObjectSetInteger(chart_ID,labelName,OBJPROP_XDISTANCE,xDistance);
   ObjectSetInteger(chart_ID,labelName,OBJPROP_YDISTANCE,yDistance);//if you need to move the object or take other steps each tick, e.g. update text - do it here
}