EAの新しい配信方法を考えてみる。その2-2
こんにちは~
fx-on.comの岩淵です。
今日も前回に引き続き「2.デモ版のダウンロード」に挑戦していきます。
前回までの内容はこちら↓
EAの新しい配信方法を考えてみる。その1
EAの新しい配信方法を考えてみる。その2 前回まででMT4からサーバーへリクエストを送信 → サーバー上でmq4ファイルをコンパイル → MT4のExpertsフォルダへダウンロードまで作成しました。 今回はMT4側からリクエスト送信までの見栄えを整えてみたいと思います。
EAの新しい配信方法を考えてみる。その2 前回まででMT4からサーバーへリクエストを送信 → サーバー上でmq4ファイルをコンパイル → MT4のExpertsフォルダへダウンロードまで作成しました。 今回はMT4側からリクエスト送信までの見栄えを整えてみたいと思います。
準備
前回作成したdownload.mq4をベースに改造していきます。 前回までのdownload.mq4#property version "1.00" #property strict #import "shell32.dll" int ShellExecuteW(int, string, string, string, string, int); #import extern string URL = "http://localhost/MT4_Test/index.php"; //ダウンロード元URL extern string FileName = "test"; //ダウンロードファイル名 extern string Folder = "Experts"; //保存先フォルダ名 int OnInit(){ string command; command = "/c @powershell -NoProfile -ExecutionPolicy Bypass -Command"; command += " \"$d=new-object System.Net.WebClient;"; command += "$d.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;"; command += "$d.DownloadFile('"; command += URL + "?file=" + FileName; command += "', '"; command += TerminalInfoString(TERMINAL_DATA_PATH); command += "\\MQL4\\" + Folder + "\\"; command += FileName + ".ex4"; command += "')\""; if(Shell32::ShellExecuteW(0, "open", "C:\\Windows\\System32\\cmd.exe", command, "", 0) > 32){ Sleep(5000); Print(command); MessageBox(FileName + ".ex4をダウンロードしました。\n更新または再起動を行って下さい。"); } return(INIT_SUCCEEDED); } void OnTick(){}
開発
前回作成したダウンロード処理は関数にします。 DownloadEX4という関数を作成し、引数にダウンロードするファイル名を与えます。 あとは任意のタイミングでこの関数を実行すると、引数で指定したファイルがダウンロードされるイメージです。void DownloadEX4(string file){ string command; command = "/c @powershell -NoProfile -ExecutionPolicy Bypass -Command"; command += " \"$d=new-object System.Net.WebClient;"; command += "$d.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;"; command += "$d.DownloadFile('"; command += URL + "?file=" + file; command += "', '"; command += TerminalInfoString(TERMINAL_DATA_PATH); command += "\\MQL4\\" + Folder + "\\"; command += file + ".ex4"; command += "')\""; if(Shell32::ShellExecuteW(0, "open", "C:\\Windows\\System32\\cmd.exe", command, "", 0) > 32){ Sleep(5000); Print(command); MessageBox(file + ".ex4をダウンロードしました。\n更新または再起動を行って下さい。"); } }Button.mqh、Dialog.mqh、Label.mqh、Panel.mqhを読み込んでダウンロードフォームを作成していきます。
#include <Controls\\Button.mqh> #include <Controls\Dialog.mqh> #include <Controls\Label.mqh> #include <Controls\Panel.mqh>このあたりは昔私が書いた記事の「GUIを利用したインジケーターの作り方」の応用です。
class CForm : public CAppDialog{ private: CButton dl_btn, demo_btn; CLabel product_label; CPanel main_panel; public: CForm(void); ~CForm(void); virtual bool Create(void); string getProductName(void); }; bool CForm::Create(){ int x = 200, y = 200; int window_width = 440, window_height = 340; int panel_width = 410, panel_height = 230; int label_width = 200, label_height = 20; int btn_width = 200, btn_height = 50; if(CAppDialog::Create(0, "Download", 0, x, y, x + window_width, y + window_height) == false){ return(false); } x = 10; y = 10; if(main_panel.Create(0, "MainPanel", 0, x, y, x + panel_width, y + panel_height) == false){ return(false); } main_panel.ColorBackground(clrGainsboro); if(Add(main_panel) == false){ return(false); } x = 20; y = 20; if(product_label.Create(0, "ProductLabel", 0, x, y, x + label_width, y + label_height) == false){ return(false); } product_label.Color(clrBlack); product_label.Font("メイリオ"); product_label.FontSize(18); product_label.Text("test"); if(Add(product_label) == false){ return(false); } x = 10; y =250; if(dl_btn.Create(0, "DownloadButton", 0, x, y, x + btn_width, y + btn_height) == false){ return(false); } dl_btn.Color(clrWhite); dl_btn.ColorBackground(clrDodgerBlue); dl_btn.ColorBackground(clrRoyalBlue); dl_btn.Font("メイリオ"); dl_btn.Text("ダウンロード"); if(Add(dl_btn) == false){ return(false); } x = 10 + btn_width + 10; if(demo_btn.Create(0, "DemoDownloadButton", 0, x, y, x + btn_width, y + btn_height) == false){ return(false); } demo_btn.Color(clrWhite); demo_btn.ColorBackground(clrOrange); demo_btn.ColorBorder(clrDarkOrange); demo_btn.Font("メイリオ"); demo_btn.Text("デモ版ダウンロード"); if(Add(demo_btn) == false){ return(false); } return(true); } CForm::CForm(void){} CForm::~CForm(void){} string CForm::getProductName(void){ return(this.product_label.Text()); } CForm dl_form;実際に表示してみるとこんな感じです。 グレーのエリアにはEAの選択画面を作成する予定ですが、まだどんな感じにするか思案中ですので保留としてます。 選択したEAの名前が上のラベルに表示されるイメージです。 とりあえず今は「test」としています。 画面下には通常のダウンロードボタンとデモ版のダウンロードボタンを作成しました。 通常のダウンロードボタンはex4ファイルをダウンロードしてきてExpertsフォルダに保存します。 (選択中のEAが未購入であればfx-on.comへのリンクにするのもいいですね) デモ版のダウンロードボタンをクリックすると、サーバー側でデモ口座で縛ってコンパイルを実行したex4をダウンロードさせたいです。 とりあえず今回は制限など関係なしにダウンロードさせてみます。 OnChartEvent内にコードを追加してボタンのクリックイベントを判定し、先ほど作成したDownloadEX4を実行します。 ダウンロードするファイル名は、ラベルに設定されているテキストを取得してきて指定しています。
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam){ dl_form.ChartEvent(id, lparam, dparam, sparam); if(id == CHARTEVENT_OBJECT_CLICK){ if(sparam == "DownloadButton"){ DownloadEX4(dl_form.getProductName()); } else if(sparam == "DemoDownloadButton"){ DownloadEX4(dl_form.getProductName()); } } }ソースコード全体は以下のとおりです。
#property version "1.00" #property strict #import "shell32.dll" int ShellExecuteW(int, string, string, string, string, int); #import #include <Controls\\Button.mqh> #include <Controls\Dialog.mqh> #include <Controls\Label.mqh> #include <Controls\Panel.mqh> extern string URL = "http://localhost/MT4_Test/index.php"; //ダウンロード元URL extern string Folder = "Experts"; //保存先フォルダ名 class CForm : public CAppDialog{ private: CButton dl_btn, demo_btn; CLabel product_label; CPanel main_panel; public: CForm(void); ~CForm(void); virtual bool Create(void); string getProductName(void); }; bool CForm::Create(){ int x = 200, y = 200; int window_width = 440, window_height = 340; int panel_width = 410, panel_height = 230; int label_width = 200, label_height = 20; int btn_width = 200, btn_height = 50; if(CAppDialog::Create(0, "Download", 0, x, y, x + window_width, y + window_height) == false){ return(false); } x = 10; y = 10; if(main_panel.Create(0, "MainPanel", 0, x, y, x + panel_width, y + panel_height) == false){ return(false); } main_panel.ColorBackground(clrGainsboro); if(Add(main_panel) == false){ return(false); } x = 20; y = 20; if(product_label.Create(0, "ProductLabel", 0, x, y, x + label_width, y + label_height) == false){ return(false); } product_label.Color(clrBlack); product_label.Font("メイリオ"); product_label.FontSize(18); product_label.Text("test"); if(Add(product_label) == false){ return(false); } x = 10; y =250; if(dl_btn.Create(0, "DownloadButton", 0, x, y, x + btn_width, y + btn_height) == false){ return(false); } dl_btn.Color(clrWhite); dl_btn.ColorBackground(clrDodgerBlue); dl_btn.ColorBackground(clrRoyalBlue); dl_btn.Font("メイリオ"); dl_btn.Text("ダウンロード"); if(Add(dl_btn) == false){ return(false); } x = 10 + btn_width + 10; if(demo_btn.Create(0, "DemoDownloadButton", 0, x, y, x + btn_width, y + btn_height) == false){ return(false); } demo_btn.Color(clrWhite); demo_btn.ColorBackground(clrOrange); demo_btn.ColorBorder(clrDarkOrange); demo_btn.Font("メイリオ"); demo_btn.Text("デモ版ダウンロード"); if(Add(demo_btn) == false){ return(false); } return(true); } CForm::CForm(void){} CForm::~CForm(void){} string CForm::getProductName(void){ return(this.product_label.Text()); } CForm dl_form; int OnInit(){ if(dl_form.Create() == false){ return(INIT_FAILED); } if(dl_form.Run() == false){ return(INIT_FAILED); } return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ dl_form.Destroy(); } void OnTick(){} void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam){ dl_form.ChartEvent(id, lparam, dparam, sparam); if(id == CHARTEVENT_OBJECT_CLICK){ if(sparam == "DownloadButton"){ DownloadEX4(dl_form.getProductName()); } else if(sparam == "DemoDownloadButton"){ DownloadEX4(dl_form.getProductName()); } } } void DownloadEX4(string file){ string command; command = "/c @powershell -NoProfile -ExecutionPolicy Bypass -Command"; command += " \"$d=new-object System.Net.WebClient;"; command += "$d.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;"; command += "$d.DownloadFile('"; command += URL + "?file=" + file; command += "', '"; command += TerminalInfoString(TERMINAL_DATA_PATH); command += "\\MQL4\\" + Folder + "\\"; command += file + ".ex4"; command += "')\""; if(Shell32::ShellExecuteW(0, "open", "C:\\Windows\\System32\\cmd.exe", command, "", 0) > 32){ Sleep(5000); Print(command); MessageBox(file + ".ex4をダウンロードしました。\n更新または再起動を行って下さい。"); } }