OBJ_LABEL

标签对象。

ObjTextLabel

注意

相对于标签的定位点位置可以从 ENUM_ANCHOR_POINT 枚举选择。定位点坐标用像素设置。

您也可以从 ENUM_BASE_CORNER 枚举选择文本标签定位角。

示例

下面的脚本创建和移动图表上的编辑对象。已开发的特别函数用于创建和改变图形对象的属性。您可以在您的个人应用中使用这些函数"as is" 。

 

//--- 描述
#property description "Script creates \"Label\" graphical object."
//--- 启动脚本期间显示输入参数的窗口
#property script_show_inputs
//--- 脚本的输入参数
input string            InpName="Label";         // 标签名称
input int               InpX=150;                // X-轴距离
input int               InpY=150;                // Y-轴距离
input string            InpFont="Arial";         // 字体
input int               InpFontSize=14;          // 字体大小
input color             InpColor=clrRed;         // 颜色
input double            InpAngle=0.0;            // 倾斜角度
input ENUM_ANCHOR_POINT InpAnchor=ANCHOR_CENTER// 定位类型
input bool              InpBack=false;           // 背景对象
input bool              InpSelection=true;       // 突出移动
input bool              InpHidden=true;          // 隐藏对象列表中
input long              InpZOrder=0;             // 鼠标单击优先
//+------------------------------------------------------------------+
//| 创建文本标签                                                       |
//+------------------------------------------------------------------+
bool LabelCreate(const long              chart_ID=0,               // 图表 ID
                 const string            name="Label",             // 标签名称
                 const int               sub_window=0,             // 子窗口指数
                 const int               x=0,                      // X 坐标
                 const int               y=0,                      // Y 坐标
                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER// 图表定位角
                 const string            text="Label",             // 文本
                 const string            font="Arial",             // 字体
                 const int               font_size=10,             // 字体大小
                 const color             clr=clrRed,               // 颜色
                 const double            angle=0.0,                // 文本倾斜
                 const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER// 定位类型
                 const bool              back=false,               // 在背景中
                 const bool              selection=false,          // 突出移动
                 const bool              hidden=true,              // 隐藏在对象列表
                 const long              z_order=0)                // 鼠标单击优先
  {
//--- 重置错误的值
   ResetLastError();
//--- 创建文本标签
   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create text label! Error code = ",GetLastError());
      return(false);
     }
//--- 设置标签坐标
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- 设置相对于定义点坐标的图表的角
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- 设置文本
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- 设置文本字体
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- 设置字体大小
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- 设置文本的倾斜角
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- 设置定位类型
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- 设置颜色
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- 显示前景 (false) 或背景 (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- 启用 (true) 或禁用 (false) 通过鼠标移动标签的模式
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- 在对象列表隐藏(true) 或显示 (false) 图形对象名称
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- 设置在图表中优先接收鼠标点击事件
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 移动文本标签                                                       |
//+------------------------------------------------------------------+
bool LabelMove(const long   chart_ID=0,   // 图表 ID
               const string name="Label"// 标签名称
               const int    x=0,          // X 坐标
               const int    y=0)          // Y 坐标
  {
//--- 重置错误的值
   ResetLastError();
//--- 移动文本标签
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))
     {
      Print(__FUNCTION__,
            ": failed to move X coordinate of the label! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))
     {
      Print(__FUNCTION__,
            ": failed to move Y coordinate of the label! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 改变绑定标签的图表角落                                              |
//+------------------------------------------------------------------+
bool LabelChangeCorner(const long             chart_ID=0,               // 图表 ID
                       const string           name="Label",             // 标签名称
                       const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER// 图表定位角
  {
//--- 重置错误的值
   ResetLastError();
//--- 改变定位角
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner))
     {
      Print(__FUNCTION__,
            ": failed to change the anchor corner! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 改变标签文本                                                       |
//+------------------------------------------------------------------+
bool LabelTextChange(const long   chart_ID=0,   // 图表 ID
                     const string name="Label"// 对象名称
                     const string text="Text")  // 文本
  {
//--- 重置错误的值
   ResetLastError();
//--- 改变对象文本
   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))
     {
      Print(__FUNCTION__,
            ": failed to change the text! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 删除文本标签                                                       |
//+------------------------------------------------------------------+
bool LabelDelete(const long   chart_ID=0,   // 图表 ID
                 const string name="Label"// 标签名称
  {
//--- 重置错误的值
   ResetLastError();
//--- 删除标签
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete a text label! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 在本地变量存储标签坐标
   int x=InpX;
   int y=InpY;
//--- 图表窗口大小
   long x_distance;
   long y_distance;
//--- 设置窗口大小
   if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))
     {
      Print("Failed to get the chart width! Error code = ",GetLastError());
      return;
     }
   if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))
     {
      Print("Failed to get the chart height! Error code = ",GetLastError());
      return;
     }
//--- 检查输入参数的正确性
   if(InpX<0 || InpX>x_distance-1 || InpY<0 || InpY>y_distance-1)
     {
      Print("Error! Incorrect values of input parameters!");
      return;
     }
//--- 准备标签的初始文本
   string text;
   StringConcatenate(text,"Upper left corner: ",x,",",y);
//--- 在图表上创建文本标签
   if(!LabelCreate(0,InpName,0,InpX,InpY,CORNER_LEFT_UPPER,text,InpFont,InpFontSize,
      InpColor,InpAngle,InpAnchor,InpBack,InpSelection,InpHidden,InpZOrder))
     {
      return;
     }
//--- 重画图表并等待0.5秒
   ChartRedraw();
   Sleep(500);
//--- 同时移动标签和改变其文本
//--- 轴的迭代次数
   int h_steps=(int)(x_distance/2-InpX);
   int v_steps=(int)(y_distance/2-InpY);
//--- 向下移动标签
   for(int i=0;i<v_steps;i++)
     {
      //--- 改变坐标
      y+=2;
      //--- 移动标签并更改其文本
      MoveAndTextChange(x,y,"Upper left corner: ");
     }
//--- 半秒延迟
   Sleep(500);
//--- 向右移动标签
   for(int i=0;i<h_steps;i++)
     {
      //--- 改变坐标
      x+=2;
      //--- 移动标签并更改其文本
      MoveAndTextChange(x,y,"Upper left corner: ");
     }
//--- 半秒延迟
   Sleep(500);
//--- 向上移动标签
   for(int i=0;i<v_steps;i++)
     {
      //--- 改变坐标
      y-=2;
      //--- 移动标签并更改其文本
      MoveAndTextChange(x,y,"Upper left corner: ");
     }
//--- 半秒延迟
   Sleep(500);
//--- 向左移动标签
   for(int i=0;i<h_steps;i++)
     {
      //--- 改变坐标
      x-=2;
      //--- 移动标签并更改其文本
      MoveAndTextChange(x,y,"Upper left corner: ");
     }
//--- 半秒延迟
   Sleep(500);
//--- 现在,通过改变定位角来移动点的位置
//--- 移动到左下角
   if(!LabelChangeCorner(0,InpName,CORNER_LEFT_LOWER))
      return;
//--- 改变标签文本
   StringConcatenate(text,"Lower left corner: ",x,",",y);
   if(!LabelTextChange(0,InpName,text))
      return;
//--- 重绘图表并等待2秒
   ChartRedraw();
   Sleep(2000);
//--- 移动到右下角
   if(!LabelChangeCorner(0,InpName,CORNER_RIGHT_LOWER))
      return;
//--- 改变标签文本
   StringConcatenate(text,"Lower right corner: ",x,",",y);
   if(!LabelTextChange(0,InpName,text))
      return;
//--- 重绘图表并等待2秒
   ChartRedraw();
   Sleep(2000);
//--- 移动到右上角
   if(!LabelChangeCorner(0,InpName,CORNER_RIGHT_UPPER))
      return;
//--- 改变标签文本
   StringConcatenate(text,"Upper right corner: ",x,",",y);
   if(!LabelTextChange(0,InpName,text))
      return;
//--- 重绘图表并等待2秒
   ChartRedraw();
   Sleep(2000);
//--- 移动到左上角
   if(!LabelChangeCorner(0,InpName,CORNER_LEFT_UPPER))
      return;
//--- 改变标签文本
   StringConcatenate(text,"Upper left corner: ",x,",",y);
   if(!LabelTextChange(0,InpName,text))
      return;
//--- 重绘图表并等待2秒
   ChartRedraw();
   Sleep(2000);
//--- 删除标签
   LabelDelete(0,InpName);
//--- 重画图表并等待0.5秒
   ChartRedraw();
   Sleep(500);
//---
  }
//+------------------------------------------------------------------+
//| 函数移动对象并改变其文本                                            |
//+------------------------------------------------------------------+
bool MoveAndTextChange(const int x,const int y,string text)
  {
//--- 移动标签
   if(!LabelMove(0,InpName,x,y))
      return(false);
//--- 改变标签文本
   StringConcatenate(text,text,x,",",y);
   if(!LabelTextChange(0,InpName,text))
      return(false);
//--- 检查脚本操作是否已经强制禁用
   if(IsStopped())
      return(false);
//--- 重画图表
   ChartRedraw();
// 0.01 秒延迟
   Sleep(10);
//--- 推出函数
   return(true);
  }