例えば指値を入力して注文を出すようなケースで、文字入力は CEdit を使うことができます
また入力した指値を CLabel で表示させてみましょう
CEdit
以下でCAppDialog にCLabel と CButton を配置しましたが、次に文字入力ができる CEdit を入れてみます
MQL4でボタンを作成していましたが、よりトレードに使えるパネルを CAppDialog と CLabel を使って作成してみたいと思います...
CEdit 文字入力
最初に基本的なダイアログを作ります
大まかな流れとして
- Edit.mqhのインクルード
- ..\MQL4\Include\Controls\Edit.mqh ヘッダーファイルのインクルードします
- あわせて文字入力を決定するボタンのためのButton.mqhもインクルード
- カスタムクラスを定義
- CAppDialogを継承したユーザーが決めたクラス、 CTestPanel クラスを定義してCreate
- CreateCedit() を定義
- Event Handlerを使って、ボタンクリックでのCEditに入力された文字列をPrint() する機能を OnClickButton() に設定
- オブジェクトの作成
- CTestPanel からオブジェクト(インスタンス)であるTPanelを作成
OnInit() でCreateして実行、Run()
- CTestPanel からオブジェクト(インスタンス)であるTPanelを作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <Controls\Dialog.mqh> #include <Controls\Edit.mqh> #include <Controls\Button.mqh> class CTestPanel : public CAppDialog{ private: CEdit testEdit; CButton okButton; public: CTestPanel(void){}; ~CTestPanel(void){}; //--- create virtual bool Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2); protected: bool CreateEdit(void); bool CreateButton(void); void OnClickButton(void); // Event Handler EVENT_MAP_BEGIN(CTestPanel) ON_EVENT(ON_CLICK, okButton, OnClickButton) EVENT_MAP_END(CAppDialog) }; bool CTestPanel::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2){ if(!CAppDialog::Create(chart, name, subwin, x1, y1, x2, y2)) return(false); if(!CreateEdit()) return(false); if(!CreateButton()) return(false); return(true); } bool CTestPanel::CreateEdit(void){ if(!testEdit.Create(m_chart_id, "Edit01", m_subwin, 45, 26, 145, 46)) return(false); // allow editing if(!testEdit.ReadOnly(false)) return(false); if(!testEdit.Text("Enter text")) return(false); if(!Add(testEdit)) return(false); return(true); } bool CTestPanel::CreateButton(void){ if(!okButton.Create(m_chart_id, "Button01", m_subwin, 45, 56, 145, 76)) return(false); if(!okButton.Text("OK")) return(false); if(!Add(okButton)) return(false); return(true); } void CTestPanel::OnClickButton(){ string inputString = testEdit.Text(); Print("Inpput: ", inputString); } CTestPanel TPanel; int OnInit(){ if(!TPanel.Create( ChartID(), "TestPanel", 0, 160, 40, 360, 210 )){ return(INIT_FAILED); } TPanel.Run(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ TPanel.Destroy(reason); } void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam ){ TPanel.ChartEvent(id, lparam, dparam, sparam); } |
文字入力すると「エキスパート」タブに文字列がPrintOutされます
CLabel
CEdit の入力文字列を CLabel で表示させてみます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
#include <Controls\Dialog.mqh> #include <Controls\Edit.mqh> #include <Controls\Button.mqh> #include <Controls\Label.mqh> class CTestPanel : public CAppDialog{ private: CEdit testEdit; CButton okButton; CLabel testLabel; public: CTestPanel(void){}; ~CTestPanel(void){}; //--- create virtual bool Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2); protected: bool CreateEdit(void); bool CreateButton(void); void OnClickButton(void); bool CreateLabel(void); // Event Handler EVENT_MAP_BEGIN(CTestPanel) ON_EVENT(ON_CLICK, okButton, OnClickButton) EVENT_MAP_END(CAppDialog) }; bool CTestPanel::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2){ if(!CAppDialog::Create(chart, name, subwin, x1, y1, x2, y2)) return(false); if(!CreateEdit()) return(false); if(!CreateButton()) return(false); if(!CreateLabel()) return(false); return(true); } bool CTestPanel::CreateEdit(void){ if(!testEdit.Create(m_chart_id, "Edit01", m_subwin, 45, 26, 145, 46)) return(false); // allow editing if(!testEdit.ReadOnly(false)) return(false); if(!testEdit.Text("Enter text")) return(false); if(!Add(testEdit)) return(false); return(true); } bool CTestPanel::CreateButton(void){ if(!okButton.Create(m_chart_id, "Button01", m_subwin, 45, 56, 145, 76)) return(false); if(!okButton.Text("OK")) return(false); if(!Add(okButton)) return(false); return(true); } bool CTestPanel::CreateLabel(void){ if(!testLabel.Create(m_chart_id, "Label", m_subwin, 45, 86, 145, 106)) return(false); if(!testLabel.Text("")) return(false); if(!Add(testLabel)) return(false); return(true); } void CTestPanel::OnClickButton(){ string inputString = testEdit.Text(); testLabel.Text(inputString); Print("Inpput: ", inputString); } CTestPanel TPanel; int OnInit(){ if(!TPanel.Create( ChartID(), "TestPanel", 0, 160, 40, 360, 210 )){ return(INIT_FAILED); } TPanel.Run(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ TPanel.Destroy(reason); } void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam ){ TPanel.ChartEvent(id, lparam, dparam, sparam); } |
入力文字がLabelで表示されました
CLabel に文字を表示させましたが、CEdit で ReadOnly に設定するとLabelのようにも使えます