Windows Develop Bookmark and Share   
 index > Windows Forms General > How to detect whether a form is hidden by another application?
 

How to detect whether a form is hidden by another application?

Hello all:

I have a form application and I need to respond to the event when other application blocks the view of this application.

For example:

If I open my application first, then I open Notepad and use notepad to cover my application.

At this moment, I want my application to know that it is blocked.

Is there a way that I could know whether my form or a control on the form is blocked by other application.

Thank you

-Daniel

daniel mark  Tuesday, August 21, 2007 6:54 PM
Although it is technically verboten by the SDK documentation, you can try using the SetParent() API function to make the old app's main window the parent of your form. If that works, your form will be minimized along with the old app's and always stay on top of it without having to use the TopMost property.
nobugz  Wednesday, August 22, 2007 3:02 PM

the are ways using the windows api to find out whether there are any windows covering yours. however, this is not trivial and there would be a not inconsiderableoverhead in checking this.

why do you want to know this?

if the answer is that you want the application to move itself in front of the offending window, look into the TopMost property.

exotericist  Wednesday, August 22, 2007 12:48 PM
You'll have to tighten-up your specification a bit. What do you call "blocked"? If Notepad covers a wee corner of your form, is that blocking? The general approach requires P/Invoking Windows API functions. Use GetWindow(hWnd, GW_HWNDPREV) to find the previous window in the Z-order, use GetWindowRect() on that window to see if it covers your form. Repeat until GetWindow() returns IntPtr.Zero. Use SetForegroundWindow() to move your form to the foreground. That doesn't always work if the previous foreground window is "in use". You can retrieve the required P/Invoke declarations from www.pinvoke.net
nobugz  Wednesday, August 22, 2007 12:52 PM
nobugz wrote:
You'll have to tighten-up your specification a bit. What do you call "blocked"? If Notepad covers a wee corner of your form, is that blocking? The general approach requires P/Invoking Windows API functions. Use GetWindow(hWnd, GW_HWNDPREV) to find the previous window in the Z-order, use GetWindowRect() on that window to see if it covers your form. Repeat until GetWindow() returns IntPtr.Zero. Use SetForegroundWindow() to move your form to the foreground. That doesn't always work if the previous foreground window is "in use". You can retrieve the required P/Invoke declarations from www.pinvoke.net

Hello nobugz:

Thank you for your comments.

The basic idea why I should know this is as follows:

I need to draw a Figure on top of an application that currently doesn't provide an easy to use Control for me.

So my workaround is to create a Form without border and try to make the new Form tied with the old application and make the new form always on top of all other applications.

However, the drawback of these trick is that the new form cannot perform as same as the old application.

For example:

1> If users minimize all applications on the desktop, the new form will still appear on the desktop.

2> If users open a new application that cover part or full of the UI of the old application, the new form will still be on the top.

I don't think it is a good ideato use this technique. However, currently I don't have other options.

Thank you

-daniel

daniel mark  Wednesday, August 22, 2007 2:47 PM
Although it is technically verboten by the SDK documentation, you can try using the SetParent() API function to make the old app's main window the parent of your form. If that works, your form will be minimized along with the old app's and always stay on top of it without having to use the TopMost property.
nobugz  Wednesday, August 22, 2007 3:02 PM

You can use google to search for other answers

Custom Search

More Threads

• Question about accessing properties, methods from a user control
• Closing Application Down after last form close
• get selected index of a ComboBox column in a DataGridView
• Display a hyperlink in the titlebar of a Form
• Exception Handling HELP
• reading a setting from another application .config file
• Window.Forms: menu appears after headline click [Solved]
• Problem in changing the custom icon for a Windows form...
• FormClosing Issue in VS2005
• Store RichTextBox's content in Access