跳至内容

ObjectDelete

ObjectDelete

此函数用于删除指定名称的对象,该对象位于指定的图表中。该函数有两种变体:

bool  ObjectDelete(
   long     chart_id,     // chart ID
   string   object_name   // object name
   );

使用此函数可以删除指定名称的对象:

bool  ObjectDelete(
   string   object_name   // object name
   );

参数

chart_id

[in] 图表标识符。

object_name

[in] 要删除的对象的名称。

返回值

如果删除成功,则返回 true;否则返回 false。要了解更多关于错误的信息,请调用GetLastError()

注意

当未指定图表 ID 时,该函数应直接作用于当前图表。在这种情况下,返回值表示函数执行的结果。

如果指定了当前图表之外的图表 ID,则返回值仅表明命令是否已添加到该图表的队列中。在这种情况下,使用异步调用,这意味着函数不会等待已添加到其他图表队列的命令的执行,而是立即返回控制。

要检查在除当前图表之外的图表上命令执行的结果,可以使用检查指定对象属性的函数。但是,请注意,此类函数被添加到该图表的队列末尾并等待执行结果,因此可能会耗费时间。在处理图表上的大量对象时,应考虑到这一特性。

示例:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   long current_chart_id=ChartID();
//--- creates several objects of label type
   for(i=0; i<300; i+=10)
     {
      string obj_name="label_object"+IntegerToString(i);
      //--- creating label object (it does not have time/price coordinates)
      if(ObjectCreate(obj_name,OBJ_LABEL,0,0,0))
        {
         PrintFormat("Object %s created.",obj_name);
         //--- set random color
         ObjectSetInteger(current_chart_id,obj_name,OBJPROP_COLOR,MathRand());
         //--- set text property
         ObjectSetString(current_chart_id,obj_name,OBJPROP_TEXT,StringFormat("Simple Label at y= %d",i));
         //--- set distance property
         ObjectSet(obj_name,OBJPROP_XDISTANCE,i);
         ObjectSet(obj_name,OBJPROP_YDISTANCE,i);
         //-- forced chart redraw
         ChartRedraw(current_chart_id);
         Sleep(10);
        }
      else
        {
         Print("Error: can't create label! code #",GetLastError());
         return(0);
        }
     }
//--- sleep to see the objects created
   Sleep(3000);
//--- delete all objects
   int obj_total=ObjectsTotal();
   PrintFormat("Total %d objects",obj_total);
   for(i=obj_total-1;i>=0;i--)
     {
      string name=ObjectName(i);
      PrintFormat("object %d: %s",i,name);
      ObjectDelete(name);
     }
   return(0);
  }
最后更新于