MQL4 绘制一个包含文本的动态 Rectangle_Label

MQL4 Drawing a Dynamic Rectangle_Label with a Text in It

我正在尝试绘制一个矩形标签,每个刻度中都有一个文本。我希望文本完全适合 Rectangle_Label。作为文本,我正在使用标签。但是不能让它正常工作..它的位置不正确..

事实上,我想创建一个 class 将所有功能合而为一...就像一个带有文本的矩形,它始终具有相同的坐标和大小等。

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

 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;
    }
 }

您不能每次都调用 ObjectCreate() - 它会 return 错误 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
}

当您说要创建 class 时,您的思路是对的。对您来说幸运的是,standard library 已经包含了制作图表对象所需的所有 classes。 Documentation

示例指标:

#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; 
}