跳至内容

EventChartCustom

EventChartCustom

此函数为指定的图表生成自定义事件。

bool  EventChartCustom(
   long    chart_id,            // identifier of the event receiving chart
   ushort  custom_event_id,     // event identifier
   long    lparam,              // parameter of type long
   double  dparam,              // parameter of type double
   string  sparam               // string parameter of the event
   );

参数

chart_id

[in] 图表标识符。0表示当前图表。

custom_event_id

[in] 用户事件的ID。此标识符会自动添加到CHARTEVENT_CUSTOM值中并转换为整数类型。

lparam

[in] 传递给OnChartEvent函数的长类型事件参数。

dparam

[in] 传递给OnChartEvent函数的双精度类型事件参数。

sparam

[in] 传递给OnChartEvent函数的字符串类型事件参数。如果字符串长度超过63个字符,则会被截断。

返回值

如果自定义事件已成功放入接收事件的图表的事件队列中,则返回true。如果失败,则返回false。使用GetLastError()获取错误代码。

注意

附加到指定图表的专家顾问或指标使用函数OnChartEvent(int event_id, long& lparam, double& dparam, string& sparam)处理事件。

对于每种类型的事件,OnChartEvent()函数的输入参数都有明确的值,这些值是处理该事件所必需的。通过这些参数传递的事件和值如下表所示。

事件id参数的值lparam参数的值dparam参数的值sparam参数的值
按键事件CHARTEVENT_KEYDOWN按键代码重复次数(用户按住键时按键重复的次数)描述键盘按钮状态的位掩码的字符串值
鼠标事件(如果图表的属性CHART_EVENT_MOUSE_MOVE=true已设置)CHARTEVENT_MOUSE_MOVEX坐标Y坐标描述鼠标按钮状态的位掩码的字符串值
图形对象创建事件(如果图表的CHART_EVENT_OBJECT_CREATE=true已设置)CHARTEVENT_OBJECT_CREATE创建的图形对象的名称
通过属性对话框更改对象属性的事件CHARTEVENT_OBJECT_CHANGE修改过的图形对象的名称
图形对象删除事件(如果图表的CHART_EVENT_OBJECT_DELETE=true已设置)CHARTEVENT_OBJECT_DELETE被删除的图形对象的名称
图表上鼠标点击事件CHARTEVENT_CLICKX坐标Y坐标
属于图表的图形对象上的鼠标点击事件CHARTEVENT_OBJECT_CLICKX坐标Y坐标发生事件的图形对象的名称
使用鼠标拖动图形对象的事件CHARTEVENT_OBJECT_DRAG被移动的图形对象的名称
LabelEdit图形对象的输入框中文本编辑完成的事件CHARTEVENT_OBJECT_ENDEDIT文本编辑完成的LabelEdit图形对象的名称
图表上的更改事件CHARTEVENT_CHART_CHANGE
用户事件的ID,编号为NCHARTEVENT_CUSTOM+NEventChartCustom()函数设置的值EventChartCustom()函数设置的值EventChartCustom()函数设置的值

示例:

//+------------------------------------------------------------------+
//|                                            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;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Create a button to send custom events
   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);

//--- Create a label for displaying information
   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);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectDelete(0,buttonID);
   ObjectDelete(0,labelID);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//--- Check the event by pressing a mouse button
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      string clickedChartObject=sparam;
      //--- If you click on the object with the name buttonID
      if(clickedChartObject==buttonID)
        {
         //--- State of the button - pressed or not
         bool selected=ObjectGetInteger(0,buttonID,OBJPROP_STATE);
         //--- log a debug message
         Print("Button pressed = ",selected);
         int customEventID; // Number of the custom event to send
         string message;    // Message to be sent in the event
         //--- If the button is pressed
         if(selected)
           {
            message="Button pressed";
            customEventID=CHARTEVENT_CUSTOM+1;
           }
         else // Button is not pressed
           {
            message="Button in not pressed";
            customEventID=CHARTEVENT_CUSTOM+999;
           }
         //--- Send a custom event "our" chart
         EventChartCustom(0,customEventID-CHARTEVENT_CUSTOM,0,0,message);
         ///--- Send a message to all open charts
         BroadcastEvent(ChartID(),0,"Broadcast Message");
         //--- Debug message
         Print("Sent an event with ID = ",customEventID);
        }
      ChartRedraw();// Forced redraw all chart objects
     }

//--- Check the event belongs to the user events
   if(id>CHARTEVENT_CUSTOM)
     {
      if(id==broadcastEventID)
        {
         Print("Got broadcast message from a chart with id = "+lparam);
        }
      else
        {
         //--- We read a text message in the event
         string info=sparam;
         Print("Handle the user event with the ID = ",id);
         //--- Display a message in a label
         ObjectSetString(0,labelID,OBJPROP_TEXT,sparam);
         ChartRedraw();// Forced redraw all chart objects
        }
     }
  }
//+------------------------------------------------------------------+
//| sends broadcast event to all open charts                         |
//+------------------------------------------------------------------+
void BroadcastEvent(long lparam,double dparam,string sparam)
  {
   int eventID=broadcastEventID-CHARTEVENT_CUSTOM;
   long currChart=ChartFirst();
   int i=0;
   while(i<CHARTS_MAX)                 // We have certainly no more than CHARTS_MAX open charts
     {
      EventChartCustom(currChart,eventID,lparam,dparam,sparam);
      currChart=ChartNext(currChart); // We have received a new chart from the previous
      if(currChart==-1) break;        // Reached the end of the charts list
      i++;// Do not forget to increase the counter
     }
  }
//+------------------------------------------------------------------+

另请参阅

客户端终端的事件事件处理函数

最后更新于