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?
Asked
Active
Viewed 172 times
-5
-
You want to control Notepad from a different process? Really? – David Heffernan Mar 10 '17 at 07:41
-
Why do you want to close other applications in the first place? If we know the reason we might be able to suggest an alternative solution. – zett42 Mar 10 '17 at 14:07
1 Answers
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