How to make the application go to the forefront and focus?

I am working on an application that is bootable for the installer, which I am also working on. The application makes several MSI calls to get the information I need to build the wizard, which is the main window of my application, which causes the progress window to open during the collection of information, and then leaves after that. Then the wizard is configured and started. My problem is that the wizard (derived from CPropertySheet) does not want to go to the front and be an active application without adding some calls to me for this.

I solved the problem of bringing it to the forefront with the following code in my OnInitDialog () method:

SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); // force window to top SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); // lose the topmost status that the previous line gave us 

My problem is that I still have not figured out how to make the window self-active (i.e. make myself the one who has the focus). SetFocus () will not work in this context. I need something that will force the window at the top of the Z-order and activate it, preferably as few calls as possible.

I assume that the progress window, opened initially by MSI calls, causes the main window to freeze, but I cannot prevent this window from appearing. In addition, it would be pointless to hide this, because it allows the user to find out what happens before the main window appears.

+7
source share
4 answers

Andrey is not quite right. Windows is really trying hard to stop you from stealing focus, but this is possible using the following method.

  • Attach to the thread of the window that currently has focus.
  • Bring your focus window.
  • Disconnect from the stream.

And the code for this will look something like this:

 DWORD dwCurrentThread = GetCurrentThreadId(); DWORD dwFGThread = GetWindowThreadProcessId(GetForegroundWindow(), NULL); AttachThreadInput(dwCurrentThread, dwFGThread, TRUE); // Possible actions you may wan to bring the window into focus. SetForegroundWindow(hwnd); SetCapture(hwnd); SetFocus(hwnd); SetActiveWindow(hwnd); EnableWindow(hwnd, TRUE); AttachThreadInput(dwCurrentThread, dwFGThread, FALSE); 

You may or may not need to run your program with administrator rights for this to work, but I used this code personally and it did the job.

+9
source

You cannot steal focus. Period.

See this Old New Thing article:

https://blogs.msdn.microsoft.com/oldnewthing/20090220-00/?p=19083

+8
source

doesn't ShowWindow work (youwindow, SW_SHOWNORMAL)? -don

0
source

You will find that BringWindowToTop or SetForegroundWindow have requirements that must be met before the window is actually forcibly brought to the foreground over all other windows (applications). If they are not running, Windows will only launch the application icon on the taskbar. In this article we will talk about this, but as 1800 INFORMATION points out, this is not recommended. I think you just need to accept it.

0
source

Source: https://habr.com/ru/post/988114/


All Articles