跳至内容

TextOut

TextOut

此函数会在自定义数组(缓冲区)中显示文本,并返回该操作的结果。该数组用于创建图形资源.

bool  TextOut(
   const string        text,          // displayed text
   int                 x,             // X coordinate
   int                 y,             // Y coordinate
   uint                anchor,        // anchor type
   uint                &data[],       // output buffer
   uint                width,         // buffer width in pixels
   uint                height,        // buffer height in pixels
   uint                color,         // text color
   ENUM_COLOR_FORMAT   color_format   // color format for output
   );

参数

text

[in] 要显示在缓冲区中的文本。仅显示单行文本。

x

[in] 显示文本的锚点的X坐标。

y

[in] 显示文本的锚点的Y坐标。

anchor

[in] 显示文本锚点位置的9种预定义方法之一的值。该值由两个标志的组合确定——水平和垂直文本对齐的标志。标志名称在下面的备注中列出。

data[]

[in] 显示文本的缓冲区。该缓冲区用于创建图形资源.

width

[in] 缓冲区的宽度,以像素为单位。

height

[in] 缓冲区的高度,以像素为单位。

color

[in] 文本颜色。

color_format

[in] 颜色格式由ENUM_COLOR_FORMAT枚举值设置。

返回值

如果成功则返回true,否则返回false。

备注

由anchor指定的锚点是水平和垂直文本对齐的两个标志的组合。水平文本对齐的标志:

  • TA_LEFT – 锚点在边界框的左侧
  • TA_CENTER – 水平锚点位于边界框的中心
  • TA_RIGHT – 锚点在边界框的右侧

垂直文本对齐的标志:

  • TA_TOP – 锚点位于边界框的上部
  • TA_VCENTER – 垂直锚点位于边界框的中心
  • TA_BOTTOM – 锚点位于边界框的下部

标志的组合和指定的锚点示例见图。

9种绑定文本字符串的方法

示例:

#property strict
//--- width and height of the canvas (used for drawing)
#define IMG_WIDTH  200
#define IMG_HEIGHT 200
//--- display the parameter window before launching the script
#property script_show_inputs
//--- enable to set color format
input ENUM_COLOR_FORMAT clr_format=COLOR_FORMAT_XRGB_NOALPHA;
//--- drawing array (buffer)
uint ExtImg[IMG_WIDTH*IMG_HEIGHT];
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- create OBJ_BITMAP_LABEL object for drawing
   ObjectCreate(0,"CLOCK",OBJ_BITMAP_LABEL,0,0,0);
//--- specify the name of the graphical resource for writing in CLOCK object
   ObjectSetString(0,"CLOCK",OBJPROP_BMPFILE,"::IMG");

//--- auxiliary variables
   double a;            // arrow corner
   uint   nm=2700;      // minute corner
   uint   nh=2700*12;   // hour counter
   uint   w,h;          // variables for receiving text string sizes
   int    x,y;          // variables for calculation of the current coordinates of text string anchor points

//--- rotate clock hands in an infinite loop, till the script is stopped
   while(!IsStopped())
     {
      //--- clear the clock drawing buffer array
      ArrayFill(ExtImg,0,IMG_WIDTH*IMG_HEIGHT,0);
      //--- set the font for drawing digits for the clock-face
      TextSetFont("Arial",-200,FW_EXTRABOLD,0);
      //--- draw the clock-face
      for(int i=1;i<=12;i++)
        {
         //--- receive the size of the current hour on the clock-face
         TextGetSize(string(i),w,h);
         //--- calculate the coordinates of the current hour on the clock-face
         a=-((i*300)%3600*M_PI)/1800.0;
         x=IMG_WIDTH/2-int(sin(a)*80+0.5+w/2);
         y=IMG_HEIGHT/2-int(cos(a)*80+0.5+h/2);
         //--- output the hour on the clock-face to ExtImg[] buffer
         TextOut(string(i),x,y,TA_LEFT|TA_TOP,ExtImg,IMG_WIDTH,IMG_HEIGHT,0xFFFFFFFF,clr_format);
        }
      //--- now, specify the font for drawing the minute hand
      TextSetFont("Arial",-200,FW_EXTRABOLD,-int(nm%3600));
      //--- receive the size of the minute hand
      TextGetSize("----->",w,h);
      //--- calculate the coordinates of the minute hand on the clock-face
      a=-(nm%3600*M_PI)/1800.0;
      x=IMG_WIDTH/2-int(sin(a)*h/2+0.5);
      y=IMG_HEIGHT/2-int(cos(a)*h/2+0.5);
      //--- output of the minute hand to the clock-face in ExtImg[]buffer
      TextOut("----->",x,y,TA_LEFT|TA_TOP,ExtImg,IMG_WIDTH,IMG_HEIGHT,0xFFFFFFFF,clr_format);

      //--- now, set the font for drawing the minute hand
      TextSetFont("Arial",-200,FW_EXTRABOLD,-int(nh/12%3600));
      TextGetSize("==>",w,h);
      //--- calculate the coordinates of the hour hand on the clock-face
      a=-(nh/12%3600*M_PI)/1800.0;
      x=IMG_WIDTH/2-int(sin(a)*h/2+0.5);
      y=IMG_HEIGHT/2-int(cos(a)*h/2+0.5);
      //--- output of the hour hand on the clock-face to ExtImg[] buffer
      TextOut("==>",x,y,TA_LEFT|TA_TOP,ExtImg,IMG_WIDTH,IMG_HEIGHT,0xFFFFFFFF,clr_format);

      //--- update the graphical resource
      ResourceCreate("::IMG",ExtImg,IMG_WIDTH,IMG_HEIGHT,0,0,IMG_WIDTH,clr_format);
      //--- forced chart update
      ChartRedraw();

      //--- increase hour and minute counters
      nm+=60;
      nh+=60;
      //--- keeping a short pause between the frames
      Sleep(10);
     }
//--- delete CLOCK object when completing the script's operation
   ObjectDelete(0,"CLOCK");
//---
  }

另请参阅

资源, ResourceCreate(), ResourceSave(), TextGetSize(), TextSetFont()

最后更新于