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(text1text_array1) || !SplitTextIntoWords(text2text_array2) || !SplitTextIntoWords(text3text_array3))
      return;
      
//--- 현재 차트 ID
   long   chart_idChartID();
   
//--- 그래픽 리소스의 매개변수를 선언합니다.
   uint   rc_width =(int)ChartGetInteger(chart_idCHART_WIDTH_IN_PIXELS); 
   uint   rc_height=(int)ChartGetInteger(chart_idCHART_HEIGHT_IN_PIXELS); 
   uint   rc_data[]; 
   uint   rc_size=rc_width*rc_height;
  
//--- 텍스트 출력을 위한 그래픽 리소스 생성
   if(!CreateResource(chart_idrc_datarc_widthrc_height))
      return;
   
//--- 너비와 높이로 공백 문자의 크기를 가져옵니다
   int space_w=0space_h=0;
   if(!TextGetSize(" "space_wspace_h))
     {
      PrintFormat("%s: TextGetSize() failed. Error code ",__FUNCTION__GetLastError()); 
      return
     }
   
//--- 문자열 사이의 수직 들여쓰기를 2만큼 늘리고 차트에 세 개의 배열에서 텍스트를 표시합니다.
   space_h+=2;
   TextArrayToChart(1text_array1COORD_XCOORD_Y+space_h*0space_wrc_datarc_widthrc_height);
   TextArrayToChart(2text_array2COORD_XCOORD_Y+space_h*1space_wrc_datarc_widthrc_height);
   TextArrayToChart(3text_array3COORD_XCOORD_Y+space_h*2space_wrc_datarc_widthrc_height);
   
//--- 모든 텍스트가 표시된 후 리소스 데이터를 업데이트합니다
   Update(RES_NAMErc_datarc_widthrc_heighttrue);
   
//--- 5초간 기다린 후 리소스를 해제하고 그래픽 객체를 삭제합니다.
      Sleep(5000);
   ResourceFree(RES_NAME);
   ObjectDelete(chart_idOBJ_NAME);
   /*
   스크립트 실행 결과로 차트에 세 개의 텍스트 문자열이 표시됩니다
   각 문자열의 각 개별 단어는 이전 단어와의 거리를 두고 표시됩니다
이전 단어의 텍스트 너비와 동일합니다.  TextGetSize(); 함수를 사용합니다 
   저널에는 각 문자열의 모든 단어와 크기가 포함됩니다
   Text array 1:
   [0word"This"width=29height=18
   [1word"is"width=12height=18
   [2word"the"width=21height=18
   [3word"first"width=25height=18
   [4word"line"width=24height=18
   [5word"of"width=13height=18
   [6word"text"width=24height=18
   Text array 2:
   [0word"The"width=26height=18
   [1word"second"width=51height=18
   [2word"line"width=24height=18
   [3word"also"width=29height=18
   [4word"contains"width=58height=18
   [5word"text"width=24height=18
   Text array 3:
   [0word"Each"width=36height=18
   [1word"word"width=34height=18
   [2word"in"width=12height=18
   [3word"each"width=34height=18
   [4word"line"width=24height=18
   [5word"has"width=25height=18
   [6word"its"width=16height=18
   [7word"own"width=28height=18
   [8word"size"width=28height=18
   */ 
  }
//+----------------------------------------------------------------------------+
//| 공백 구분 기호(" ")를 사용하여 문자열을 단어 배열로 분할 합니다|                  |
//+----------------------------------------------------------------------------+
bool SplitTextIntoWords(const string textstring &array[])
  {
   ResetLastError();
   if(StringSplit(textStringGetCharacter(" "0), array)<0)
     {
      PrintFormat("%s: StringSplit() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
   return(true);
  }
/+------------------------------------------------------------------+
//| 배열로 부터 텍스트를 차트에 표시                                    |
/+------------------------------------------------------------------+
void TextArrayToChart(int array_numstring &array[], const int text_xconst int text_yint space_wuint &pixel_data[], const uint res_widthconst uint res_height)
  {
   int width=0height=0;  // 텍스트 너비와 높이
   int x=text_x;           // 출력된 텍스트의 X 좌표
   
//--- 처리된 단어 배열의 이름이 포함된 헤더를 인쇄합니다
   Print("Text array "array_num,":");
   
//--- 단어 배열에 의한 루프에서
   int total=(int)array.Size();
   for(int i=0i<totali++)
     {
//--- 다음 단어를 가져와 차트로 보냅니다(리소스 픽셀 배열에 그립니다)
      string word=array[i];
      TextOut(wordxtext_yANCHOR_LEFT_UPPERpixel_datares_widthres_heightColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
      
//--- 현재 단어의 텍스트 크기를 가져옵니다
      ResetLastError();
      if(!TextGetSize(wordwidthheight))
        {
         PrintFormat("%s: TextGetSize(\"%s\") failed. Error code ",__FUNCTION__wordGetLastError()); 
         continue
        }
//--- 저널의 텍스트 데이터(단어, 너비, 높이)를 출력합니다.
//--- 그런 다음 단어의 X 좌표를 (단어 너비) + (공백 너비)만큼 증가시킵니다.
      PrintFormat("[%d] word: \"%s\", width=%d, height=%d",iwordwidthheight);
      x+=width+space_w;
     }
  }
/+------------------------------------------------------------------+
//| 전체 차트에 대한 그래픽 리소스를 만듭니다                            |
/+------------------------------------------------------------------+
bool CreateResource(const long chart_iduint &pixel_data[], const uint widthconst uint height)
  {
//--- 픽셀 배열의 크기를 설정합니다
   ResetLastError(); 
   uint size=width*height;
   if(ArrayResize(pixel_datasize)!=size
     { 
      PrintFormat("%s: ArrayResize() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     } 
//--- 픽셀 배열을 투명한 색상으로 채우고 이를 기반으로 그래픽 리소스를 만듭니다.
   ArrayInitialize(pixel_data0x00FFFFFF); 
   if(!ResourceCreate(RES_NAMEpixel_datawidthheight000COLOR_FORMAT_ARGB_NORMALIZE)) 
     { 
      PrintFormat("%s: ResourceCreate() failed. Error code ",__FUNCTION__GetLastError()); 
      return(false); 
     } 
  
//--- 차트의 왼쪽 상단 모서리 좌표에 그래픽 레이블 객체를 생성합니다.
   if(!ObjectCreate(0OBJ_NAMEOBJ_BITMAP_LABEL000)) 
     { 
      PrintFormat("%s: ObjectCreate() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     } 
//--- 생성된 비트맵 객체의 너비와 높이를 그래픽 리소스의 너비와 높이와 같게 설정합니다.
//--- 객체 앵커 포인트를 중앙으로 설정합니다.
   if(!ObjectSetInteger(chart_idOBJ_NAMEOBJPROP_XSIZEwidth))
     {
      PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
   if(!ObjectSetInteger(chart_idOBJ_NAMEOBJPROP_YSIZEheight))
     {
      PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
//--- 이전에 비트맵 객체에 대해 생성된 그래픽 리소스를 이미지 파일로 지정합니다
//--- 이 경우 사용된 그래픽 리소스의 이름을 나타내기 위해 이름 앞에 "::"를 추가해야 합니다
   if(!ObjectSetString(chart_idOBJ_NAMEOBJPROP_BMPFILE"::"+RES_NAME))
     {
      PrintFormat("%s: ObjectSetString() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
    
//--- 모든게 작동합니다
   return(true);
  }
/+------------------------------------------------------------------+
//| 그래픽 리소스 데이터를 업데이트 합니다                               |
/+------------------------------------------------------------------+
void Update(const string res_nameconst uint &pixel_data[], const uint widthconst uint heightconst bool redraw
  { 
//--- 0차원이 전달되면 그대로 둡니다 
   if(width==0 || height==0
      return
//--- 리소스 데이터를 업데이트하고 차트를 다시 그립니다
   if(ResourceCreate(res_namepixel_datawidthheight000COLOR_FORMAT_ARGB_NORMALIZE) && redraw
      ChartRedraw(); 
  } 

 

참고 항목

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