#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define COORD_X 200
#define COORD_Y 100
#define OBJ_NAME "TestTextGetSizeBitmapLabel"
#define RES_NAME "TestTextGetSizeResource"
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- チャートに出力する3行のテキストを準備する
string text1="This is the first line of text";
string text2="The second line also contains text";
string text3="Each word in each line has its own size";
string text_array1[]; // 文字列1から単語の集合を取得する配列
string text_array2[]; // 文字列2から単語の集合を取得する配列
string text_array3[]; // 文字列3から単語の集合を取得する配列
//--- 単語の配列に書き込む
if(!SplitTextIntoWords(text1, text_array1) || !SplitTextIntoWords(text2, text_array2) || !SplitTextIntoWords(text3, text_array3))
return;
//--- 現在のチャートID
long chart_id= ChartID();
//--- グラフィックリソースのパラメータを宣言する
uint rc_width =(int)ChartGetInteger(chart_id, CHART_WIDTH_IN_PIXELS);
uint rc_height=(int)ChartGetInteger(chart_id, CHART_HEIGHT_IN_PIXELS);
uint rc_data[];
uint rc_size=rc_width*rc_height;
//--- 文字出力用のグラフィカルリソースを作成する
if(!CreateResource(chart_id, rc_data, rc_width, rc_height))
return;
//--- スペース文字の幅と高さを取得する
int space_w=0, space_h=0;
if(!TextGetSize(" ", space_w, space_h))
{
PrintFormat("%s: TextGetSize() failed. Error code %d",__FUNCTION__, GetLastError());
return;
}
//--- 文字列間の縦方向のインデントを2増やし、3つの配列からテキストをグラフにプロットする
space_h+=2;
TextArrayToChart(1, text_array1, COORD_X, COORD_Y+space_h*0, space_w, rc_data, rc_width, rc_height);
TextArrayToChart(2, text_array2, COORD_X, COORD_Y+space_h*1, space_w, rc_data, rc_width, rc_height);
TextArrayToChart(3, text_array3, COORD_X, COORD_Y+space_h*2, space_w, rc_data, rc_width, rc_height);
//--- すべてのテキストが表示された後、リソースデータを更新する
Update(RES_NAME, rc_data, rc_width, rc_height, true);
//--- 5秒待ってから、リソースをリリースsひ、グラフィックオブジェクトを削除する
Sleep(5000);
ResourceFree(RES_NAME);
ObjectDelete(chart_id, OBJ_NAME);
/*
スクリプト実行の結果、チャート上に3つの文字列が表示される
各文字列内の個々の単語は、前の単語から一定の距離に表示される
この距離は、前の単語のテキストの幅をTextGetSize()関数を使って取得した値に等しくなる
操作ログには、各文字列のすべての単語とそのサイズが記録される
Text array 1:
[0] word: "This", width=29, height=18
[1] word: "is", width=12, height=18
[2] word: "the", width=21, height=18
[3] word: "first", width=25, height=18
[4] word: "line", width=24, height=18
[5] word: "of", width=13, height=18
[6] word: "text", width=24, height=18
Text array 2:
[0] word: "The", width=26, height=18
[1] word: "second", width=51, height=18
[2] word: "line", width=24, height=18
[3] word: "also", width=29, height=18
[4] word: "contains", width=58, height=18
[5] word: "text", width=24, height=18
Text array 3:
[0] word: "Each", width=36, height=18
[1] word: "word", width=34, height=18
[2] word: "in", width=12, height=18
[3] word: "each", width=34, height=18
[4] word: "line", width=24, height=18
[5] word: "has", width=25, height=18
[6] word: "its", width=16, height=18
[7] word: "own", width=28, height=18
[8] word: "size", width=28, height=18
*/
}
//+----------------------------------------------------------------------------+
//| スペース区切り(" ")を使って文字列を単語の配列に分割する |
//+----------------------------------------------------------------------------+
bool SplitTextIntoWords(const string text, string &array[])
{
ResetLastError();
if(StringSplit(text, StringGetCharacter(" ", 0), array)<0)
{
PrintFormat("%s: StringSplit() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
return(true);
}
//+------------------------------------------------------------------+
//--- チャート上に配列からテキストを表示する |
//+------------------------------------------------------------------+
void TextArrayToChart(int array_num, string &array[], const int text_x, const int text_y, int space_w, uint &pixel_data[], const uint res_width, const uint res_height)
{
int width=0, height=0; // テキストの幅と高さ
int x=text_x; // テキスト出力のX座標
//--- ヘッダと処理された単語配列の名前を出力する
Print("Text array ", array_num,":");
//--- 単語の配列のループ
int total=(int)array.Size();
for(int i=0; i<total; i++)
{
//--- 次の単語を取得してチャートに送る(リソースのピクセル配列に描画する)
string word=array[i];
TextOut(word, x, text_y, ANCHOR_LEFT_UPPER, pixel_data, res_width, res_height, ColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
//--- 現在の単語のテキストサイズを取得する
ResetLastError();
if(!TextGetSize(word, width, height))
{
PrintFormat("%s: TextGetSize(\"%s\") failed. Error code %d",__FUNCTION__, word, GetLastError());
continue;
}
//--- 操作ログにテキストデータを出力する(単語、その幅と高さ)
//--- そのあと、次の単語のX座標を(単語の幅)+(スペースの幅)だけ増やす
PrintFormat("[%d] word: \"%s\", width=%d, height=%d",i, word, width, height);
x+=width+space_w;
}
}
//+------------------------------------------------------------------+
//| チャート全体用のグラフィカルリソースを作成する |
//+------------------------------------------------------------------+
bool CreateResource(const long chart_id, uint &pixel_data[], const uint width, const uint height)
{
//--- ピクセル配列のサイズを設定する
ResetLastError();
uint size=width*height;
if(ArrayResize(pixel_data, size)!=size)
{
PrintFormat("%s: ArrayResize() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- ピクセル配列を透明色で塗りつぶし、それに基づいてグラフィック リソースを作成する
ArrayInitialize(pixel_data, 0x00FFFFFF);
if(!ResourceCreate(RES_NAME, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE))
{
PrintFormat("%s: ResourceCreate() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- チャート左上隅の座標にグラフィックラベルオブジェクトを作成する
if(!ObjectCreate(0, OBJ_NAME, OBJ_BITMAP_LABEL, 0, 0, 0))
{
PrintFormat("%s: ObjectCreate() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- 作成されたビットマップ オブジェクトの幅と高さを、グラフィックリソースの幅と高さに等しく設定する
//--- オブジェクトのアンカーポイントをその中心に設定する
if(!ObjectSetInteger(chart_id, OBJ_NAME, OBJPROP_XSIZE, width))
{
PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
if(!ObjectSetInteger(chart_id, OBJ_NAME, OBJPROP_YSIZE, height))
{
PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- ビットマップオブジェクト用に以前に作成したグラフィックリソースを画像ファイルとして指定する
//--- この場合、使用されるグラフィックリソースの名前を示すために、その名前の前に「::」を追加する必要がある
if(!ObjectSetString(chart_id, OBJ_NAME, OBJPROP_BMPFILE, "::"+RES_NAME))
{
PrintFormat("%s: ObjectSetString() failed. Error code %d",__FUNCTION__, GetLastError());
return(false);
}
//--- すべてが成功
return(true);
}
//+------------------------------------------------------------------+
//| グラフィックリソースデータを更新する |
//+------------------------------------------------------------------+
void Update(const string res_name, const uint &pixel_data[], const uint width, const uint height, const bool redraw)
{
//--- ゼロ次元が渡された場合は終了する
if(width==0 || height==0)
return;
//--- リソース データを更新し、チャートを再描画する
if(ResourceCreate(res_name, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE) && redraw)
ChartRedraw();
}
|