这篇文章是使用mql5创建你自己的图形化操作面板1[译文]的第二篇,预计一共4篇,敬请期待!
创建一个图形面板让我们先理解这个概念,图形面板是一个我们可以用来显示自定义的内容但不包括画线的缓冲区的单独的窗口。这个面板使用图表对象(chart objects)嵌入在终端中(MT5)简单的显示了需要的信息。这些信息可以以以下这些形式展现: 为了创建这个面板,我们需要2个文件: 类的描述现在,我们已经创建了工作文件夹,让我们现在在菜单中找到“导航”窗口并单击鼠标右键,在弹出的菜单中选择“New File”(创建新文件),然后在选择“New Class”(新建类),这样就会弹出一个mql5向导窗口,单击“下一步”。按下面的图片中的现实填写内容。 单击“完成”。结果就是我们得到了下面的代码: //+------------------------------------------------------------------+ //| PanelDialog.mqh | //| Copyright 2011, MetaQuotes Software Corp. | //| http://www. | //+------------------------------------------------------------------+ #property copyright "Copyright 2011, MetaQuotes Software Corp." #property link "http://www." #property version "1.00" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CPanelDialog : public CAppDialog { private: public: CPanelDialog(); ~CPanelDialog(); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CPanelDialog::CPanelDialog() { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CPanelDialog::~CPanelDialog() { } //+------------------------------------------------------------------+ 在标准库里加入包含文件<Controls\Dialog.mqh>,里面有基类:CAppDialog类的描述和注释。 //+------------------------------------------------------------------+ //| PanelDialog.mqh | //| Copyright 2011, MetaQuotes Software Corp. | //| http://www. | //+------------------------------------------------------------------+ #include <Controls\Dialog.mqh> //+------------------------------------------------------------------+ //| CPanelDialog class | //| Function: main application dialog | //+------------------------------------------------------------------+ class CPanelDialog : public CAppDialog { private: public: CPanelDialog(void); ~CPanelDialog(void); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CPanelDialog::CPanelDialog(void) { } //+------------------------------------------------------------------+ //| Destructor | //+------------------------------------------------------------------+ CPanelDialog::~CPanelDialog(void) { } //+------------------------------------------------------------------+ 现在我们拥有一个类的描述允许我们在自定义指标中使用对话框。我们的对话框现在是空的,但接下来我们将在里面加上一些控件。现在,让我们开始这个指标吧。 这个自定义指标的源代码我们依然用MQL5向导来创建这个自定义指标。和我们之前写类的描述时必须填写的类似。只有一个比较麻烦的地方:我们在MQL5向导中选择“自定义指标”,为了创建这个指标,我们需要填写3个对话框。 在第二个对话框中,勾选“OnChartEvent"(必需)和“OnTimer”,如下: 在第三个对话框中勾选“单独的窗口指标”(必需) 然后单击“完成”。生成的代码如下: //+------------------------------------------------------------------+ //| PanelIndicator.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| http://www. | //+------------------------------------------------------------------+ #property copyright "Copyright 2011, MetaQuotes Software Corp." #property link "http://www." #property version "1.00" #property indicator_separate_window //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping //--- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[]) { //--- //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { //--- } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- } //+------------------------------------------------------------------+ 加入我们的模板后代码中包含以下内容: Missing(?)指标属性的描述 我们将得到可以直接使用的指标代码: //+------------------------------------------------------------------+ //| PanelIndicator.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| http://www. | //+------------------------------------------------------------------+ #property copyright "Copyright 2011, MetaQuotes Software Corp." #property link "http://www." #property version "1.00" #property indicator_separate_window #property indicator_plots 0 #property indicator_buffers 0 #property indicator_minimum 0.0 #property indicator_maximum 0.0 //+------------------------------------------------------------------+ //| Include files | //+------------------------------------------------------------------+ #include "PanelDialog.mqh" //+------------------------------------------------------------------+ //| Global variables | //+------------------------------------------------------------------+ CPanelDialog ExtDialog; //+------------------------------------------------------------------+ //| Initialization | //+------------------------------------------------------------------+ int OnInit() { //--- creating the application dialog if(!ExtDialog.Create(0,"Panel Indicator",0,0,0,0,130)) return(-1); //--- starting the application if(!ExtDialog.Run()) return(-2); //--- creating the timer EventSetTimer(1); //--- success return(0); } //+------------------------------------------------------------------+ //| Deinitialization | //+------------------------------------------------------------------+ int OnDeinit() { //--- destroying the dialog ExtDialog.Destroy(); //--- killing the timer EventKillTimer(); } //+------------------------------------------------------------------+ //| Iteration | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[]) { //--- returning the prev_calculated value for the next call return(rates_total); } //+------------------------------------------------------------------+ //| Timer event handler | //+------------------------------------------------------------------+ void OnTimer() { //--- } //+------------------------------------------------------------------+ //| Chart event handler | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- handling the event ExtDialog.ChartEvent(id,lparam,dparam,sparam); } //+------------------------------------------------------------------+ 目前以上的代码不会显示任何内容。我们编译后并在客户端的导航中使用它,在图表中会出现一个新的窗口,里面有一个空的对话框。 |
|
来自: visionary555 > 《证券》