You are here:

C++/MFC SetForegroundWindow() (TOPMOST)


Hello Eddie, txs in advance for any help you can give me, this is my first time using AllExperts ...

I'm having trouble making an App popup on instance collision (running the application again and making the old window) the topmost in the Z order.

I've managed to show but it seems that as I click the icon to run another instance the Win OS prevents the old window to be on top (as in not making it the topmost with focus like when you run an App for the first time), I've tried some of the most used methods like SetForegroundWindow() and SetActiveWindow() or SetFocus() but the only way that I've managed to make if perform like I intended was using SetWindowPos() but that would make it a fixed topmost window (there should be something that I'm missing)...

Part of the same question but not the same problem, is there a way to get from the Win32/MFC framework what is the active window of a given App, I'm using a MFC CDocument framework on my MDI and CWinApp::m_pMainWnd or GetActiveWindow() will only report the active frame window, my intention is to have lets say a dialog active and the mainframe hidden at some point and if the App runs again the dialog would popup not the mainframe.


Hello Cinap, thank you for the question.

One of my friends ran into this problem a while back, and I remember that it was a real pain. He kept a hierarchy a structures that contain a pointer to a CWnd and a integer representing the object's current age. Then he ran a sort function all the way down the hierachry based on the age and ordered the windows from youngest to oldest.  

This seems to also help you with your second problem. You can also traverse your hierarchy and call the function CWnd::GetFocus() on all the objects until you found the object whose call to that function didn't return NULL. That should give you the active window of the application.

If you have another question I'd be glad to help some more.

I hope this information was helpful.

- Eddie  


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 All rights reserved.