I’ll let my application be automated from another application by exposing a net pipe. Basically, application B will emulate being a user using application A, which is a WinForms application.
My only problem so far is that I don’t want application B to be able to access functionality you wouldn’t be able to access if you’d be an actual user, like clicking buttons in a form while there is a message box or a modal dialog on top of it. This should keep me on the safe side while still giving application B full control over application A.
What I would do is override MessageBox.Show with custom code, as well as every call to any ShowDialog in application A to set some global boolean to false while the modal dialog is being shown that will tell whether the main form is accessible at the moment or not. Then I could either make application B wait or return an error.
But instead of my solution, is there some built in way to tell whether a form has a dialog on top or not? I tried ‘Active’, ‘Focused’, even the native GetForegroundWindow but none of them work as I need. To start with, all of them will be false if my application is minimized, which I don’t want.
Modal dialogs disable their owners so one simple test would be to check if the window was enabled. Use IsWindowEnabled to determine this. You also need to check that the window is visible with IsWindowVisible.