OBJ_BITMAP

位图对象。

注意

对于位图对象,您可以选择图像的可视范围

示例

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

//--- 描述

#property description "Script creates a bitmap in the chart window."

//--- 启动脚本期间显示输入参数的窗口

#property script_show_inputs

//--- 脚本的输入参数

input string          InpFile="\\Images\\dollar.bmp"; // 位图文件名称

input int             InpWidth=24;                    // 可见范围 X 坐标

input int             InpHeight=24;                   // 可见范围 Y 坐标

input int             InpXOffset=4;                   // 可见范围沿着 X 轴移动

input int             InpYOffset=4;                   // 可见范围沿着 Y 轴移动

input color           InpColor=clrRed;                // 高亮显示时的边界颜色

input ENUM_LINE_STYLE InpStyle=STYLE_SOLID;           // 高亮显示时的线条风格

input int             InpPointWidth=1;                // 移动点大小

input bool            InpBack=false;                  // 背景对象

input bool            InpSelection=false;             // 突出移动

input bool            InpHidden=true;                 // 隐藏在对象列表

input long            InpZOrder=0;                    // 鼠标单击优先

//+------------------------------------------------------------------+

//| 在图表窗口创建位图                                                  |

//+------------------------------------------------------------------+

bool BitmapCreate(const long            chart_ID=0,        // 图表 ID

                  const string          name="Bitmap",     // 位图名称

                  const int             sub_window=0,      // 子窗口指数

                  datetime              time=0,            // 定位点时间

                  double                price=0,           // 定位点价格

                  const string          file="",           // 位图文件名称

                  const int             width=10,          // 可见范围 X 坐标

                  const int             height=10,         // 可见范围 Y 坐标

                  const int             x_offset=0,        // 可见范围沿着 X 轴移动

                  const int             y_offset=0,        // 可见范围沿着 Y 轴移动

                  const color           clr=clrRed,        // 高亮显示时的边界颜色

                  const ENUM_LINE_STYLE style=STYLE_SOLID, // 高亮显示时的线条风格

                  const int             point_width=1,     // 移动点大小

                  const bool            back=false,        // 在背景中

                  const bool            selection=false,   // 突出移动

                  const bool            hidden=true,       // 隐藏在对象列表

                  const long            z_order=0)         // 鼠标单击优先

  {

//--- 若未设置则设置定位点的坐标

   ChangeBitmapEmptyPoint(time,price);

//--- 重置错误的值

   ResetLastError();

//--- 创建位图

   if(!ObjectCreate(chart_ID,name,OBJ_BITMAP,sub_window,time,price))

     {

      Print(__FUNCTION__,

            ": failed to create a bitmap in the chart window! Error code = ",GetLastError());

      return(false);

     }

//--- 设置图像文件路径

   if(!ObjectSetString(chart_ID,name,OBJPROP_BMPFILE,file))

     {

      Print(__FUNCTION__,

            ": failed to download the image! Error code = ",GetLastError());

      return(false);

     }

//--- 设置图形的可见范围;如果有宽度或高度的值

//--- 分别超出源图像的宽度和高度,

//--- 则不绘制;相反,

//--- 只有对应这些值的部分才可绘制

   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);

   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);

//--- 设置将会显示在可见范围内的图像部分

//--- 默认显示图像的左上区域;该值允许

//--- 执行从该区域移动显示图像的另一个区域

   ObjectSetInteger(chart_ID,name,OBJPROP_XOFFSET,x_offset);

   ObjectSetInteger(chart_ID,name,OBJPROP_YOFFSET,y_offset);

//--- 设置对象高亮模式启动时的边界颜色

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- 设置对象高亮模式启动时的边界线条风格

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- 设置移动对象的定位点大小

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,point_width);

//--- 显示前景 (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 BitmapSetImage(const long   chart_ID=0,    // 图表 ID

                    const string name="Bitmap", // 位图名称

                    const string file="")       // 文件路径

  {

//--- 重置错误的值

   ResetLastError();

//--- 设置图像文件路径

   if(!ObjectSetString(chart_ID,name,OBJPROP_BMPFILE,file))

     {

      Print(__FUNCTION__,

            ": failed to download the image! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

//+------------------------------------------------------------------+

//| 在图表窗口移动位图                                                 |

//+------------------------------------------------------------------+

bool BitmapMove(const long   chart_ID=0,    // 图表 ID

                const string name="Bitmap", // 位图名称

                datetime     time=0,        // 定位点时间

                double       price=0)       // 定位点价格

  {

//--- 如果没有设置点的位置,则将其移动到当前的卖价柱

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- 重置错误的值

   ResetLastError();

//--- 移动定位点

   if(!ObjectMove(chart_ID,name,0,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

//+------------------------------------------------------------------+

//| 改变可见范围(位图)大小                                              |

//+------------------------------------------------------------------+

bool BitmapChangeSize(const long   chart_ID=0,    // 图表 ID

                      const string name="Bitmap", // 位图名称

                      const int    width=0,       // 位图宽度

                      const int    height=0)      // 位图高度

  {

//--- 重置错误的值

   ResetLastError();

//--- 改变位图大小

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width))

     {

      Print(__FUNCTION__,

            ": failed to change the bitmap width! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height))

     {

      Print(__FUNCTION__,

            ": failed to change the bitmap height! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

//+--------------------------------------------------------------------+

//| 改变可见范围左上角的坐标                                               |

//+--------------------------------------------------------------------+

bool BitmapMoveVisibleArea(const long   chart_ID=0,    // 图表 ID

                           const string name="Bitmap", // 位图名称

                           const int    x_offset=0,    // 可见范围 X 坐标

                           const int    y_offset=0)    // 可见范围 Y坐标<

  {

//--- 重置错误的值

   ResetLastError();

//--- 改变位图可见范围的坐标

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XOFFSET,x_offset))

     {

      Print(__FUNCTION__,

            ": failed to change X coordinate of the visibility scope! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YOFFSET,y_offset))

     {

      Print(__FUNCTION__,

            ": failed to change Y coordinate of the visibility scope! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

//+------------------------------------------------------------------+

//| 删除位图                                                          |

//+------------------------------------------------------------------+

bool BitmapDelete(const long   chart_ID=0,    // 图表 ID

                  const string name="Bitmap") // 位图名称

  {

//--- 重置错误的值

   ResetLastError();

//--- 删除标签

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete a bitmap! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

//+------------------------------------------------------------------+

//| 检查定位点的值和为空点设置                                           |

//| 默认的值                                                          |

//+------------------------------------------------------------------+

void ChangeBitmapEmptyPoint(datetime &time,double &price)

  {

//--- 如果点的时间没有设置,它将位于当前柱

   if(!time)

      time=TimeCurrent();

//--- 如果点的价格没有设置,则它将用卖价值

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

  }

//+------------------------------------------------------------------+

//| 脚本程序起始函数                                                   |

//+------------------------------------------------------------------+

void OnStart()

  {

   datetime date[];  // 存储可见柱日期的数组

   double   close[]; // 存储收盘价的数组

//--- 位图文件名称

   string   file="\\Images\\dollar.bmp";

//--- 图表窗口的可见柱的数量

   int bars=(int)ChartGetInteger(0,CHART_VISIBLE_BARS);

//--- 内存分配

   ArrayResize(date,bars);

   ArrayResize(close,bars);

//--- 填写日期数组

   ResetLastError();

   if(CopyTime(Symbol(),Period(),0,bars,date)==-1)

     {

      Print("Failed to copy time values! Error code = ",GetLastError());

      return;

     }

//--- 填写收盘价的数组

   if(CopyClose(Symbol(),Period(),0,bars,close)==-1)

     {

      Print("Failed to copy the values of Close prices! Error code = ",GetLastError());

      return;

     }

//--- 定义图像显示的频率

   int scale=(int)ChartGetInteger(0,CHART_SCALE);

//--- 定义步骤

   int step=1;

   switch(scale)

     {

      case 0:

         step=27;

         break;

      case 1:

         step=14;

         break;

      case 2:

         step=7;

         break;

      case 3:

         step=4;

         break;

      case 4:

         step=2;

         break;

     }

//--- 创建最高和最低柱的值的位图(间隔)。

   for(int i=0;i<bars;i+=step)

     {

      //--- 创建位图

      if(!BitmapCreate(0,"Bitmap_"+(string)i,0,date[i],close[i],InpFile,InpWidth,InpHeight,InpXOffset,

         InpYOffset,InpColor,InpStyle,InpPointWidth,InpBack,InpSelection,InpHidden,InpZOrder))

        {

         return;

        }

      //--- 检查脚本操作是否已经强制禁用

      if(IsStopped())

         return;

      //--- 重画图表

      ChartRedraw();

      // 0.05 秒延迟

      Sleep(50);

     }

//--- 半秒延迟

   Sleep(500);

//--- 删除卖出符号

   for(int i=0;i<bars;i+=step)

     {

      if(!BitmapDelete(0,"Bitmap_"+(string)i))

         return;

      if(!BitmapDelete(0,"Bitmap_"+(string)i))

         return;

      //--- 重画图表

      ChartRedraw();

      // 0.05 秒延迟

      Sleep(50);

     }

//---

  }