Get text from window

 

Hi all!

There is code of script which get the name of chart window in MetaTrader 4 terminal (e.g. "EURUSD,M1"):

#import "user32.dll"
   int GetParent(int hWnd);
   int GetWindow(int hWnd, int uCmd);
   int GetWindowTextA(int hWnd, string str, int nMaxCount);
   int GetWindowTextLengthA(int i);
#import

#define GW_CHILD 0x5

void start()
{
   int MDI_handle = GetParent(GetParent(WindowHandle(Symbol(), Period())));
   
   int Chart_board = GetWindow(MDI_handle, GW_CHILD);
   string s = "                             ";
   int nMaxCount = GetWindowTextLengthA(Chart_board);
   GetWindowTextA(Chart_board, s, nMaxCount + 1);
   Print(s);
}

These script worked with MT4 builds older then 2014 year. But with 2014 year's builds this script doesn't work. It just prints spaces.

Please, help me to get name of chart in MT4 terminal by its handle.

 

strings in the new build (600 >) are unicode

adapt your code accordingly

 
qjol:

strings in the new build (600 >) are unicode

adapt your code accordingly


thanks!


so, solution is to use GetWindowTextW and "&" with string

 
Vitalya Antonov:

thanks!


so, solution is to use GetWindowTextW and "&" with string

It's been many years since you made your post, but I am now struggling with this same problem. I'm using the same code as above to get the name of chart window in MetaTrader 4 terminal (e.g. "EURUSD,M1"). I put "&" with string in the function imported from user32.dll:

int GetWindowTextW(int hWnd, string & lpString, int nMaxCount)

But when I execute your code in the first post I get this text:  For Help, press F1

How do I target the correct title text of the chart eg "EURUSD,M1"?

 

Hi,

If you want to get the chart window text which is opened like "EURUSD,H1" try this code:

#include <WinUser32.mqh>
//+------------------------------------------------------------------+
void start()
  {
   int MDI_handle=GetParent(WindowHandle(Symbol(),Period()));

   char s_char[];
   int nMaxCount=(int)GetWindowTextLengthW(MDI_handle);
   ArrayResize(s_char,nMaxCount+1);
   GetWindowTextA(MDI_handle,s_char,nMaxCount+1);

   Print("Return Text : "+ CharArrayToString(s_char));
  }

The return text value will be below (it's in backtest visual mode) :

Return Text : EURUSD,H1 (visual)


Or if you want to get Terminal MT4 text try this code:
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
void init()
  {
   int MDI_handle=GetParent(GetParent(GetParent(WindowHandle(Symbol(),Period()))));

   char s_char[];
   int nMaxCount=(int)GetWindowTextLengthW(MDI_handle);
   ArrayResize(s_char,nMaxCount+1);
   GetWindowTextA(MDI_handle,s_char,nMaxCount+1);

   Print("Return Text : "+CharArrayToString(s_char));
  }

The return text value will be below (it's in backtest visual mode) :

Return Text : 9988567: Alpari Limited MT4 - [EURUSD,H1 (visual)]

Regards!

 
Mehrdad Jeddi:

Hi,

If you want to get the chart window text which is opened like "EURUSD,H1" try this code:

The return text value will be below (it's in backtest visual mode) :

...

Or if you want to get Terminal MT4 text try this code:

The return text value will be below (it's in backtest visual mode) :

...

Regards!

Many, many thanks! This is my first post on mql5.com. It's heartwarming to see people who are willing to help.

That's one less stress in my life. That double "GetParent" followed by "GetWindow" in the first post was tripping me up. I'll stick to GetWindowTextA and char array. GetWindowTextW is too confusing.
 

After a little more fiddling, I figured out the correct usage of GetWindowTextW.

To get title text of a chart:

#import "user32.dll"
   int GetParent(int hWnd);
   int GetWindowTextW(int hWnd, string & lpString, int nMaxCount);
   int GetWindowTextLengthW(int i);
#import

void start()
{
   //Getting chart window handle, getting title of current chart using GetWindowTextW
   int MDI_handle = GetParent(WindowHandle(Symbol(), Period()));  //single GetParent to get the chart's handle
   string s = "";
   int nMaxCount=(int)GetWindowTextLengthW(MDI_handle);
   for (int i = 0; i < nMaxCount; i++) s += " ";  //extend string s by nMaxCount spaces     
   GetWindowTextW(MDI_handle,s,nMaxCount+1);
   Print("Return Text : ",s);
}

And to get title text of the MT4 terminal:

#import "user32.dll"
   int GetParent(int hWnd);
   int GetWindowTextW(int hWnd, string & lpString, int nMaxCount);
   int GetWindowTextLengthW(int i);
#import

void start()
{
   //Getting terminal window handle, getting title of terminal using GetWindowTextW
   int MDI_handle = GetParent(GetParent(GetParent(WindowHandle(Symbol(), Period()))));  //Three GetParent's to get terminal's window handle
   string s = "";
   int nMaxCount=(int)GetWindowTextLengthW(MDI_handle);
   for (int i = 0; i < nMaxCount; i++) s += " ";    //extend string s by nMaxCount spaces  
   GetWindowTextW(MDI_handle,s,nMaxCount+1);
   Print("Return Text : ",s);
}

Thanks for the inspiration Mehrdad Jeddi!

 
shoxie:

After a little more fiddling, I figured out the correct usage of GetWindowTextW.

To get title text of a chart:

And to get title text of the MT4 terminal:

Thanks for the inspiration Mehrdad Jeddi!

You're welcome,

Yes,your code is correct,The GetWindowTextW will be used for UNICODE string,But the GetWindowTextA will be used for ANSI code that will be spent to char array variable

then will be converted to a string UNICODE variable,

Reason: