Page 1 of 1
How to set up BrowserControl to send an event when it closes
Posted: Wed Apr 12, 2006 1:06 am
by revbob
I wrote about a Pandora node a while back, and it works just ducky except for one thing: when I close the browser window, the player (an embedded Flash control) keeps on playing invisibly.
Here's how I create the window:
Code: Select all
Sub OpenInPopup(Node)
Dim Form, BrowserControl, Command
Command = Node.CustomData
Set Form = SDB.UI.NewForm
Form.Common.SetRect 100, 100, 900, 700
Form.Caption = Command
Set BrowserControl = SDB.UI.NewActiveX(Form, "Shell.Explorer")
BrowserControl.Common.Align = 5 ' Fill all client rectangle
BrowserControl.Interf.Navigate Command ' Go to the specified URL
Form.Common.Visible = True
SDB.Objects("Pandora Window") = Form ' Make the window hang about
Script.RegisterEvent Form, "OnShutdown", "FormClose"
End Sub
When I put a MessageBox in my FormClose sub to indicate receipt of the event, neither closing the browser window nor closing MM activates , so I suspect the problem is where I hung the BrowserControl. Here's the sub, just in case the fault is there:
Code: Select all
Sub FormClose
SDB.MessageBox "Got OnShutdown", mtInformation, Array(mbOk)
End Sub
What would be a good thing to put in that sub, once I get the event? I was thinking about
Code: Select all
SDB.Objects("Pandora Window") = Nothing
but is that just wishful thinking that the right thing will happen?
Thanks in advance for any help.
Posted: Wed Apr 12, 2006 3:08 am
by Steegy
Hello
The close event of the form is more than probably OnClose, and not OnShutdown (that one is raised when you shut down the application).
So the line should be:
Code: Select all
Script.RegisterEvent Form, "OnClose", "FormClose"
Yes, when your form closes, you should release the reference:
Code: Select all
Sub FormClose
SDB.MessageBox "Got OnClose", mtInformation, Array(mbOk)
SDB.Objects("Pandora Window") = Nothing
End Sub
BTW: Are you aware that it's now possible to use MediaMonkey's default browser window, referenced by WebControl?
sample code:
http://www.mediamonkey.com/forum/viewto ... 9852#39852
Cheers
Steegy
Posted: Wed Apr 12, 2006 4:17 pm
by revbob
Thanks. OnClose did the trick. I'm getting the event now. Alas, when the window closes, the Pandora applet keeps playing the music and only stops playing when you exit from MM, same as before.
Yeah, but if I use MM and come back to that window, Pandora gets restarted. Curiously, when I display, e.g., a track listing from the library in MM's main pane, the Pandora applet keeps playing the music, just like when I shut down the popup.
When I use another function like this,
Code: Select all
Script.RegisterEvent PandoraNode, "OnNodeFocused", "OpenInDefaultBrowser"
and have a handler like this,
Code: Select all
Sub OpenInDefaultBrowser(PandoraNode)
Dim WPandoraShell, Command
Set WPandoraShell = CreateObject("WScript.Shell")
Command = PandoraNode.CustomData
Result = WPandoraShell.Run(Command, 0, 0)
End Sub
It works just fine, turning off the music when the window closes.
It sure sounds like the MSIE embedded control isn't telling all its children when it's leaving the building.
Anyhow, I'm beginning to think the error is in the MSIE control and not in my code or in my understanding of OLE (etc.). Does that sound sensible to you?
Posted: Wed Apr 12, 2006 5:49 pm
by Steegy
most of it...
The way that the WebControl works might seem a bit strange, but it's certainly not an error.
If you understand how these things work, it's quite obvious.
Now I'm trying to explain the difference between the built in WebControl and a custom web page control that you can add yourself:
WebControl: a permanent available web page control that is hidden after the tracklist, and can be made visible through a "OnNodeFocused" event. It doesn't close, it can only be shown and hidden (and it acts as any other web control).
E.g. when the tracklist is shown and you do a WebControl refresh, you can "hear" the page refreshing in the background. That means it's there and it's active (it can be loading/running a page) but just isn't made visible by MediaMonkey.
That means that you could "preload" things in the background, and then show them later by making the WebControl visible.
(To show the WebControl you are accessing, I guess you have to return value 2 from the event handling method.
So, I think the clue is, when OpenInDefaultBrowser (in your code, changed to use the WebControl) returns value 2 (this method is a Function, not a Sub !) MediaMonkey hides the tracklist and shows the WebControl.)
- When you want to close/stop a page in the WebControl, you have to do that by navigating to about:blank or by using the Stop command. The WebControl itself stays available so you can't close it.
By navigating to about:blank or to another page, you are getting rid of the previous page.
- A normal web page control you create and show yourself, can be closed (destroyed, e.g. by closing the form).
When the control is closed, of course the previous page is gone too (automaticly).
Cheers
Steegy
Posted: Wed Apr 12, 2006 11:09 pm
by revbob
Yeee, hawwww! It works!! Thanks, Steegy.
The whole script is at
http://www.mediamonkey.com/forum/viewto ... 3036#43036
The extra bits that make it work are one line toward the end of OpenPopup():
Code: Select all
...
SDB.Objects("Pandora Window") = Form ' Make the window hang about
SDB.Objects("Pandora Browser") = BrowserControl.Interf
Script.RegisterEvent Form, "OnClose", "FormClose"
End Sub
where I park the browser control's interface object. And I come back and get it in FormClose handler (here's the whole thing):
Code: Select all
Sub FormClose(Node)
SDB.Objects("Pandora Window") = Nothing
SDB.Objects("Pandora Browser").Navigate "about:blank"
SDB.Objects("Pandora Browser") = Nothing
End Sub
I've got some qualms about sending an event to an object, particularly an object as complex as a browser control, and then immediately setting the handle to null, for fear the object will ask about its parent, but it doesn't seem to do anything too horrible.
Well, tell a lie, you've got to click something else (pretty much anything) before you click the Pandora node again, but I think that's because OnNodeFocused is an edge triggered event, and it still hasn't left the last state. Verdad?
Anyhow, it seems to be non-evil, and anybody who'd like to add Pandora to MM, you can get it at the URL above.
Thanks again, Steegy.