views:

537

answers:

5

Hello,

Normally you use Form.Visible to check if Window is visible at all. But sometimes on the screen window is below other windows so it's really invisible.

So how to check in c# Windows Forms if window is really visible or not?

I would like to accomplish this: when I click CTRL+K on my keyboard and my window is visible on my screen it does nothing. But when it's underneath other windows it pops to the top (Bring to front).

kind regards

A: 

Hm... weird question. :P

Perhaps you could ask the location of the forms, and if two forms interlap (figure out their coords, and make a simple method) check if one form has Focus(). If it has focus, then other must be "invisible" (in the sense that a user can't see it because it's underneath the other form).

Obviously this method is hacky at best, but it's something you can start working with.

Sergio Tapia
+2  A: 

You can call the Activate method on the form to bring it to the front if it isn't already.

However, note that if a different program is active, it will usually simply flash the desktop button (depending where you call it from). This is Windows' standard protection against focus-stealing and you should not try to work around it.

SLaks
A: 

To answer the question as asked, you could try calling the WindowFromPoint API function to find the window at various points on your form, and check whether it returns the handle of whatever you expect to be at that point.

SLaks
A: 

You could use Windows API to enumerate all windows, retrieve their Z-Order and compare it with the Z-Order of your window. I thing someone did this already here.

Nestor
A: 

You should be able to find out if your window is visible by overriding the OnPaint method. You'll want to pass control to the base class in order to do the actual painting, but you'll be able to detect whether a paint message is received. Update: no, this doesn't work, Sorry!

In principle, the Activate method should bring your window to the foreground, but in practice I've always found this problematic if other processes have the input focus. If you really want someone to see a window, set the topmost bit, but expect them to be annoyed! One surefire way to get some attention for a window is to close it and reopen it, if you can get away with that.

One way to achieve what you're looking for is to use a notify icon, this will get the user's attention in a way that is compliant with Windows UI guidelines.

Paul Keister