0

Wonder if there is a way to check if an application is on top of my form?

For example if you open up your form, and then open up 2 other windows(like music player and web browser), and to list these windows?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • You can check if your form has `Focus` (but this might not be totally correct) – gunr2171 Oct 11 '13 at 19:10
  • you could check for focus pretty easily, but that's not exactly the same thing – Jonesopolis Oct 11 '13 at 19:10
  • 5
    with questions like these, my question is always "Why?" can you do it? you can use native win32 apis to enumerate all windows and find those which overlap yours, yes. but what would you do then? what are you *actually* trying to do? Do you want to force yours to be always on top? do you simply want to bring yours to the front? – John Gardner Oct 11 '13 at 19:12
  • 2
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 11 '13 at 19:12
  • gunr2171, Jonesy, no its not what i mean but thanks. Habib, not a duplicate at all, but thanks. John, Why do you care? :) I have my own program, which needs this part to work correctly. If you would not like to help, then just leave the posts alone, thanks ;) –  Oct 11 '13 at 22:32

2 Answers2

0

For example if you open up your form, and then open up 2 other windows(like music player and web browser), and to list these windows?

You could P/Invoke EnumWindows. This will return the list of windows in z order.

As soon as your Form is found, you'd know you no longer have to continue searching. All windows you found prior to your window Handle would be "above" your Form in the z order.

If you're only interested in windows which overlap yours, you'd have to check their position and size as well.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • that gets them in z order, but not whether they are overlapping, correct? you'd have to do extra work to compare window bounds/etc? – John Gardner Oct 11 '13 at 19:15
  • @JohnGardner Yes - see the last paragraph ;) – Reed Copsey Oct 11 '13 at 19:50
  • You could combine this with some changes to my answer here http://stackoverflow.com/questions/18860247/how-can-i-check-if-a-form-is-behind-another-window/18876823#18876823 – TDull Oct 12 '13 at 11:43
0

That standard way would be to

  1. Get your window's bounding box as a rectangle.
  2. Get your window's location on the Z-axis.
  3. Find all open windows with a Z-axis location higher than yours (which see the answer by @ReedCopsey above).
  4. For each such window, determine if its bounding box intersects yours. See:

    for a discussion of the algorithm. Or let the CLR do the work for you using the System.Windows.Rect.Intersect() overloads.

Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • Thanks for the reply, this seems to be the most complete answer, so i accept this one, just meanwhile, i found an easier way to do what id like to. Thanks anyway, caz i might need this later! –  Oct 13 '13 at 15:34