//+------------------------------------------------------------------+
//| Panel_Buttons.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property description "The panel with several CButton buttons"
#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>
//+------------------------------------------------------------------+
//| 정의 |
//+------------------------------------------------------------------+
//--- 들여쓰기 및 간격
#define INDENT_LEFT (11) // 왼쪽에서 들여쓰기(경계선 너비 허용)
#define INDENT_TOP (11) // 위에서 들여쓰기(경계선 너비 허용)
#define CONTROLS_GAP_X (5) // 간격(X 좌표)
#define CONTROLS_GAP_Y (5) // 간격(Y 좌표)
//--- 버튼용
#define BUTTON_WIDTH (100) // 크기(X 좌표)
#define BUTTON_HEIGHT (20) // 크기(Y 좌표)
//--- 표시 영역용
#define EDIT_HEIGHT (20) // 크기(Y 좌표)
//--- 사용자 정의 함수 유형 생성
typedef int(*TAction)(string,int);
//+------------------------------------------------------------------+
//| 파일 열기 |
//+------------------------------------------------------------------+
int Open(string name,int id)
{
PrintFormat("%s 함수 호출됨 (name=%s id=%d)",__FUNCTION__,name,id);
return(1);
}
//+------------------------------------------------------------------+
//| 파일 저장 |
//+------------------------------------------------------------------+
int Save(string name,int id)
{
PrintFormat("%s 함수 호출됨 (name=%s id=%d)",__FUNCTION__,name,id);
return(2);
}
//+------------------------------------------------------------------+
//| 파일 닫기 |
//+------------------------------------------------------------------+
int Close(string name,int id)
{
PrintFormat("%s 함수 호출됨 (name=%s id=%d)",__FUNCTION__,name,id);
return(3);
}
//+------------------------------------------------------------------+
//| 이벤트 처리 기능으로 버튼 클래스 만들기 |
//+------------------------------------------------------------------+
class MyButton: public CButton
{
private:
TAction m_action; // 차트 이벤트 핸들러
public:
MyButton(void){}
~MyButton(void){}
//--- 이벤트 처리 함수에 대한 버튼 텍스트 및 포인터 지정하는 생성자
MyButton(string text,TAction act)
{
Text(text);
m_action=act;
}
//--- OnEvent() 이벤트 처리기에서 호출된 사용자 지정 함수 설정
void SetAction(TAction act){m_action=act;}
//--- 표준 차트 이벤트 처리기
virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam) override
{
if(m_action!=NULL && lparam==Id())
{
//--- 커스텀 핸들러 호출
m_action(sparam,(int)lparam);
return(true);
}
else
//--- CButton 부모 클래스에서 처리기를 호출한 결과를 반환합니다
return(CButton::OnEvent(id,lparam,dparam,sparam));
}
};
//+------------------------------------------------------------------+
//| CControlsDialog 클래스 |
//| 목적: 어플리케이션 관리를 위한 그래픽 패널 |
//+------------------------------------------------------------------+
class CControlsDialog : public CAppDialog
{
private:
CArrayObj m_buttons; // 버튼 배열
public:
CControlsDialog(void){};
~CControlsDialog(void){};
//--- 생성
virtual bool Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2) override;
//--- 버튼 추가
bool AddButton(MyButton &button){return(m_buttons.Add(GetPointer(button)));m_buttons.Sort();};
protected:
//--- 버튼 생성
bool CreateButtons(void);
};
//+------------------------------------------------------------------+
//| 차트에 CControlsDialog 객체 생성 |
//+------------------------------------------------------------------+
bool CControlsDialog::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);
return(CreateButtons());
//---
}
//+------------------------------------------------------------------+
//| CControlsDialog 패널에 버튼 작성 및 추가 |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateButtons(void)
{
//--- 버튼 좌표 계산
int x1=INDENT_LEFT;
int y1=INDENT_TOP+(EDIT_HEIGHT+CONTROLS_GAP_Y);
int x2;
int y2=y1+BUTTON_HEIGHT;
//--- 버튼 개체와 함수에 대한 포인터 추가
AddButton(new MyButton("Open",Open));
AddButton(new MyButton("Save",::Save));
AddButton(new MyButton("Close",Close));
//--- 버튼을 그래픽으로 생성
for(int i=0;i<m_buttons.Total();i++)
{
MyButton *b=(MyButton*)m_buttons.At(i);
x1=INDENT_LEFT+i*(BUTTON_WIDTH+CONTROLS_GAP_X);
x2=x1+BUTTON_WIDTH;
if(!b.Create(m_chart_id,m_name+"bt"+b.Text(),m_subwin,x1,y1,x2,y2))
{
PrintFormat("버튼 생성 실패 %s %d",b.Text(),i);
return(false);
}
//--- 각 버튼을 CControlsDialog 컨테이너에 추가
if(!Add(b))
return(false);
}
//--- 성공
return(true);
}
//--- 프로그램을 시작할 때 개체를 자동으로 만들려면 전역 수준에서 개체를 선언합니다
CControlsDialog MyDialog;
//+------------------------------------------------------------------+
//| Expert 초기화 함수 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 이제 차트에 개체를 만듭니다
if(!MyDialog.Create(0,"Controls",0,40,40,380,344))
return(INIT_FAILED);
//--- 어플리케이션 실행
MyDialog.Run();
//--- 어플리케이션이 성공적으로 초기화됨
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert 초기화 해제 함수 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- 대화상자 제거
MyDialog.Destroy(reason);
}
//+------------------------------------------------------------------+
//| Expert 차트 이벤트 함수 |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, // 이벤트 ID
const long& lparam, // long 타입의 이벤트 매개 변수
const double& dparam, // double 타입의 이벤트 매개 변수
const string& sparam) // 문자열 타입의 이벤트 매개 변수
{
//--- 차트 이벤트에 대해 부모 클래스에서 핸들러를 호출(여기서는 CAppDialog 입니다).
MyDialog.ChartEvent(id,lparam,dparam,sparam);
}
|