Question about program a script, for an object, how to know which window the object lies on?

 
ObjectGet() can't return which window the object belongs to,
when a script run on a subwindow, it even has no way to know which window it runs on? the subwindow's shortname? WindowOnDropped() only return the window id when using mouse dragging, thus it always return 0 when use hotkey to activate the script.
 
Hello raidsan,

Here is a possible workaround...

Define these three global variables in your expert, script or indicator.

int App.WindowIndex;
int App.WindowHandle;
string App.Name;

In the init() function do the following...

1. Set App.WindowIndex to 0 if the code is for a script or an expert. If the code is for an indicator then set App.WindowIndex to -1. Don't bother trying to use WindowFind in the init() function since it will always return -1.

2. Set App.Name to the name of your app, i.e. App.Name = "myapp";

3. If your app is an indicator then call SetIndicatorShortName(App.Name);

4. Call App.WindowHandle = WindowHandle (Symbol(), Period());

Note: WindowHandle() may return 0 if your app is an indicator or expert attached to a chart and you have just launched your MT4 terminal and the charts are in the process of being drawn for the first time. Don't worry though because you can set App.WindowHandle in the start() function (see below).


At the beginning ot your start() function include the following code...


////////////////////////////////////////////////////////////////////////////////

int window_index;

if (App.WindowIndex != 0) {
window_index = WindowFind(App.Name);
if (window_index <= 0) {
Print("App.WindowIndex="+App.WindowIndex+" is <= 0; App.Name=["+App.Name+"], exiting application");
return;
}
if (window_index != App.WindowIndex) {
App.WindowIndex = window_index;
Print("App.Name="+App.Name+", App.WindowIndex="+App.WindowIndex);
}
}


if (App.WindowHandle == 0) {
App.WindowHandle = WindowHandle(Symbol(), Period());
if (App.WindowHandle == 0) {
Print("App.WindowHandle is ZERO; Symbol="+Symbol()+" Period="+Period()+", exiting application");
return;
}
}

////////////////////////////////////////////////////////////////////////////////

You can then use App.WindowIndex and App.WindowHandle in the rest of your code.

Hope this helps.

Regards,

Laurence.
 
Thanks for answer, but it not solve my question.
The situation I want to do with just like this:

Backgroud:
* working on a RSI subwindow
* I draw some trendline on the RSI subwindow

Application:

* App1: an auto period visual property setting script
1. the user run a script on the RSI subwindow
2.1 the script find the trendline object in the subwindow,
2.2 the script set the object's visual property to visible only in current period.

* App2: trendline cross alert indicator
1. the user lauch the indicator, give an unique alert number;
2. the indicator draw a trendline on the target window, the trendline name with the unique alert number ;
3. the user move the trendline to a position her wanted.
4. the indicator then monitor the RSI line goes, when find it cross the trendline, do alert.


* Problem:
when work on a drawing object , can't recognise which subwindow it lies on!
 

* Problem:
when work on a drawing object , can't recognise which subwindow it lies on!


Scripts always run on the main chart window. It doesn't matter how many sub windows you have, if you drag a script onto a sub window it is effectively running on the main chart window. You can only have 1 script running on a chart irrespective of how many subwindows are on the chart.

WindowHandle() will return the same value for the main chart window and all sub windows on the chart.

If you know the name of the indicator window and you know the name of the object you are looking for then why can't you use WindowFind(indicator_name) to get the WindowIndex and then pass that value to ObjectGet along with the name of the object you are looking for.



Do you have a code sample you could post?

Regards,

Laurence.
 
Pseudo-code &#65306;

int windid = WindowHandle("RSI(14)");

for all obj in ObjectsTotal() loop:
{
if obj is in RSI subwindow then //?????????? How can I know that ?
{
if obj is a trendline
{
//do some thing with the obj
......
}
}
}
 
* Problem:
when work on a drawing object , can't recognise which subwindow it lies on!



int ObjectFind( string name)
Search for an object having the specified name. The function returns index of the windows that contains the object to be found. If it fails, the returned value will be -1. To get the detailed error information, one has to call the GetLastError() function. The chart sub-windows (if there are sub-windows with indicators in the chart) are numbered starting from 1. The chart main window always exists and has the 0 index.
 
oh, thank you, I see now, it is so kind of you to give me the answer.
Reason: