ユーザーの選択を CheckBox を使って確認できます
CheckBox
CheckBox のチェックの状態をどのように取得するか
CheckBox.mqh のヘッダーファイルには以下のような記述があるので
Checked(void) が使えるようです
1 2 3 |
//--- state bool Checked(void) const { return(m_button.Pressed()); } bool Checked(const bool flag) { return(m_button.Pressed(flag)); } |
Event HandlerでStatus変更を受け取る
最初に基本的なダイアログを作ります
大まかな流れとして
- CheckBox.mqhのインクルード
- ..\MQL4\Include\Controls\CheckBox.mqh ヘッダーファイルをインクルードします
- ..\MQL4\Include\Controls\CheckBox.mqh ヘッダーファイルをインクルードします
- カスタムクラスを定義
- CAppDialogを継承したユーザーが決めたクラス、 CTestPanel クラスを定義して下地になるパネルをCreate
- CreateCedit() を定義
- Event Handlerを使って、CheckBox のチェックに応じてラベルを「Checked」「UnChecked」に変える機能を OnChangeCheckBox() に設定
- オブジェクトの作成
- 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 |
#include <Controls\Dialog.mqh> #include <Controls\CheckBox.mqh> class CTestPanel : public CAppDialog{ private: CCheckBox tCBox; 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); virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam); protected: bool CreateCheckBox(void); void OnChangeCheckBox(void); }; // Event Handler EVENT_MAP_BEGIN(CTestPanel) ON_EVENT(ON_CHANGE, tCBox, OnChangeCheckBox) 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(!CreateCheckBox()) return(false); return(true); } bool CTestPanel::CreateCheckBox(void){ if(!tCBox.Create(m_chart_id, m_name+"CheckBox01", m_subwin, 45, 26, 145, 46)) return(false); if(!tCBox.Color(clrBlue)) return(false); if(!tCBox.Text("Check Box")) return(false); if(!Add(tCBox)) return(false); return(true); } void CTestPanel::OnChangeCheckBox(){ if(tCBox.Checked() == 1){ tCBox.Text("Checked"); } if(tCBox.Checked() == 0){ tCBox.Text("UnChecked"); } } 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); } |
チェックを入れる
チェックを外す
CCheckGroup
複数の CCheckBox をまとめてグループとして扱うことができます
CheckGroup.mqh は CheckBox.mqh をインクルードしています
チェックされたかどうかは、Value() で確認できます
ここではビット演算子の右シフトを使いチェックされたステータスを取得し
Groupで合計して表示させます
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 |
#include <Controls\Dialog.mqh> #include <Controls\CheckGroup.mqh> class CTestPanel : public CAppDialog{ private: CCheckGroup tCheckGroup; 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); virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam); protected: bool CreateCheckGroup(void); void OnChangeCheckGroup(void); }; // Event Handler EVENT_MAP_BEGIN(CTestPanel) ON_EVENT(ON_CHANGE, tCheckGroup, OnChangeCheckGroup) 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(!CreateCheckGroup()) return(false); return(true); } bool CTestPanel::CreateCheckGroup(void){ if(!tCheckGroup.Create(m_chart_id, m_name+"CheckGroup", m_subwin, 45, 26, 145, 86)) return(false); if(!Add(tCheckGroup)) return(false); for(int i=0; i<3; i++){ if(!tCheckGroup.AddItem("CheckBox_"+IntegerToString(i), 1<<i)) return(false); } Comment("CheckGroup = "+IntegerToString(tCheckGroup.Value())); return(true); } void CTestPanel::OnChangeCheckGroup(void){ for(int i=0; i<3; i++){ Comment("CheckGroup = "+IntegerToString(tCheckGroup.Value())); } } 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); } |
CheckBox_0とCheckBox_1がチェックされてコメントで3となります
CheckBox_0 = 0001
CheckBox_1 = 0010
CheckBox_2 = 0100
なのですべてチェックされると7になります