How can I detect when a floating window is closed?

P

Pete

Using the Visio Drawing Control, I need to know when the user closes a
floating window like the Shapes window or the Pan & Zoom window.

When the window is DOCKED, I get the event in
AxDrawingControl.ViewChanged, and can process it. But if the user has
detached the window so it is FLOATING, the ViewChanged event does not
fire when the floating window is closed by the user. Looking at the
Visio Event Monitor, I can see that an event "BeforeWindowClose" is
fired. However, the Visio Drawing Control only has an event
"BeforeWindowClosed" (with a 'D' at the end), which does NOT get
called while a floating window is being closed. I don't know if they
are supposed to be separate events, or if there was a spelling error,
or what. But I have not found a way to detect when a floating window
is closed.

Has anyone encountered this, or if any of the Microsoft people are
checking on this, is there a work-around?

Thanks,
Pete
 
C

Chris Roth

I whipped up this VBA code to detect window closures at the application
level:

Dim WithEvents app As Visio.Application

Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
Set app = Visio.Application
End Sub

Private Sub app_BeforeWindowClosed(ByVal Window As IVWindow)
Debug.Print Window.Caption, Window.Type
End Sub

' Sample outputs:
' Window caption Type
' Drawing2 1
' Drawing Explorer 6


You might be able to narrow the scope by watching the window(s) of the
document you care about. I did a quick test and it didn't work, but it was
only a quick test. Also, you can check for Window.ParentWindow to see if the
sub-window that was closed is in a window that your code really cares about.

--

Hope this helps,

Chris Roth
Visio MVP
visioguy @ extremely warm mail.com
 
P

Pete

Chris,

Thanks. I was not able to get your sample to work, though.

The problem I am having is that the Drawing Control is not throwing
any events, or throwing a Visio event that the DC apparently can't
catch. I put together a simple test program, with a Drawing Control
on a form and a button to make the Pan & Zoom window appear and
disappear. I carefully watched all of the possible events thrown by
the DC, setting a breakpoint at each one, and I also watched in the
Visio monitor. Below are the steps I took, and the events listed in
the Event Monitor:

1. Open P&Z as docked - WindowOpened, ViewChanged
2. Close P&Z through code, as docked - ViewChanged
3. Open P&Z as docked - ViewChanged
4. Close P&Z by X, as docked - BeforeWindowClose (Not the same as
the BeforeWindowClosed event of the DC! Breakpoint is not hit.)
5. Open P&Z as docked - WindowOpened, ViewChanged
6. Detach P&Z, now floating - ViewChanged
7. Close P&Z through code, as floating - No event in Monitor, none
thrown
8. Open P&Z as floating - No event in Monitor, none thrown
9. Close P&Z by X, as floating - BeforeWindowClose (Breakpoint not
hit)

So I see two problems. The first is in Steps 4 and 9, where the Event
Monitor shows an event "BeforweWindowClose", but the Drawing Control
only has the event "BeforeWindowClosed", which gets thrown when the
actual drawing control is closed while the app is exiting. It never
gets called otherwise as far as I can tell. Is there a way for me to
add further events to the drawing control that I can catch? I am not
advanced enough to know that level of detail off the top of my head.

The second problem is in steps 7 and 8, where a floating window,
closed through the code (Visible set to false) does not trigger any
event at all. I suppose that is not a big deal, since the user would
be doing something in my program to cause that change and I can track
it there, but that still is troubling.

I don't know if these are bugs or if I am missing something simple.
Thanks for any help from anyone who can.

Pete
 
C

Chris Roth

Hmm. It worked fine for me. Do you have some lines like this somewhere in
your code:

Dim WithEvents VisApp as Visio.Application
VisApp = CType(visControl.Document.Application, Visio.Application)

Private Shared Sub VisApp_BeforeWindowClosed(ByVal Window As
Microsoft.Office.Interop.Visio.Window) Handles VisApp.BeforeWindowClosed

Debug.WriteLine("Before window closed")

End Sub

You get the Visio application object from the control...I think you were
using the BeforeWindowClosed event for the Visio control. Get the Visio
Application object from the control, then you can work like normal. I know,
it's a bit weird.

- Chris.
 
P

Pete

Never mind!

Someone pointed out to me that I could just make local variables for
the Windows collection of the drawing controls and add custom event
handlers to them to do what I want. I feel kind of dumb for not
thinking of it sooner!

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top