TextGetSize

이 함수는 현재 폰트 설정에서 선 너비 및 높이를 반환합니다.

bool  TextGetSize(
   const string       text,          // 텍스트 문자열
   uint&               width,        // 버퍼 너비(픽셀)
   uint&               height        // 버퍼 높이(픽셀)
   );

Parameter

텍스트

[in]  길이와 너비를 구해야 하는 문자열.

width

[out]  폭 수신에 대한 입력 매개 변수.

height

[out]  높이 수신에 대한 입력 매개 변수.

반환값

성공하면 true를 반환하고 그렇지 않으면 false를 반환합니다. 가능한 코드 에러:

  • ERR_INTERNAL_ERROR(4001) - 운영 체제 에러.

 

예:

#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"
 
/+------------------------------------------------------------------+
//| Script program start function                                   |
/+------------------------------------------------------------------+
void OnStart()
  {
//--- 차트에 출력할 세 줄의 텍스트를 준비합니다
   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에서 단어 집합을 가져오는 배열
   
//--- 단어 배열 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 ",__FUNCTION__, GetLastError()); 
      return; 
     }
   
//--- 문자열 사이의 수직 들여쓰기를 2만큼 늘리고 차트에 세 개의 배열에서 텍스트를 표시합니다.
   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초간 기다린 후 리소스를 해제하고 그래픽 객체를 삭제합니다.
      Sleep(5000);
   ResourceFree(RES_NAME);
   ObjectDelete(chart_id, OBJ_NAME);
   /*
   스크립트 실행 결과로 차트에 세 개의 텍스트 문자열이 표시됩니다
   각 문자열의 각 개별 단어는 이전 단어와의 거리를 두고 표시됩니다
이전 단어의 텍스트 너비와 동일합니다.  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 ",__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 ",__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) 
  { 
//--- 0차원이 전달되면 그대로 둡니다 
   if(width==0 || height==0) 
      return; 
//--- 리소스 데이터를 업데이트하고 차트를 다시 그립니다
   if(ResourceCreate(res_name, pixel_data, width, height, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE) && redraw) 
      ChartRedraw(); 
  } 

 

참고 항목

Resources, ResourceCreate(), ResourceSave(), TextSetFont(), TextOut()