I'm not going to post the code I used for this, but I've done that several times.
The issue is, it's a NASTY method to do something that we should be able to have specified as an option in a dialog.
I very much would like to be able to "attach" a dialog to a channel, and when the active channel window changes, the dialog automatically knows what window its attached to.