What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [?] Detecting when a Plus! Window is closed

[?] Detecting when a Plus! Window is closed
Author: Message:
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [?] Detecting when a Plus! Window is closed
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...

This post was edited on 10-14-2009 at 12:22 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
10-14-2009 12:08 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[?] Detecting when a Plus! Window is closed - by ryxdp on 10-13-2009 at 08:36 AM
RE: [?] Detecting when a Plus! Window is closed - by matty on 10-13-2009 at 01:17 PM
RE: [?] Detecting when a Plus! Window is closed - by ryxdp on 10-14-2009 at 03:10 AM
RE: [?] Detecting when a Plus! Window is closed - by Matti on 10-14-2009 at 12:08 PM
RE: RE: [?] Detecting when a Plus! Window is closed - by ryxdp on 10-14-2009 at 10:46 PM
RE: RE: [?] Detecting when a Plus! Window is closed - by CookieRevised on 10-16-2009 at 05:47 PM
RE: RE: RE: [?] Detecting when a Plus! Window is closed - by ryxdp on 10-16-2009 at 11:13 PM
RE: [?] Detecting when a Plus! Window is closed - by Matti on 10-16-2009 at 03:33 PM
RE: [?] Detecting when a Plus! Window is closed - by CookieRevised on 10-16-2009 at 11:50 PM
RE: RE: [?] Detecting when a Plus! Window is closed - by ryxdp on 10-17-2009 at 12:06 AM
RE: [?] Detecting when a Plus! Window is closed - by Matti on 10-17-2009 at 08:55 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On