Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I get the handle of the window that will be foreground window when my app is closed.While doing this, I need my application form is visible. For example I have a form with TopMost property is true.I clicked any other program and my app is still foreground window.This means if I closed my app,the other program will be foreground window. How can I get this other program window handle without closing my app?
Posted
Updated 19-May-14 8:43am
v2
Comments
OriginalGriff 19-May-14 14:21pm    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Without it, that makes no sense to me whatsoever...
Use the "Improve question" widget to edit your question and provide better information.
Amt-Coder 19-May-14 14:44pm    
Sorry for my english.I'm trying to tell my problem as best I can.
Sergey Alexandrovich Kryukov 19-May-14 15:41pm    
You English is clear enough. You probably have misunderstanding of the concept "on top", "always on top", and activation. Please see my answer.
—SA
Sergey Alexandrovich Kryukov 19-May-14 15:36pm    
Do you need an Windows Handle (HWnd) or something else? And why?
I guess this is System.Windows.Forms, but you should add it as a tag of your question.
The problem is very simple, but I don't want to waste time if you have a wrong idea.
—SA

1 solution

Please see my comment to the question. Maybe the MSDN documentation is not quite clear about the subject but the specific purpose of the property TopMost is to show the form of top of all other forms on the desktop regardless of their activation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost%28v=vs.110%29.aspx[^].

This effect is used quite rarely, because it can distract from other applications, so it is used only in some very important critical need of some very special applications. If you want something else, you should not use TopMost. But then, if setting this property to false won't solve the problem you are trying to solve, you should explain the behavior your want. And you always need to explain why you want something.

[EDIT]

How to get a handle?

If you have a form instance, that's Form.Handle:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handle.aspx[^].

In all other cases, you can get an instance of some process (your current System.Diagnostics.Process.GetCurrentProcess or any other one) and get the handle of a main window of the corresponding application using System.Diagnostics.Process.MainWindowHandle:
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.mainwindowhandle[^].

Pay attention for the exception which can be thrown is there is not handle. Therefore, you generally have to call this property under a try-catch block.

—SA
 
Share this answer
 
v3
Comments
Amt-Coder 19-May-14 16:32pm    
I know TopMost is used to set the form is always foreground window. I'm working on a screencapture application.This app can take foreground window screenshot.I'm doing this using GetForegroundWindow and GetWindowRect. When user clicked on takeScreenShot button my code hides the form ,then calls GetForegroundWindow to get handle ,sends the handle to GetWindowRect to get ForegroundWindow rectangle location and size. But I want to warn user with a text if this rectangle is belong to TaskBar or null before click the button. I hope I'm clear at this time.
Sergey Alexandrovich Kryukov 19-May-14 18:15pm    
Maybe, at the moment of asking question, you did not know the meaning of this "always". You wrote: "I clicked any other program and my app is still foreground window", but this is exactly what you should have expected. Are you capturing foreground or active window? Do you know the difference? What stops you from showing the user a text? Note that having a TopMost window doesn't mean having it activated. Besides, you can use the style which prevents your application form from activation, which can be used in combination with TopMost.
—SA
Amt-Coder 20-May-14 17:01pm    
Oh,sorry.I have just learned the difference between active and foreground.According to this difference my application takes "foreground window".So I changed "ActiveWindow" to "Foreground" in my question. And here is a screenshot to tell my problem more clearly
http://i.imgur.com/jCTnM9F.jpg?1?9183
How to get the handle of the window that is under my app when my app is visible?
Sergey Alexandrovich Kryukov 20-May-14 23:24pm    
Please see the update to my answer, after [EDIT]. Does it answer this question?
If it does, kindly accept it formally (green "Accept" button). In all cases, your follow-up questions will be welcome.
—SA
Amt-Coder 22-May-14 9:48am    
Thanks for your efford Sergey.I've looked at your solution but it seems it is not my problem's solution.Maybe I can tell my problem more clearly with this question "How can I get the handle main window that I clicked penultimate?"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900