ユーザーの選択をラジオボタン CRadioBox を使って確認できます
CRadioButton
CRadioBox のプレスの状態をどのように取得するかですが
CRadioBox.mqh のヘッダーファイルには以下のような記述があるので
State() が使えるようです
1 2 3 |
//--- state bool State(void) const { return(m_button.Pressed()); } bool State(const bool flag) { return(m_button.Pressed(flag)); } |
ラジオボタンとチェックボックスはユーザーの選択を取得するという点では似ていますが
ラジオボタンは排他的な選択で、一方が選択されると他方は非選択となるような関係を形成します
なので1つだけではわかりにくいですが、この後で複数の CRaidoButton でグループでの扱いをやってみます
CRadioButton 変更を受け取る
最初に基本的なダイアログを作ります
大まかな流れとして
- CRadioButton.mqhのインクルード
- ..\MQL4\Include\Controls\CRadioButton.mqh ヘッダーファイルをインクルードします
- ..\MQL4\Include\Controls\CRadioButton.mqh ヘッダーファイルをインクルードします
- カスタムクラスを定義
- CAppDialogを継承したユーザーが決めたクラス、 CTestPanel クラスを定義して下地になるパネルをCreate
- CreateCedit() を定義
- Event Handlerを使って、CRadioButtonの変更を State() で取得してチャート上にComment()で表示
- オブジェクトの作成
- 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 |
#include <Controls\Dialog.mqh> #include <Controls\RadioButton.mqh> class CTestPanel : public CAppDialog{ private: CRadioButton tRadioButton; 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 CreateRadioButton(void); void OnChangeRadioButton(void); }; // Event Handler EVENT_MAP_BEGIN(CTestPanel) ON_EVENT(ON_CHANGE, tRadioButton, OnChangeRadioButton) 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(!CreateRadioButton()) return(false); return(true); } bool CTestPanel::CreateRadioButton(void){ if(!tRadioButton.Create(m_chart_id, "RadioButton01", m_subwin, 45, 26, 145, 46)) return(false); if(!tRadioButton.Text("RadioButton")) return(false); if(!tRadioButton.State(false)) return(false); if(!Add(tRadioButton)) return(false); Comment("RadioButton = "+IntegerToString(tRadioButton.State())); return(true); } void CTestPanel::OnChangeRadioButton(void){ Comment("RadioButton = "+IntegerToString(tRadioButton.State())); Print("State="+IntegerToString(tRadioButton.State())); } 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); } |
RadioButtonの選択で1となり、非選択で0となります
CRadioGroup
複数の CRadioButton をまとめてグループとして扱うことができます
CRadioGroup.mqh は CRadioButton.mqh をインクルードしています
チェックされたかどうかは、Value() で確認できます
ここではビット演算子の右シフトを使いチェックされたステータスを取得し
どのボタンが選択されたかCommentで表示しています
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 |
#include <Controls\Dialog.mqh> #include <Controls\RadioGroup.mqh> class CTestPanel : public CAppDialog{ private: CRadioGroup tRadioGroup; 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 CreateRadioGroup(void); void OnChangeRadioGroup(void); }; // Event Handler EVENT_MAP_BEGIN(CTestPanel) ON_EVENT(ON_CHANGE, tRadioGroup, OnChangeRadioGroup) 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(!CreateRadioGroup()) return(false); return(true); } bool CTestPanel::CreateRadioGroup(void){ if(!tRadioGroup.Create(m_chart_id, m_name+"RadioButton", m_subwin, 45, 26, 165, 86)) return(false); if(!Add(tRadioGroup)) return(false); for(int i=0; i<3; i++){ if(!tRadioGroup.AddItem("RadioButton_"+IntegerToString(i), 1<<i)) return(false); } return(true); } void CTestPanel::OnChangeRadioGroup(void){ string comm; int val = tRadioGroup.Value(); if(val == 1) comm ="RadioButton_0 Selected"; else if(val == 2) comm ="RadioButton_1 Selected"; else if(val == 4) comm ="RadioButton_2 Selected"; Comment(comm); } 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); } |
RadioButton_2 が選択されています