//+------------------------------------------------------------------+
//| ButtonClickExpert.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
string buttonID="Button";
string labelID="Info";
int broadcastEventID=5000;
//+------------------------------------------------------------------+
//| 专家初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 创建按钮发送自定义事件
ObjectCreate(0,buttonID,OBJ_BUTTON,0,100,100);
ObjectSetInteger(0,buttonID,OBJPROP_COLOR,clrWhite);
ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,clrGray);
ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,100);
ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,100);
ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,200);
ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,50);
ObjectSetString(0,buttonID,OBJPROP_FONT,"Arial");
ObjectSetString(0,buttonID,OBJPROP_TEXT,"Button");
ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,10);
ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
//--- 创建标签展示信息
ObjectCreate(0,labelID,OBJ_LABEL,0,100,100);
ObjectSetInteger(0,labelID,OBJPROP_COLOR,clrRed);
ObjectSetInteger(0,labelID,OBJPROP_XDISTANCE,100);
ObjectSetInteger(0,labelID,OBJPROP_YDISTANCE,50);
ObjectSetString(0,labelID,OBJPROP_FONT,"Trebuchet MS");
ObjectSetString(0,labelID,OBJPROP_TEXT,"No information");
ObjectSetInteger(0,labelID,OBJPROP_FONTSIZE,20);
ObjectSetInteger(0,labelID,OBJPROP_SELECTABLE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 专家无法初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ObjectDelete(0,buttonID);
ObjectDelete(0,labelID);
}
//+------------------------------------------------------------------+
//| 专家订单号函数 |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//--- 通过按下鼠标键检测事件
if(id==CHARTEVENT_OBJECT_CLICK)
{
string clickedChartObject=sparam;
//--- 如果点击带有按键ID名的物件
if(clickedChartObject==buttonID)
{
//--- 按键状态-按或者不按
bool selected=ObjectGetInteger(0,buttonID,OBJPROP_STATE);
//--- 调试信息日志
Print("Button pressed = ",selected);
int customEventID; // 发送的自定义事件数
string message; // 事件中被发送的信息
//--- 如果按下按键
if(selected)
{
message="Button pressed";
customEventID=CHARTEVENT_CUSTOM+1;
}
else // 不按按键
{
message="Button in not pressed";
customEventID=CHARTEVENT_CUSTOM+999;
}
//--- 发送自定义事件“our”图表
EventChartCustom(0,customEventID-CHARTEVENT_CUSTOM,0,0,message);
///--- 发送信息到所有打开的图表
BroadcastEvent(ChartID(),0,"Broadcast Message");
//--- 调试信息
Print("Sent an event with ID = ",customEventID);
}
ChartRedraw();// 强制重画所有图表物件
}
//--- 检测属于用户事件的事件
if(id>CHARTEVENT_CUSTOM)
{
if(id==broadcastEventID)
{
Print("Got broadcast message from a chart with id = "+lparam);
}
else
{
//--- 阅读事件中的文本信息
string info=sparam;
Print("Handle the user event with the ID = ",id);
//--- 标签中展示信息
ObjectSetString(0,labelID,OBJPROP_TEXT,sparam);
ChartRedraw();// 强制重画所有图表物件
}
}
}
//+------------------------------------------------------------------+
//| 发送广播事件到所有打开的图表 |
//+------------------------------------------------------------------+
void BroadcastEvent(long lparam,double dparam,string sparam)
{
int eventID=broadcastEventID-CHARTEVENT_CUSTOM;
long currChart=ChartFirst();
int i=0;
while(i<CHARTS_MAX) // 打开图表不能多于CHARTS_MAX
{
EventChartCustom(currChart,eventID,lparam,dparam,sparam);
currChart=ChartNext(currChart); // 从上一个接收新图表
if(currChart==-1) break; // 达到图表列表末端
i++; // 不要忘记增加计数器
}
}
//+------------------------------------------------------------------+
|