#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>
#include <Controls\Label.mqh>
#include <Controls\ComboBox.mqh>
//--- 사전정의된 상수
#define X_START 0
#define Y_START 0
#define X_SIZE 280
#define Y_SIZE 300
//+------------------------------------------------------------------+
//| 메모리 작업을 위한 대화상자 클래스 |
//+------------------------------------------------------------------+
class CMemoryControl : public CAppDialog
{
private:
//--- 배열 크기
int m_arr_size;
//--- 배열
char m_arr_char[];
int m_arr_int[];
float m_arr_float[];
double m_arr_double[];
long m_arr_long[];
//--- 라벨
CLabel m_lbl_memory_physical;
CLabel m_lbl_memory_total;
CLabel m_lbl_memory_available;
CLabel m_lbl_memory_used;
CLabel m_lbl_array_size;
CLabel m_lbl_array_type;
CLabel m_lbl_error;
CLabel m_lbl_change_type;
CLabel m_lbl_add_size;
//--- 버튼
CButton m_button_add;
CButton m_button_free;
//--- 콤보 상자
CComboBox m_combo_box_step;
CComboBox m_combo_box_type;
//--- 콤보 상자에서 배열 유형의 현재 값
int m_combo_box_type_value;
public:
CMemoryControl(void);
~CMemoryControl(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);
//--- 차트 이벤트의 처리기
virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
protected:
//--- 라벨 생성
bool CreateLabel(CLabel &lbl,const string name,const int x,const int y,const string str,const int font_size,const int clr);
//--- 제어 요소 생성
bool CreateButton(CButton &button,const string name,const int x,const int y,const string str,const int font_size,const int clr);
bool CreateComboBoxStep(void);
bool CreateComboBoxType(void);
//--- 이벤트 처리기
void OnClickButtonAdd(void);
void OnClickButtonFree(void);
void OnChangeComboBoxType(void);
//--- 현재 배열 작업 방법
void CurrentArrayFree(void);
bool CurrentArrayAdd(void);
};
//+------------------------------------------------------------------+
//| 현재 배열의 최적화 메모리 |
//+------------------------------------------------------------------+
void CMemoryControl::CurrentArrayFree(void)
{
//--- 배열 크기 재설정
m_arr_size=0;
//--- 배열 풀기
if(m_combo_box_type_value==0)
ArrayFree(m_arr_char);
if(m_combo_box_type_value==1)
ArrayFree(m_arr_int);
if(m_combo_box_type_value==2)
ArrayFree(m_arr_float);
if(m_combo_box_type_value==3)
ArrayFree(m_arr_double);
if(m_combo_box_type_value==4)
ArrayFree(m_arr_long);
}
//+------------------------------------------------------------------+
//| 현재 배열에 대한 메모리 추가 시도 |
//+------------------------------------------------------------------+
bool CMemoryControl::CurrentArrayAdd(void)
{
//--- 사용된 메모리의 크기가 물리적 메모리의 크기를 초과할 경우 종료
if(TerminalInfoInteger(TERMINAL_MEMORY_PHYSICAL)/TerminalInfoInteger(TERMINAL_MEMORY_USED)<2)
return(false);
//--- 현재 유형에 따라 메모리 할당 시도
if(m_combo_box_type_value==0 && ArrayResize(m_arr_char,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==1 && ArrayResize(m_arr_int,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==2 && ArrayResize(m_arr_float,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==3 && ArrayResize(m_arr_double,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==4 && ArrayResize(m_arr_long,m_arr_size)==-1)
return(false);
//--- 할당된 메모리
return(true);
}
//+------------------------------------------------------------------+
//| 핸들링 이벤트 |
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(CMemoryControl)
ON_EVENT(ON_CLICK,m_button_add,OnClickButtonAdd)
ON_EVENT(ON_CLICK,m_button_free,OnClickButtonFree)
ON_EVENT(ON_CHANGE,m_combo_box_type,OnChangeComboBoxType)
EVENT_MAP_END(CAppDialog)
//+------------------------------------------------------------------+
//| 생성자 |
//+------------------------------------------------------------------+
CMemoryControl::CMemoryControl(void)
{
}
//+------------------------------------------------------------------+
//| 소멸자 |
//+------------------------------------------------------------------+
CMemoryControl::~CMemoryControl(void)
{
}
//+------------------------------------------------------------------+
//| 클래스 객체 생성 방법 |
//+------------------------------------------------------------------+
bool CMemoryControl::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);
//--- 라벨을 위한 문자열 준비
string str_physical="물리적 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_PHYSICAL)+" Mb";
string str_total="전체 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_TOTAL)+" Mb";
string str_available="사용가능 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)+" Mb";
string str_used="사용되는 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_USED)+" Mb";
//--- 라벨 생성
if(!CreateLabel(m_lbl_memory_physical,"physical_label",X_START+10,Y_START+5,str_physical,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_memory_total,"total_label",X_START+10,Y_START+30,str_total,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_memory_available,"available_label",X_START+10,Y_START+55,str_available,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_memory_used,"used_label",X_START+10,Y_START+80,str_used,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_array_type,"type_label",X_START+10,Y_START+105,"Array type = double",12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_array_size,"size_label",X_START+10,Y_START+130,"Array size = 0",12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_error,"error_label",X_START+10,Y_START+155,"",12,clrRed))
return(false);
if(!CreateLabel(m_lbl_change_type,"change_type_label",X_START+10,Y_START+185,"Change type",10,clrBlack))
return(false);
if(!CreateLabel(m_lbl_add_size,"add_size_label",X_START+10,Y_START+210,"Add to array",10,clrBlack))
return(false);
//--- 제어 요소 생성
if(!CreateButton(m_button_add,"add_button",X_START+15,Y_START+245,"Add",12,clrBlue))
return(false);
if(!CreateButton(m_button_free,"free_button",X_START+75,Y_START+245,"Free",12,clrBlue))
return(false);
if(!CreateComboBoxType())
return(false);
if(!CreateComboBoxStep())
return(false);
//--- 변수 초기화
m_arr_size=0;
//--- 실행 성공
return(true);
}
//+------------------------------------------------------------------+
//| 버튼 생성 |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateButton(CButton &button,const string name,const int x,
const int y,const string str,const int font_size,
const int clr)
{
//--- 버튼 생성
if(!button.Create(m_chart_id,name,m_subwin,x,y,x+50,y+20))
return(false);
//--- 텍스트
if(!button.Text(str))
return(false);
//--- 폰트 크기
if(!button.FontSize(font_size))
return(false);
//--- 라벨 색상
if(!button.Color(clr))
return(false);
//--- 제어 요소에 버튼을 추가
if(!Add(button))
return(false);
//--- 실행 성공
return(true);
}
//+------------------------------------------------------------------+
//| 배열 크기에 대한 콤보 상자 생성 |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateComboBoxStep(void)
{
//--- 콤보 상자 생성
if(!m_combo_box_step.Create(m_chart_id,"step_combobox",m_subwin,X_START+100,Y_START+185,X_START+200,Y_START+205))
return(false);
//--- 콤보 상자에 요소 추가
if(!m_combo_box_step.ItemAdd("100 000",100000))
return(false);
if(!m_combo_box_step.ItemAdd("1 000 000",1000000))
return(false);
if(!m_combo_box_step.ItemAdd("10 000 000",10000000))
return(false);
if(!m_combo_box_step.ItemAdd("100 000 000",100000000))
return(false);
//--- 현재 콤보 상자 요소를 설정
if(!m_combo_box_step.SelectByValue(1000000))
return(false);
//--- 콤보 상자를 제어 요소에 추가
if(!Add(m_combo_box_step))
return(false);
//--- 실행 성공
return(true);
}
//+------------------------------------------------------------------+
//| 배열 유형에 대한 콤보 상자 생성 |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateComboBoxType(void)
{
//--- 콤보 상자 생성
if(!m_combo_box_type.Create(m_chart_id,"type_combobox",m_subwin,X_START+100,Y_START+210,X_START+200,Y_START+230))
return(false);
//--- 콤보 상자에 요소 추가
if(!m_combo_box_type.ItemAdd("char",0))
return(false);
if(!m_combo_box_type.ItemAdd("int",1))
return(false);
if(!m_combo_box_type.ItemAdd("float",2))
return(false);
if(!m_combo_box_type.ItemAdd("double",3))
return(false);
if(!m_combo_box_type.ItemAdd("long",4))
return(false);
//--- 현재 콤보 상자 요소를 설정
if(!m_combo_box_type.SelectByValue(3))
return(false);
//--- 현재 콤보 상자 요소를 저장
m_combo_box_type_value=3;
//--- 콤보 상자를 제어 요소에 추가
if(!Add(m_combo_box_type))
return(false);
//--- 실행 성공
return(true);
}
//+------------------------------------------------------------------+
//| 라벨 생성 |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateLabel(CLabel &lbl,const string name,const int x,
const int y,const string str,const int font_size,
const int clr)
{
//--- 라벨 생성
if(!lbl.Create(m_chart_id,name,m_subwin,x,y,0,0))
return(false);
//--- 텍스트
if(!lbl.Text(str))
return(false);
//--- 폰트 크기
if(!lbl.FontSize(font_size))
return(false);
//--- 색상
if(!lbl.Color(clr))
return(false);
//--- 제어 요소에 라벨 추가
if(!Add(lbl))
return(false);
//--- 성공함
return(true);
}
//+------------------------------------------------------------------+
//| "추가" 버튼 이벤트 클릭 처리기 event |
//+------------------------------------------------------------------+
void CMemoryControl::OnClickButtonAdd(void)
{
//--- 배열 크기 증가
m_arr_size+=(int)m_combo_box_step.Value();
//--- 현재 배열에 대해 메모리 할당 시도
if(CurrentArrayAdd())
{
//--- 할당된 메모리, 화면에 현재 상태 표시
m_lbl_memory_available.Text("사용 가능 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)+" Mb");
m_lbl_memory_used.Text("사용되는 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_USED)+" Mb");
m_lbl_array_size.Text("배열 크기 = "+IntegerToString(m_arr_size));
m_lbl_error.Text("");
}
else
{
//--- 메모리 할당 실패, 오류 메시지 표시
m_lbl_error.Text("오류 발생, 배열이 너무 큽니다!");
//--- 이전 배열 크기를 반환
m_arr_size-=(int)m_combo_box_step.Value();
}
}
//+------------------------------------------------------------------+
//| "무료" 버튼 이벤트 처리기 |
//+------------------------------------------------------------------+
void CMemoryControl::OnClickButtonFree(void)
{
//--- 현재 배열의 메모리 최적화
CurrentArrayFree();
//--- 화면에 현재 상태를 표시
m_lbl_memory_available.Text("사용 가능한 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)+" Mb");
m_lbl_memory_used.Text("사용되는 메모리 = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_USED)+" Mb");
m_lbl_array_size.Text("배열 크기 = 0");
m_lbl_error.Text("");
}
//+------------------------------------------------------------------+
//| 콤보 상자 변경 이벤트 처리기 |
//+------------------------------------------------------------------+
void CMemoryControl::OnChangeComboBoxType(void)
{
//--- 배열 유형이 변경되었는지 확인
if(m_combo_box_type.Value()!=m_combo_box_type_value)
{
//--- 현재 배열의 메모리 최적화
OnClickButtonFree();
//--- 다른 배열 유형으로 작업
m_combo_box_type_value=(int)m_combo_box_type.Value();
//--- 화면에 새 배열 유형을 표시
if(m_combo_box_type_value==0)
m_lbl_array_type.Text("배열 유형 = char");
if(m_combo_box_type_value==1)
m_lbl_array_type.Text("배열 유형 = int");
if(m_combo_box_type_value==2)
m_lbl_array_type.Text("배열 유형 = float");
if(m_combo_box_type_value==3)
m_lbl_array_type.Text("배열 유형 = double");
if(m_combo_box_type_value==4)
m_lbl_array_type.Text("배열 유형 = long");
}
}
//--- CMemoryControl 클래스 개체
CMemoryControl ExtDialog;
//+------------------------------------------------------------------+
//| 엑스퍼트 초기화 기능 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 대화상자 생성
if(!ExtDialog.Create(0,"MemoryControl",0,X_START,Y_START,X_SIZE,Y_SIZE))
return(INIT_FAILED);
//--- 시작
ExtDialog.Run();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 엑스퍼트 초기화 해제 기능 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ExtDialog.Destroy(reason);
}
//+------------------------------------------------------------------+
//| 엑스퍼트 차트 이벤트 기능 |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
ExtDialog.ChartEvent(id,lparam,dparam,sparam);
}
|