Get Window ID in AFL?

I'm wondering if there's any way to get a Window ID in AFL? I'm using static variables to pass information between different panes in the same window, but I want to isolate the variables to the same window. This way, if there are multiple windows open, the variables won't cross-contaminate.

Two solutions I came up with are:

  1. Create a Window ID parameter and enter that same parameter in all the panes within the same window. I don't like this approach because then I have to manage it manually.

  2. Use the OLE object to get the SelectedTab property. This isn't bad, but AmiBroker displays a warning message about performance when using OLE in AFL scripts.

It would be nice if there was a native AFL function like GetWindowID(), even better if this was available as a placeholder for declared static variables.

1 Like

Brad, there is such function - GetChartID()

https://www.amibroker.com/guide/afl/getchartid.html


Besides, to understand possible relationships between chart panes (and the important role of Chart ID), you should read this article:

http://www.amibroker.com/kb/2014/10/06/relationship-between-chart-panes/

1 Like

So you can write for example:

ChartID = GetChartID();
StaticVarSet("MyVariable" + ChartID, SomeValue);

... and later to retrieve the value:

StaticVarGet("MyVariable" + ChartID);
1 Like

Hi Milosz,

The Chart ID is specific to the chart pane, not the window. Different charts in the same window will have different chart IDs, so this cannot be used for isolating static variables to the same window.

I disagree. Chart ID's can be used for isolating static variables to the same window if you use their specific (hard-coded) numbers. You need to check/note each pane's chart ID and you will be able to identify which panes belong to a particular chart. I've been using similar solution for a long time.

1 Like

Yes, I get that, but it requires you to manually lookup the chart ID and hard-code it into the formula where you want to reference the static variable. I'm developing a commercial plugin for AmiBroker, so my users won't have access to the source code.

I can work around it, but I was just hoping there was some undocumented variable or private API that would allow for accessing the window ID without having to use OLE. Since all the panes would share the same window ID, you could set a static variable in one pane and retrieve it in a different pane without having to manually lookup the originating chart ID.

You can automate this task, by adding one additional line of code to your existing codes, which if applied to a chart will be retrieving chart ID and adding this number to some static variable. The main code will be reading this static variable holding selected chart ID's.

Usually the vendor recommends some specific layout - for example the main chart and three additional panes below, so it shouldn't be difficult to group the codes accordingly. In this example the static variable will be holding three different chart ID's.

And remember that in some cases different charts can have the same chart ID. Follow my second link for more details.

1 Like

Yeah, it's doable. Thanks for the suggestions!

@bkonia,

I may get flamed for suggesting this, but here goes... :slight_smile:

You can use the OLE object to retrieve the SelectedTab property from within AmiBroker without raising a warning by encapsulating the call in a script.

// JavaScript OLE call to retrieve the selected tab
EnableScript("jscript");
<%
function GetSelectedTab()
{
	AB = new ActiveXObject("Broker.Application");
	activeWindow = AB.ActiveWindow;

	return activeWindow.SelectedTab;
}
%>

// retrieve the selected tab
script = GetScriptObject();
selectedTab = script.GetSelectedTab();

// trace the result
formulaName = StrExtract(StrExtract(GetFormulaPath(), -1, '\\'), -2, '.');
_TRACE(formulaName +", Selected Tab: " + selectedTab);

You may need to do some additional work if you have multiple visible windows. Only one of them will be the active window.

2 Likes

That's a good suggestion, thank you.