OBJ_EDIT

编辑对象。

ObjEdit

注意

定位点坐标用像素设置。您可以从ENUM_BASE_CORNER枚举选择编辑定位角。

您也可以从 ENUM_ALIGN_MODE 枚举在编辑中选择一种文本对齐类型。

示例

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

//--- 描述
#property description "Script creates \"Edit\" object."
//--- 启动脚本期间显示输入参数的窗口
#property script_show_inputs
//--- 脚本的输入参数
input string           InpName="Edit";              // 对象名称
input string           InpText="Text";              // 对象文本
input string           InpFont="Arial";             // 字体
input int              InpFontSize=14;              // 字体大小
input ENUM_ALIGN_MODE  InpAlign=ALIGN_CENTER;       // 文本对齐类型
input bool             InpReadOnly=false;           // 编辑能力
input ENUM_BASE_CORNER InpCorner=CORNER_LEFT_UPPER// 图表定位角
input color            InpColor=clrBlack;           // 文本颜色
input color            InpBackColor=clrWhite;       // 背景色
input color            InpBorderColor=clrBlack;     // 边框颜色
input bool             InpBack=false;               // 背景对象
input bool             InpSelection=false;          // 突出移动
input bool             InpHidden=true;              // 隐藏在对象列表
input long             InpZOrder=0;                 // 鼠标单击优先
//+------------------------------------------------------------------+
//| 创建编辑对象                                                       |
//+------------------------------------------------------------------+
bool EditCreate(const long             chart_ID=0,               // 图表 ID
                const string           name="Edit",              // 对象名称
                const int              sub_window=0,             // 子窗口指数
                const int              x=0,                      // X 坐标
                const int              y=0,                      // Y 坐标
                const int              width=50,                 // 宽度
                const int              height=18,                // 高度
                const string           text="Text",              // 文本
                const string           font="Arial",             // 字体
                const int              font_size=10,             // 字体大小
                const ENUM_ALIGN_MODE  align=ALIGN_CENTER,       // 对齐类型
                const bool             read_only=false,          // 编辑能力
                const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER// 图表定位角
                const color            clr=clrBlack,             // 文本颜色
                const color            back_clr=clrWhite,        // 背景色
                const color            border_clr=clrNONE,       // 边框颜色
                const bool             back=false,               // 在背景中
                const bool             selection=false,          // 突出移动
                const bool             hidden=true,              // 隐藏在对象列表
                const long             z_order=0)                // 鼠标单击优先
  {
//--- 重置错误的值
   ResetLastError();
//--- 创建编辑字段
   if(!ObjectCreate(chart_ID,name,OBJ_EDIT,sub_window,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create \"Edit\" object! Error code = ",GetLastError());
      return(false);
     }
//--- 设置对象坐标
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- 设置对象大小
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
//--- 设置文本
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- 设置文本字体
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- 设置字体大小
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- 设置对象文本算法的类型
   ObjectSetInteger(chart_ID,name,OBJPROP_ALIGN,align);
//--- 启用 (true) 或禁用 (false) 只读模式
   ObjectSetInteger(chart_ID,name,OBJPROP_READONLY,read_only);
//--- 设置相对于定义对象坐标的图表的角
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- 设置文本颜色
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- 设置背景颜色
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
//--- 设置边界颜色
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_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 EditMove(const long   chart_ID=0,  // 图表ID
              const string name="Edit"// 对象名称
              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 object! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))
     {
      Print(__FUNCTION__,
            ": failed to move Y coordinate of the object! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 调整编辑对象                                                       |
//+------------------------------------------------------------------+
bool EditChangeSize(const long   chart_ID=0,  // 图表 ID
                    const string name="Edit"// 对象名称
                    const int    width=0,     // 宽度
                    const int    height=0)    // 高度
  {
//--- 重置错误的值
   ResetLastError();
//--- 改变对象大小
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width))
     {
      Print(__FUNCTION__,
            ": failed to change the object width! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height))
     {
      Print(__FUNCTION__,
            ": failed to change the object height! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 改变编辑对象文本                                                   |
//+------------------------------------------------------------------+
bool EditTextChange(const long   chart_ID=0,  // 图表 ID
                    const string name="Edit"// 对象名称
                    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 EditTextGet(string      &text,        // 文本
                 const long   chart_ID=0,  // 图表's ID
                 const string name="Edit"// 对象名称
  {
//--- 重置错误的值
   ResetLastError();
//--- 获得对象文本
   if(!ObjectGetString(chart_ID,name,OBJPROP_TEXT,0,text))
     {
      Print(__FUNCTION__,
            ": failed to get the text! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 删除编辑对象                                                       |
//+------------------------------------------------------------------+
bool EditDelete(const long   chart_ID=0,  // 图表 ID
                const string name="Edit"// 对象名称
  {
//--- 重置错误的值
   ResetLastError();
//--- 删除标签
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete \"Edit\" object! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 图表窗口大小
   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;
     }
//--- 定义改变编辑字段的步骤
   int x_step=(int)x_distance/64;
//--- 设置编辑字段坐标和其大小
   int x=(int)x_distance/8;
   int y=(int)y_distance/2;
   int x_size=(int)x_distance/8;
   int y_size=InpFontSize*2;
//--- 在本地变量存储文本
   string text=InpText;
//--- 创建编辑字段
   if(!EditCreate(0,InpName,0,x,y,x_size,y_size,InpText,InpFont,InpFontSize,InpAlign,InpReadOnly,
      InpCorner,InpColor,InpBackColor,InpBorderColor,InpBack,InpSelection,InpHidden,InpZOrder))
     {
      return;
     }
//--- 重画图表并等待1秒
   ChartRedraw();
   Sleep(1000);
//--- 延伸编辑字段
   while(x_size-x<x_distance*5/8)
     {
      //--- increase edit field's width
      x_size+=x_step;
      if(!EditChangeSize(0,InpName,x_size,y_size))
         return;
      //--- 检查脚本操作是否已经强制禁用
      if(IsStopped())
         return;
      //--- 重绘图表并等待0.05 秒
      ChartRedraw();
      Sleep(50);
     }
//--- 半秒延迟
   Sleep(500);
//--- 改变文本
   for(int i=0;i<20;i++)
     {
      //--- 在开始和结束添加 "+" 
      text="+"+text+"+";
      if(!EditTextChange(0,InpName,text))
         return;
      //--- 检查脚本操作是否已经强制禁用
      if(IsStopped())
         return;
      //--- 重绘图表并等待 0.1 秒
      ChartRedraw();
      Sleep(100);
     }
//--- 半秒延迟
   Sleep(500);
//--- 删除编辑字段
   EditDelete(0,InpName);
   ChartRedraw();
//--- 等待1秒
   Sleep(1000);
//---
  }