//--- 説明
#property description "Script creates \"Label\" graphical object."
//--- スクリプトの起動時に入力パラメータのウィンドウを表示する
#property script_show_inputs
//--- スクリプト入力パラメータ
input string InpName="Label"; // ラベル名
input int InpX=150; // X 軸距離
input int InpY=150; // Y 軸距離
input string InpFont="Arial"; // フォント
input int InpFontSize=14; // フォントサイズ
input color InpColor=clrRed; // 色
input double InpAngle=0.0; // 傾斜角度
input ENUM_ANCHOR_POINT InpAnchor=ANCHOR_CENTER; // アンカーの種類
input bool InpBack=false; // 背景オブジェクト
input bool InpSelection=true; // 強調表示して移動
input bool InpHidden=true; // オブジェクトリストに隠す
input long InpZOrder=0; // マウスクリックの優先順位
//+------------------------------------------------------------------+
//| ラベルを作成する |
//+------------------------------------------------------------------+
bool LabelCreate(const long chart_ID=0, // チャート識別子
const string name="Label", // ラベル名
const int sub_window=0, // サブウィンドウ番号
const int x=0, // X 座標
const int y=0, // Y 座標
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // アンカーに使用されるチャートのコーナー
const string text="Label", // テキスト
const string font="Arial", // フォント
const int font_size=10, // フォントサイズ
const color clr=clrRed, // 色
const double angle=0.0, // テキストの傾斜
const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // アンカーの種類
const bool back=false, // 背景で表示する
const bool selection=false, // 強調表示して移動
const bool hidden=true, // オブジェクトリストに隠す
const long z_order=0) // マウスクリックの優先順位
{
//--- エラー値をリセットする
ResetLastError();
//--- ラベルを作成する
if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
{
Print(__FUNCTION__,
": failed to create text label! Error code = ",GetLastError());
return(false);
}
//--- ラベル座標を設定する
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- ポイント座標が相対的に定義されているチャートのコーナーを設定
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- テキストを設定する
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- テキストフォントを設定する
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- フォントサイズを設定する
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- テキストの傾斜を設定する
ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- アンカーの種類を設定
ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- 色を設定
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- 前景(false)または背景(true)に表示
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- マウスでラベルを移動させるモードを有効(true)か無効(false)にする
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- オブジェクトリストのグラフィックオブジェクトを非表示(true)か表示(false)にする
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- チャートのマウスクリックのイベントを受信するための優先順位を設定する
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- 実行成功
return(true);
}
//+------------------------------------------------------------------+
//| ラベルを移動する |
//+------------------------------------------------------------------+
bool LabelMove(const long chart_ID=0, // チャート識別子
const string name="Label", // ラベル名
const int x=0, // X 座標
const int y=0) // Y 座標
{
//--- エラー値をリセットする
ResetLastError();
//--- ラベルを移動する
if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))
{
Print(__FUNCTION__,
": failed to move X coordinate of the label! Error code = ",GetLastError());
return(false);
}
if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))
{
Print(__FUNCTION__,
": failed to move Y coordinate of the label! Error code = ",GetLastError());
return(false);
}
//--- 実行成功
return(true);
}
//+------------------------------------------------------------------+
//| ラベル結合に使用されるチャートのコーナーを変更する |
//+------------------------------------------------------------------+
bool LabelChangeCorner(const long chart_ID=0, // チャート識別子
const string name="Label", // ラベル名
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER) // アンカーに使用されるチャートのコーナー
{
//--- エラー値をリセットする
ResetLastError();
//--- アンカーに使用される隅を変更する
if(!ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner))
{
Print(__FUNCTION__,
": failed to change the anchor corner! Error code = ",GetLastError());
return(false);
}
//--- 実行成功
return(true);
}
//+------------------------------------------------------------------+
//| ラベルテキストを変更する |
//+------------------------------------------------------------------+
bool LabelTextChange(const long chart_ID=0, // チャート識別子
const string name="Label", // オブジェクト名
const string text="Text") // テキスト
{
//--- エラー値をリセットする
ResetLastError();
//--- オブジェクトのテキストを変更する
if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))
{
Print(__FUNCTION__,
": failed to change the text! Error code = ",GetLastError());
return(false);
}
//--- 実行成功
return(true);
}
//+------------------------------------------------------------------+
//| ラベルを削除する |
//+------------------------------------------------------------------+
bool LabelDelete(const long chart_ID=0, // チャート識別子
const string name="Label") // ラベル名
{
//--- エラー値をリセットする
ResetLastError();
//--- ラベルを削除する
if(!ObjectDelete(chart_ID,name))
{
Print(__FUNCTION__,
": failed to delete a text label! Error code = ",GetLastError());
return(false);
}
//--- 実行成功
return(true);
}
//+------------------------------------------------------------------+
//| スクリプトプログラムを開始する関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- ラベルの座標をローカル変数に収納する
int x=InpX;
int y=InpY;
//--- チャートウィンドウサイズ
long x_distance;
long y_distance;
//--- ウィンドウサイズを設定する
if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))
{
Print("Failed to get the chart width! Error code = ",GetLastError());
return;
}
if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))
{
Print("Failed to get the chart height! Error code = ",GetLastError());
return;
}
//--- 入力パラメータの正しさをチェックする
if(InpX<0 || InpX>x_distance-1 || InpY<0 || InpY>y_distance-1)
{
Print("Error! Incorrect values of input parameters!");
return;
}
//--- ラベルの初期テキストを準備する
string text;
StringConcatenate(text,"Upper left corner: ",x,",",y);
//--- チャートにラベルを作成する
if(!LabelCreate(0,InpName,0,InpX,InpY,CORNER_LEFT_UPPER,text,InpFont,InpFontSize,
InpColor,InpAngle,InpAnchor,InpBack,InpSelection,InpHidden,InpZOrder))
{
return;
}
//--- チャートを再描画して1 秒待つ
ChartRedraw();
Sleep(500);
//--- ラベルを移動すると同時に、そのテキストを変更する
//--- 軸による反復回数
int h_steps=(int)(x_distance/2-InpX);
int v_steps=(int)(y_distance/2-InpY);
//--- ラベルを下に移動する
for(int i=0;i<v_steps;i++)
{
//--- 座標を変更する
y+=2;
//--- ラベルを移動してテキストを変更する
MoveAndTextChange(x,y,"Upper left corner: ");
}
//--- 半秒の遅れ
Sleep(500);
//--- ラベルを右に移動する
for(int i=0;i<h_steps;i++)
{
//--- 座標を変更する
x+=2;
//--- ラベルを移動してテキストを変更する
MoveAndTextChange(x,y,"Upper left corner: ");
}
//--- 半秒の遅れ
Sleep(500);
//--- ラベルを上に移動する
for(int i=0;i<v_steps;i++)
{
//--- 座標を変更する
y-=2;
//--- ラベルを移動してテキストを変更する
MoveAndTextChange(x,y,"Upper left corner: ");
}
//--- 半秒の遅れ
Sleep(500);
//--- ラベルを左に移動する
for(int i=0;i<h_steps;i++)
{
//--- 座標を変更する
x-=2;
//--- ラベルを移動してテキストを変更する
MoveAndTextChange(x,y,"Upper left corner: ");
}
//--- 半秒の遅れ
Sleep(500);
//--- アンカーに使用される隅を変更してポイントを移動する
//--- 左下の隅に移動する
if(!LabelChangeCorner(0,InpName,CORNER_LEFT_LOWER))
return;
//--- ラベルテキストを変更する
StringConcatenate(text,"Lower left corner: ",x,",",y);
if(!LabelTextChange(0,InpName,text))
return;
//--- チャートを再描画して 2 秒待つ
ChartRedraw();
Sleep(2000);
//--- 右下の隅に移動する
if(!LabelChangeCorner(0,InpName,CORNER_RIGHT_LOWER))
return;
//--- ラベルテキストを変更する
StringConcatenate(text,"Lower right corner: ",x,",",y);
if(!LabelTextChange(0,InpName,text))
return;
//--- チャートを再描画して 2 秒待つ
ChartRedraw();
Sleep(2000);
//--- 右上の隅に移動する
if(!LabelChangeCorner(0,InpName,CORNER_RIGHT_UPPER))
return;
//--- ラベルテキストを変更する
StringConcatenate(text,"Upper right corner: ",x,",",y);
if(!LabelTextChange(0,InpName,text))
return;
//--- チャートを再描画して 2 秒待つ
ChartRedraw();
Sleep(2000);
//--- 左上の隅に移動する
if(!LabelChangeCorner(0,InpName,CORNER_LEFT_UPPER))
return;
//--- ラベルテキストを変更する
StringConcatenate(text,"Upper left corner: ",x,",",y);
if(!LabelTextChange(0,InpName,text))
return;
//--- チャートを再描画して 2 秒待つ
ChartRedraw();
Sleep(2000);
//--- ラベルを削除する
LabelDelete(0,InpName);
//--- チャートを再描画して1 秒待つ
ChartRedraw();
Sleep(500);
//---
}
//+------------------------------------------------------------------+
//| この関数はオブジェクトを移動してそのテキストを変更する |
//+------------------------------------------------------------------+
bool MoveAndTextChange(const int x,const int y,string text)
{
//--- ラベルを移動する
if(!LabelMove(0,InpName,x,y))
return(false);
//--- ラベルテキストを変更する
StringConcatenate(text,text,x,",",y);
if(!LabelTextChange(0,InpName,text))
return(false);
//--- スクリプトの動作が強制的に無効にされているかどうかをチェックする
if(IsStopped())
return(false);
//--- チャートを再描画する
ChartRedraw();
// 0.01 秒の遅れ
Sleep(10);
//--- 関数を終了する
return(true);
}
|