-5

I'm trying to close other windows in my WPF application similar to this. The problem is that some window applications such as notepad only shows a dialog box containing "Do you want to save changes?". How do I detect if a window doesn't automatically close? Also, is there a possible way to override this behavior?

Community
  • 1
  • 1
RandomUser
  • 79
  • 4

1 Answers1

0

There is no generic "Do you want to save?" dialog in Windows. Applications typically just display a MessageBox in response to the WM_CLOSE message.

You can try to manually post the WM_CLOSE message to a top-level window, and if after a short timeout, the window becomes disabled but still visible then you can assume that it is displaying a modal dialog that is perhaps asking the user to save.

To make it more robust you could send the message on one thread and check the enabled state on another thread but this is still a hack that you really should avoid.

The correct way to interact with other UI applications is to use UI Automation. Especially if you intend to do something nasty like clicking one of the buttons on behalf of the user.

Anders
  • 97,548
  • 12
  • 110
  • 164