输入字段特性:文本对齐和只读

对于类型为 OBJ_EDIT(输入字段)的对象,MQL 程序可以使用 ObjectSetInteger/ObjectGetInteger函数设置两个特定特性。

标识符

说明

值类型

OBJPROP_ALIGN

水平文本对齐

ENUM_ALIGN_MODE

OBJPROP_READONLY

文本编辑能力

bool

ENUM_ALIGN_MODE 枚举包含以下成员。

标识符

说明

ALIGN_LEFT

左对齐

ALIGN_CENTER

居中对齐

ALIGN_RIGHT

右对齐

请注意,与 OBJ_TEXT 和 OBJ_LABEL 对象不同,输入字段不会自动调整自身大小以适应输入的文本,因此,对于长字符串,你可能需要显式设置 OBJPROP_XSIZE 特性。

在编辑模式下,输入字段内支持文本的水平滚动。

ObjectEdit.mq5脚本会创建四个 OBJ_EDIT 对象:其中三个可编辑且采用不同的文本对齐方式,第四个处于只读模式。

#include "ObjectPrefix.mqh"
   
void SetupEdit(const int x, const int y, const int dx, const int dy,
   const ENUM_ALIGN_MODE alignment = ALIGN_LEFT, const bool readonly = false)
{
   // create an object with a description of the properties
   const string props = EnumToString(alignment)
      + (readonly ? " read-only" : " editable");
   const string name = ObjNamePrefix + "Edit" + props;
   ObjectCreate(0, name, OBJ_EDIT, 0, 0, 0);
   // position and size
   ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
   ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
   ObjectSetInteger(0, name, OBJPROP_XSIZE, dx);
   ObjectSetInteger(0, name, OBJPROP_YSIZE, dy);
   // specific properties of input fields
   ObjectSetInteger(0, name, OBJPROP_ALIGN, alignment);
   ObjectSetInteger(0, name, OBJPROP_READONLY, readonly);
   // colors (different depending on editability)
   ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clrWhite);
   ObjectSetInteger(0, name, OBJPROP_COLOR, readonly ? clrRed : clrBlue);
   // content
   ObjectSetString(0, name, OBJPROP_TEXT, props);
   // tooltip for editable
   ObjectSetString(0, name, OBJPROP_TOOLTIP,
      (readonly ? "\n" : "Click me to edit"));
}
   
void OnStart()
{
   SetupEdit(100, 100, 200, 20);
   SetupEdit(100, 120, 200, 20, ALIGN_RIGHT);
   SetupEdit(100, 140, 200, 20, ALIGN_CENTER);
   SetupEdit(100, 160, 200, 20, ALIGN_CENTER, true);
}

脚本的运行结果如下图所示。

不同模式下的输入字段

不同模式下的输入字段

可以点击任何可编辑字段并更改其内容。