It seems that matty copied that code snippet from Screenshot Sender. The line with _debug can simply be removed, and the _win32 can be replaced by Interop.Call.
However the code could still use some optimization. There's no need for a for-loop to check whether the window exists - you can use "[key] in [enumerable]" as an expression as well:
Javascript code:
var objWindows = {};
function OpenWindow(Name, XML, ShowCode){
if (typeof(objWindows[Name]) === 'object' && typeof(objWindows[Name].Handle) === 'number'
&& Interop.Call('user32', 'IsWindow', objWindows[Name].Handle)) {
Interop.Call('user32', 'SetForegroundWindow', objWindows[Name].Handle);
return false;
}
if (typeof(ShowCode) === 'undefined') ShowCode = 0;
objWindows[Name] = MsgPlus.CreateWnd(XML, Name, ShowCode);
return objWindows[Name];
}
function CheckIfWindowExists ( Name ) {
return ( Name in objWindows && typeof(objWindows[Name]) === 'object');
}
function OnWindowIdEvent_Destroyed(pPlusWnd) {
delete objWindows[pPlusWnd.WindowId];
}
quote:
Originally posted by ryxdp
Pretty much what I need is a way of calling OnWindowidEvent_Destroyed without knowing what the WindowId actually is, as it can't be hardcoded into the function name.
Do you want to destroy the window and thus triggering the Destroyed event or do you only want to call the OnWindowIdEvent_Destroyed function without actually destroying the window?
In the first case, you can simply check whether it already exists (using CheckIfWindowExists above) and then use PlusWnd.Close().
In the second case, I'd suggest you to move the contents of the event function to some other place. Calling a function in the global scope by its function name in JScript can only be done using eval() and as you know, eval is evil and should be avoided as much as possible. Here's a possible solution: store the events in a global object indexed by the window's name and the event name, then retrieve the right function from the object and call it whenever needed.
Javascript code:
// Store the event handlers here
var objEvents = {
'WndMain' : {
'Destroyed' : function(PlusWnd, ExitCode) {
// Code goes here
}
},
'WndAbout' : {
'Destroyed' : function(PlusWnd, ExitCode) {
// More code here
}
}
};
// Define the "real" event handlers
function OnWndMainEvent_Destroyed(PlusWnd, ExitCode) {
return objEvents.WndMain.Destroyed(PlusWnd, ExitCode);
}
function OnWndAboutEvent_Destroyed(PlusWnd, ExitCode) {
return objEvents.WndAbout.Destroyed(PlusWnd, ExitCode);
}
// Now you can call the event handlers from anywhere using:
var sWindow = PlusWnd.WindowId;
objEvents[sWindow].Destroyed(PlusWnd, 0);
// You can even get the event name itself dynamically!
var sWindow = PlusWnd.WindowId;
var sEvent = ( foo === bar ? "CtrlClicked" : "Destroyed" );
objEvents[sWindow][sEvent](PlusWnd, 0);
It would be much easier if Plus! were be using a more flexible, possibly OOP way of assigning and triggering event handlers though...