//--- 描述
#property description "Script creates \"Rectangle Label\" graphical object."
//--- 启动脚本期间显示输入参数的窗口
#property script_show_inputs
//--- 脚本的输入参数
input string InpName="RectLabel"; // 标签名称
input color InpBackColor=clrSkyBlue; // 背景色
input ENUM_BORDER_TYPE InpBorder=BORDER_FLAT; // 边框类型
input ENUM_BASE_CORNER InpCorner=CORNER_LEFT_UPPER; // 图表定位角
input color InpColor=clrDarkBlue; // 平面边框颜色 (Flat)
input ENUM_LINE_STYLE InpStyle=STYLE_SOLID; // 平面边框风格 (Flat)
input int InpLineWidth=3; // 平面边框宽度(Flat)
input bool InpBack=false; // 背景对象
input bool InpSelection=true; // 突出移动
input bool InpHidden=true; // 隐藏在对象列表
input long InpZOrder=0; // 鼠标单击优先
//+------------------------------------------------------------------+
//| 创建矩形标签 |
//+------------------------------------------------------------------+
bool RectLabelCreate(const long chart_ID=0, // 图表 ID
const string name="RectLabel", // 标签名称
const int sub_window=0, // 子窗口指数
const int x=0, // X 坐标
const int y=0, // Y 坐标
const int width=50, // 宽度
const int height=18, // 高度
const color back_clr=C'236,233,216', // 背景色
const ENUM_BORDER_TYPE border=BORDER_SUNKEN, // 边框类型
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // 图表定位角
const color clr=clrRed, // 平面边框颜色 (Flat)
const ENUM_LINE_STYLE style=STYLE_SOLID, // 平面边框风格
const int line_width=1, // 平面边框宽度
const bool back=false, // 在背景中
const bool selection=false, // 突出移动
const bool hidden=true, // 隐藏在对象列表
const long z_order=0) // 鼠标单击优先
{
//--- 重置错误的值
ResetLastError();
//--- 创建矩形标签
if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE_LABEL,sub_window,0,0))
{
Print(__FUNCTION__,
": failed to create a rectangle label! 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);
//--- 设置背景颜色
ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
//--- 设置边框类型
ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_TYPE,border);
//--- 设置相对于定义点坐标的图表的角
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- 设置平面边框颜色 (在平面模式下)
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- 设置平面边框线型风格
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- 设置平面边框宽度
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,line_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 RectLabelMove(const long chart_ID=0, // 图表 ID
const string name="RectLabel", // 标签名称
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 label! Error code = ",GetLastError());
return(false);
}
if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))
{
Print(__FUNCTION__,
": failed to move Y coordinate of the label! Error code = ",GetLastError());
return(false);
}
//--- 成功执行
return(true);
}
//+------------------------------------------------------------------+
//| 改变矩形标签的大小 |
//+------------------------------------------------------------------+
bool RectLabelChangeSize(const long chart_ID=0, // 图表 ID
const string name="RectLabel", // 标签名称
const int width=50, // 标签宽度
const int height=18) // 标签高度
{
//--- 重置错误的值
ResetLastError();
//--- 改变标签大小
if(!ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width))
{
Print(__FUNCTION__,
": failed to change the label's width! Error code = ",GetLastError());
return(false);
}
if(!ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height))
{
Print(__FUNCTION__,
": failed to change the label's height! Error code = ",GetLastError());
return(false);
}
//--- 成功执行
return(true);
}
//+------------------------------------------------------------------+
//| 改变矩形标签边框类型 |
//+------------------------------------------------------------------+
bool RectLabelChangeBorderType(const long chart_ID=0, // 图表 ID
const string name="RectLabel", // 标签名称
const ENUM_BORDER_TYPE border=BORDER_SUNKEN) // 边框类型
{
//--- 重置错误的值
ResetLastError();
//--- 改变边框类型
if(!ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_TYPE,border))
{
Print(__FUNCTION__,
": failed to change the border type! Error code = ",GetLastError());
return(false);
}
//--- 成功执行
return(true);
}
//+------------------------------------------------------------------+
//| 删除矩形标签 |
//+------------------------------------------------------------------+
bool RectLabelDelete(const long chart_ID=0, // 图表 ID
const string name="RectLabel") // 标签名称
{
//--- 重置错误的值
ResetLastError();
//--- 删除标签
if(!ObjectDelete(chart_ID,name))
{
Print(__FUNCTION__,
": failed to delete a rectangle label! 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=(int)x_distance/4;
int y=(int)y_distance/4;
//--- 设置标签大小
int width=(int)x_distance/4;
int height=(int)y_distance/4;
//--- 创建矩形标签
if(!RectLabelCreate(0,InpName,0,x,y,width,height,InpBackColor,InpBorder,InpCorner,
InpColor,InpStyle,InpLineWidth,InpBack,InpSelection,InpHidden,InpZOrder))
{
return;
}
//--- 重绘图表并等待1秒
ChartRedraw();
Sleep(1000);
//--- 改变矩形标签的大小
int steps=(int)MathMin(x_distance/4,y_distance/4);
for(int i=0;i<steps;i++)
{
//--- resize
width+=1;
height+=1;
if(!RectLabelChangeSize(0,InpName,width,height))
return;
//--- 检查脚本操作是否已经强制禁用
if(IsStopped())
return;
//--- 重绘图表并等待0.01秒
ChartRedraw();
Sleep(10);
}
//--- 1 秒延迟
Sleep(1000);
//--- 改变边框类型
if(!RectLabelChangeBorderType(0,InpName,BORDER_RAISED))
return;
//--- 重画图表并等待1秒
ChartRedraw();
Sleep(1000);
//--- 改变边框类型
if(!RectLabelChangeBorderType(0,InpName,BORDER_SUNKEN))
return;
//--- 重画图表并等待1秒
ChartRedraw();
Sleep(1000);
//--- 删除标签
RectLabelDelete(0,InpName);
ChartRedraw();
//--- 等待1秒
Sleep(1000);
//---
}
|