[MQL5]俺得MT5ショートカット追加スクリプト

# ショートカット一覧

|key|description|
|---|---|
|P|スクリーンショットを撮る(Files/に保存)|
|1|すべての時間足をM1に変更|
|2|すべての時間足をM5に変更|
|3|すべての時間足をM15に変更|
|4|すべての時間足をM30に変更|
|5|すべての時間足をH1に変更|
|6|すべての時間足をH4に変更|
|7|すべての時間足をD1に変更|
|8|すべての時間足をW1に変更|
|9|すべての時間足をMNに変更|

# スクリプト

```　C++
//+------------------------------------------------------------------+
//|                                            AdvancedShortCuts.mq5 |
//|                                                Shintaro Tanikawa |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Shintaro Tanikawa"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property description "The script to add some of utility shortcuts."

#include <Utilities.mqh>

//---------------------------------------------------------
// Properties

// colors
static const long vLineColor = clrRed;
static const long hLineColor = clrGold;

// keyboards
static const string keyScreenShot                 = "P";
static const string keyAllTimeFramesToM1          = "1";
static const string keyAllTimeFramesToM5          = "2";
static const string keyAllTimeFramesToM15         = "3";
static const string keyAllTimeFramesToM30         = "4";
static const string keyAllTimeFramesToH1          = "5";
static const string keyAllTimeFramesToH4          = "6";
static const string keyAllTimeFramesToD1          = "7";
static const string keyAllTimeFramesToW1          = "8";
static const string keyAllTimeFramesToMN1         = "9";

//----------------------------------------------------------

class ChartEventHandler {
public:
  void OnChartEventKeyDown(const long &lparam,
                           const double &dparam,
                           const string &sparam)
  {
    const string keyString = Utilities::KeyCodeToString(lparam);
    if (keyString == keyAllTimeFramesToM1)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_M1);
    else if (keyString == keyAllTimeFramesToM5)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_M5);
    else if (keyString == keyAllTimeFramesToM15)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_M15);
    else if (keyString == keyAllTimeFramesToM30)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_M30);
    else if (keyString == keyAllTimeFramesToH1)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_H1);
    else if (keyString == keyAllTimeFramesToH4)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_H4);
    else if (keyString == keyAllTimeFramesToD1)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_D1);
    else if (keyString == keyAllTimeFramesToW1)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_W1);
    else if (keyString == keyAllTimeFramesToMN1)
      Utilities::SetAllTimeFrames(ChartID(), PERIOD_MN1);
    else if (keyString == keyScreenShot)
      Utilities::TakeScreenShot(ChartID());
  }
  
  void OnChartEventMouseMove(const long &lparam,
                             const double &dparam,
                             const string &sparam)
  {
    _mousePositionX = (int)lparam;
    _mousePositionY = (int)dparam;
  }
  
  void OnChartEventMouseClick(const long &lparam,
                              const double &dparam,
                              const string &sparam)
  {
    // do something
  }
  
private:
  int _mousePositionX;
  int _mousePositionY;
};

int OnInit()
{
   // enable CHART_EVENT_MOUSE_MOVE messages 
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,1); 
   
   return(INIT_SUCCEEDED);
}

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
  static ChartEventHandler eventHandler;
  switch (id)
  {
    case CHARTEVENT_KEYDOWN:
      eventHandler.OnChartEventKeyDown(lparam, dparam, sparam);
      break;
    case CHARTEVENT_CLICK:
      eventHandler.OnChartEventMouseClick(lparam, dparam, sparam);
      break;
    case CHARTEVENT_MOUSE_MOVE:
      eventHandler.OnChartEventMouseMove(lparam, dparam, sparam);
      break;
    default:
      break;
  }
}
//+------------------------------------------------------------------+

```

```　C++
//+------------------------------------------------------------------+
//|                                                    Utilities.mqh |
//|                                                Shintaro Tanikawa |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Shintaro Tanikawa"
#property link      "https://www.mql5.com"

static const string hLineName = "H-Line";
static const string vLineName = "V-Line";

static const int screenWidth = 1200;
static const int screenHeight = 600;

class Utilities {
public:
  static void TakeScreenShot(long chartId)
  {
    MqlDateTime stm;
    TimeToStruct(TimeCurrent(), stm);
    string name = StringFormat("ChartScreenShot-%s-%s-%d%02d%02d-%02d%02d%02d.png",
                                  ChartSymbol(chartId),
                                  TimeFramesToString(ChartPeriod(chartId)),
                                  stm.year,
                                  stm.mon,
                                  stm.day,
                                  stm.hour,
                                  stm.min,
                                  stm.sec);
                                  
    if (ChartScreenShot(chartId, name, screenWidth, screenHeight))
      Print("Saved a screen shot of current chart. (", name, ")");
  }
  
  static void SetAllTimeFrames(long chartId,
                               const ENUM_TIMEFRAMES timeframes)
  {
    for (int i = 0; i < CHARTS_MAX; ++i)
    {
      if (ChartPeriod(chartId) != timeframes)
      {
        ChartSetSymbolPeriod(chartId, ChartSymbol(chartId), timeframes);
      }
      chartId = ChartNext(chartId);
    }
  }

  static void CreateHLine(const long chartId,
                          const int &x,
                          const int &y,
                          const long& clr)
  {
    datetime dt = 0;
    double price = 0;
    int window = 0;
    if (ChartXYToTimePrice(ChartID(), x, y, window, dt, price))
    {
      if (ObjectFind(chartId, hLineName))
      {
        ObjectDelete(chartId, hLineName);
      }
      
      if (ObjectCreate(chartId, hLineName, OBJ_HLINE, 0, dt, price))
      {
        ObjectSetInteger(chartId, hLineName, OBJPROP_COLOR, clr);
        
        ChartRedraw(ChartID());
      }
    }
  }
  
  static void CreateVLine(const long chartId,
                          const int &x,
                          const int &y,
                          const long& clr)
  {
    datetime dt = 0;
    double price = 0;
    int window = 0;
    if (ChartXYToTimePrice(ChartID(), x, y, window, dt, price))
    {
      if (ObjectFind(chartId, vLineName))
      {
        ObjectDelete(chartId, vLineName);
      }
      
      if (ObjectCreate(chartId, vLineName, OBJ_VLINE, window, dt, price))
      {
        ObjectSetInteger(chartId, vLineName, OBJPROP_COLOR, clr);
        
        ChartRedraw(ChartID());
      }
    }
  }
  
  static void CreateTextLabel(const long chartId,
                              const int &x,
                              const int &y,
                              const long& clr,
                              const string font,
                              const int fontSize)
  {
    datetime dt = 0;
    double price = 0;
    int window = 0;
    if (ChartXYToTimePrice(ChartID(), x, y, window, dt, price))
    {
      const string name = TimeToString(TimeCurrent(), TIME_DATE | TIME_SECONDS);
      if (ObjectCreate(chartId, name, OBJ_LABEL, window, dt, price))
      {
        ObjectSetInteger(chartId, name, OBJPROP_COLOR, clr);
        ObjectSetString(chartId, name, OBJPROP_FONT, font);
        ObjectSetInteger(chartId, name , OBJPROP_FONTSIZE, fontSize);
        
        ChartRedraw(chartId);
      }
    }
  }
  
  static string KeyCodeToString(const long &key)
  {
    switch ((int)key)
    {
      case 49: // Pressed Key 1
        return "1";
      case 50: // Pressed Key 2
        return "2";
      case 51: // Pressed Key 3
        return "3";
      case 52: // Pressed Key 4
        return "4";
      case 53: // Pressed Key 5
        return "5";
      case 54: // Pressed Key 6
        return "6";
      case 55: // Pressed Key 7
        return "7";
      case 56: // Pressed Key 8
        return "8";
      case 57: // Pressed Key 9
        return "9";
      case 65: // Pressed Key A
        return "A";
      case 66: // Pressed Key B
        return "B";
      case 67: // Pressed Key C
        return "C";
      case 68: // Pressed Key D
        return "D";
      case 69: // Pressed Key E
        return "E";
      case 70: // Pressed Key F
        return "F";
      case 71: // Pressed Key G
        return "G";
      case 72: // Pressed Key H
        return "H";
      case 73: // Pressed Key I
        return "I";
      case 74: // Pressed Key J
        return "J";
      case 75: // Pressed Key K
        return "K";
      case 76: // Pressed Key L
        return "L";
      case 77: // Pressed Key M
        return "M";
      case 78: // Pressed Key N
        return "N";
      case 79: // Pressed Key O
        return "O";
      case 80: // Pressed Key P
        return "P";
      case 81: // Pressed Key Q
        return "Q";
      case 82: // Pressed Key R
        return "R";
      case 83: // Pressed Key S
        return "S";
      case 84: // Pressed Key T
        return "T";
      case 85: // Pressed Key U
        return "U";
      case 86: // Pressed Key V
        return "V";
      case 87: // Pressed Key W
        return "W";
      case 88: // Pressed Key X
        return "X";
      case 89: // Pressed Key Y
        return "Y";
      case 90: // Pressed Key Z
        return "Z";
      default:
        return "";
    }
  }

  static bool IsKeyCodeNumber(const long &keyCode)
  {
    return (keyCode >= 49 && keyCode <= 57);
  }
  
  static bool IsKeyCodeAlphabet(const long &keyCode)
  {
    return (keyCode >= 65 && keyCode <= 90);
  }
  
  static string TimeFramesToString(ENUM_TIMEFRAMES timeframes)
  {
    switch (timeframes)
    {
      case PERIOD_M1:
        return "M1";
      case PERIOD_M5:
        return "M5";
      case PERIOD_M15:
        return "M15;";
      case PERIOD_M30:
        return "M30";
      case PERIOD_H1:
        return "H1";
      case PERIOD_H4:
        return "H4";
      case PERIOD_D1:
        return "D1";
      case PERIOD_W1:
        return "W1";
      case PERIOD_MN1:
        return "MN";
      default:
        return "";
    }
  }
};
//+------------------------------------------------------------------+

```