見出し画像

(MT4)MQLボタンの表示用コード

MT4にてボタン操作を作成することが増えたので備忘録兼コピペ用メモです。

ボタンの数だけコードも増えるので、ある程度レイアウトが固まったコードがあったほうが便利なので、MQL4で使用可能なボタン表示のコードについて残しておきます。

コピペ用のMQLコードは下の方に貼っておきます。
自分がコピペで使いまわせるよう備忘録的なものですが、他の方も使いたい方いたらどうぞって感じです。

ボタンのイメージは画像の通りです。
コードの解読時の参考にしてください。

👆クリックでボタンの色が反転する

クリックすると色が反転するようにしています。希望の色や表示したい文字があれば該当箇所を変えることで反映可能です。ボタンをクリックした際の動きについては各自内容は作成してください。

//+------------------------------------------------------------------+
//|                                                     btn_indi.mq4 |
//|                                          Copyright 2024, junico. |
//|               https://coconala.com/mypage/direct_message/6287479 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, junico."
#property link      "https://note.com/junico02_se_blog/n/nf9d452e999ed"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   ObjectCreate(0,"BTN_1",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"BTN_1",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"BTN_1",OBJPROP_BACK,false);
   ObjectSetInteger(0,"BTN_1",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,"BTN_1",OBJPROP_SELECTED,false);
   ObjectSetInteger(0,"BTN_1",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"BTN_1",OBJPROP_ZORDER,0);
   ObjectSetString(0,"BTN_1",OBJPROP_TEXT,"BTN_1");
   ObjectSetString(0,"BTN_1",OBJPROP_FONT,"Calibri");
   ObjectSetInteger(0,"BTN_1",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"BTN_1",OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,"BTN_1",OBJPROP_XDISTANCE,20);
   ObjectSetInteger(0,"BTN_1",OBJPROP_YDISTANCE,20);
   ObjectSetInteger(0,"BTN_1",OBJPROP_XSIZE,60);
   ObjectSetInteger(0,"BTN_1",OBJPROP_YSIZE,16);
   ObjectSetInteger(0,"BTN_1",OBJPROP_BGCOLOR,clrYellow);
   ObjectSetInteger(0,"BTN_1",OBJPROP_BORDER_COLOR,clrGold);
   ObjectSetInteger(0,"BTN_1",OBJPROP_STATE,false);

   ObjectCreate(0,"BTN_2",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"BTN_2",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"BTN_2",OBJPROP_BACK,false);
   ObjectSetInteger(0,"BTN_2",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,"BTN_2",OBJPROP_SELECTED,false);
   ObjectSetInteger(0,"BTN_2",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"BTN_2",OBJPROP_ZORDER,0);
   ObjectSetString(0,"BTN_2",OBJPROP_TEXT,"BTN_2");
   ObjectSetString(0,"BTN_2",OBJPROP_FONT,"Calibri");
   ObjectSetInteger(0,"BTN_2",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"BTN_2",OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,"BTN_2",OBJPROP_XDISTANCE,85);
   ObjectSetInteger(0,"BTN_2",OBJPROP_YDISTANCE,20);
   ObjectSetInteger(0,"BTN_2",OBJPROP_XSIZE,60);
   ObjectSetInteger(0,"BTN_2",OBJPROP_YSIZE,16);
   ObjectSetInteger(0,"BTN_2",OBJPROP_BGCOLOR,clrYellow);
   ObjectSetInteger(0,"BTN_2",OBJPROP_BORDER_COLOR,clrGold);
   ObjectSetInteger(0,"BTN_2",OBJPROP_STATE,false);

   ObjectCreate(0,"BTN_3",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"BTN_3",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"BTN_3",OBJPROP_BACK,false);
   ObjectSetInteger(0,"BTN_3",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,"BTN_3",OBJPROP_SELECTED,false);
   ObjectSetInteger(0,"BTN_3",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"BTN_3",OBJPROP_ZORDER,0);
   ObjectSetString(0,"BTN_3",OBJPROP_TEXT,"BTN_3");
   ObjectSetString(0,"BTN_3",OBJPROP_FONT,"Calibri");
   ObjectSetInteger(0,"BTN_3",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"BTN_3",OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,"BTN_3",OBJPROP_XDISTANCE,150);
   ObjectSetInteger(0,"BTN_3",OBJPROP_YDISTANCE,20);
   ObjectSetInteger(0,"BTN_3",OBJPROP_XSIZE,60);
   ObjectSetInteger(0,"BTN_3",OBJPROP_YSIZE,16);
   ObjectSetInteger(0,"BTN_3",OBJPROP_BGCOLOR,clrYellow);
   ObjectSetInteger(0,"BTN_3",OBJPROP_BORDER_COLOR,clrGold);
   ObjectSetInteger(0,"BTN_3",OBJPROP_STATE,false);

   ObjectCreate(0,"BTN_4",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"BTN_4",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"BTN_4",OBJPROP_BACK,false);
   ObjectSetInteger(0,"BTN_4",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,"BTN_4",OBJPROP_SELECTED,false);
   ObjectSetInteger(0,"BTN_4",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"BTN_4",OBJPROP_ZORDER,0);
   ObjectSetString(0,"BTN_4",OBJPROP_TEXT,"BTN_4");
   ObjectSetString(0,"BTN_4",OBJPROP_FONT,"Calibri");
   ObjectSetInteger(0,"BTN_4",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"BTN_4",OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,"BTN_4",OBJPROP_XDISTANCE,20);
   ObjectSetInteger(0,"BTN_4",OBJPROP_YDISTANCE,41);
   ObjectSetInteger(0,"BTN_4",OBJPROP_XSIZE,60);
   ObjectSetInteger(0,"BTN_4",OBJPROP_YSIZE,16);
   ObjectSetInteger(0,"BTN_4",OBJPROP_BGCOLOR,clrYellow);
   ObjectSetInteger(0,"BTN_4",OBJPROP_BORDER_COLOR,clrGold);
   ObjectSetInteger(0,"BTN_4",OBJPROP_STATE,false);

   ObjectCreate(0,"BTN_5",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"BTN_5",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"BTN_5",OBJPROP_BACK,false);
   ObjectSetInteger(0,"BTN_5",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,"BTN_5",OBJPROP_SELECTED,false);
   ObjectSetInteger(0,"BTN_5",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"BTN_5",OBJPROP_ZORDER,0);
   ObjectSetString(0,"BTN_5",OBJPROP_TEXT,"BTN_5");
   ObjectSetString(0,"BTN_5",OBJPROP_FONT,"Calibri");
   ObjectSetInteger(0,"BTN_5",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"BTN_5",OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,"BTN_5",OBJPROP_XDISTANCE,85);
   ObjectSetInteger(0,"BTN_5",OBJPROP_YDISTANCE,41);
   ObjectSetInteger(0,"BTN_5",OBJPROP_XSIZE,60);
   ObjectSetInteger(0,"BTN_5",OBJPROP_YSIZE,16);
   ObjectSetInteger(0,"BTN_5",OBJPROP_BGCOLOR,clrYellow);
   ObjectSetInteger(0,"BTN_5",OBJPROP_BORDER_COLOR,clrGold);
   ObjectSetInteger(0,"BTN_5",OBJPROP_STATE,false);

   ObjectCreate(0,"BTN_6",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"BTN_6",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,"BTN_6",OBJPROP_BACK,false);
   ObjectSetInteger(0,"BTN_6",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,"BTN_6",OBJPROP_SELECTED,false);
   ObjectSetInteger(0,"BTN_6",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"BTN_6",OBJPROP_ZORDER,0);
   ObjectSetString(0,"BTN_6",OBJPROP_TEXT,"BTN_6");
   ObjectSetString(0,"BTN_6",OBJPROP_FONT,"Calibri");
   ObjectSetInteger(0,"BTN_6",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"BTN_6",OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,"BTN_6",OBJPROP_XDISTANCE,150);
   ObjectSetInteger(0,"BTN_6",OBJPROP_YDISTANCE,41);
   ObjectSetInteger(0,"BTN_6",OBJPROP_XSIZE,60);
   ObjectSetInteger(0,"BTN_6",OBJPROP_YSIZE,16);
   ObjectSetInteger(0,"BTN_6",OBJPROP_BGCOLOR,clrYellow);
   ObjectSetInteger(0,"BTN_6",OBJPROP_BORDER_COLOR,clrGold);
   ObjectSetInteger(0,"BTN_6",OBJPROP_STATE,false);
//---
   return(INIT_SUCCEEDED);
  }

int deinit()
  {
   ObjectsDeleteAll(0,"BTN_");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {

   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam == "BTN_1" && ObjectGetInteger(0,"BTN_1",OBJPROP_BGCOLOR) == clrYellow)
        {
         ObjectSetInteger(0,"BTN_1",OBJPROP_COLOR,clrGold);
         ObjectSetInteger(0,"BTN_1",OBJPROP_BGCOLOR,clrBlack);
         ObjectSetInteger(0,"BTN_1",OBJPROP_STATE,false);
        }
      else
         if(sparam == "BTN_1" && ObjectGetInteger(0,"BTN_1",OBJPROP_BGCOLOR) == clrBlack)
           {
            ObjectSetInteger(0,"BTN_1",OBJPROP_COLOR,clrBlack);
            ObjectSetInteger(0,"BTN_1",OBJPROP_BGCOLOR,clrYellow);
            ObjectSetInteger(0,"BTN_1",OBJPROP_STATE,false);
           }
      if(sparam == "BTN_2" && ObjectGetInteger(0,"BTN_2",OBJPROP_BGCOLOR) == clrYellow)
        {
         ObjectSetInteger(0,"BTN_2",OBJPROP_COLOR,clrGold);
         ObjectSetInteger(0,"BTN_2",OBJPROP_BGCOLOR,clrBlack);
         ObjectSetInteger(0,"BTN_2",OBJPROP_STATE,false);
        }

      else
         if(sparam == "BTN_2" && ObjectGetInteger(0,"BTN_2",OBJPROP_BGCOLOR) == clrBlack)
           {
            ObjectSetInteger(0,"BTN_2",OBJPROP_COLOR,clrBlack);
            ObjectSetInteger(0,"BTN_2",OBJPROP_BGCOLOR,clrYellow);
            ObjectSetInteger(0,"BTN_2",OBJPROP_STATE,false);
           }
      if(sparam == "BTN_3" && ObjectGetInteger(0,"BTN_3",OBJPROP_BGCOLOR) == clrYellow)
        {
         ObjectSetInteger(0,"BTN_3",OBJPROP_COLOR,clrGold);
         ObjectSetInteger(0,"BTN_3",OBJPROP_BGCOLOR,clrBlack);
         ObjectSetInteger(0,"BTN_3",OBJPROP_STATE,false);
        }
      else
         if(sparam == "BTN_3" && ObjectGetInteger(0,"BTN_3",OBJPROP_BGCOLOR) == clrBlack)
           {
            ObjectSetInteger(0,"BTN_3",OBJPROP_COLOR,clrBlack);
            ObjectSetInteger(0,"BTN_3",OBJPROP_BGCOLOR,clrYellow);
            ObjectSetInteger(0,"BTN_3",OBJPROP_STATE,false);
           }
      if(sparam == "BTN_4" && ObjectGetInteger(0,"BTN_4",OBJPROP_BGCOLOR) == clrYellow)
        {
         ObjectSetInteger(0,"BTN_4",OBJPROP_COLOR,clrGold);
         ObjectSetInteger(0,"BTN_4",OBJPROP_BGCOLOR,clrBlack);
         ObjectSetInteger(0,"BTN_4",OBJPROP_STATE,false);
        }
      else
         if(sparam == "BTN_4" && ObjectGetInteger(0,"BTN_4",OBJPROP_BGCOLOR) == clrBlack)
           {
            ObjectSetInteger(0,"BTN_4",OBJPROP_COLOR,clrBlack);
            ObjectSetInteger(0,"BTN_4",OBJPROP_BGCOLOR,clrYellow);
            ObjectSetInteger(0,"BTN_4",OBJPROP_STATE,false);
           }
      if(sparam == "BTN_5" && ObjectGetInteger(0,"BTN_5",OBJPROP_BGCOLOR) == clrYellow)
        {
         ObjectSetInteger(0,"BTN_5",OBJPROP_COLOR,clrGold);
         ObjectSetInteger(0,"BTN_5",OBJPROP_BGCOLOR,clrBlack);
         ObjectSetInteger(0,"BTN_5",OBJPROP_STATE,false);
        }
      else
         if(sparam == "BTN_5" && ObjectGetInteger(0,"BTN_5",OBJPROP_BGCOLOR) == clrBlack)
           {
            ObjectSetInteger(0,"BTN_5",OBJPROP_COLOR,clrBlack);
            ObjectSetInteger(0,"BTN_5",OBJPROP_BGCOLOR,clrYellow);
            ObjectSetInteger(0,"BTN_5",OBJPROP_STATE,false);
           }
      if(sparam == "BTN_6" && ObjectGetInteger(0,"BTN_6",OBJPROP_BGCOLOR) == clrYellow)
        {
         ObjectSetInteger(0,"BTN_6",OBJPROP_COLOR,clrGold);
         ObjectSetInteger(0,"BTN_6",OBJPROP_BGCOLOR,clrBlack);
         ObjectSetInteger(0,"BTN_6",OBJPROP_STATE,false);
        }
      else
         if(sparam == "BTN_6" && ObjectGetInteger(0,"BTN_6",OBJPROP_BGCOLOR) == clrBlack)
           {
            ObjectSetInteger(0,"BTN_6",OBJPROP_COLOR,clrBlack);
            ObjectSetInteger(0,"BTN_6",OBJPROP_BGCOLOR,clrYellow);
            ObjectSetInteger(0,"BTN_6",OBJPROP_STATE,false);
           }

     }

  }
//+------------------------------------------------------------------+


この記事が気に入ったらサポートをしてみませんか?