CAPP Basics WinAPI - Switch between windows

Not very sure how to explain this in a clear way. In general, the fact is that I'm looking for a way to change the current active window (spontaneous determination, I hope that this will be clear) - a window where the text is directly typed right now. Whooh.

What I have already discovered is msdn help and SetFocus () or SetActiveWindow (), but this does not solve my problem (or, which is also possible, I just use it incorrectly).

Just:

HWND Dest = GetFocus(); ... //Some moving around on the 'alt-tab level' :-| SetFocus(Dest); 

Does not activate the Dest window. Please excuse the beginners for questions, hope you don’t need much time. Thanx!

+4
source share
2 answers

SetFocus () does not bring the window to the beginning. It just sets the keyboard focus.

SetActiveWindow () , on the other hand, displays a specific window at the top, but only if the application that calls it is also the application that owns It. (according to the documentation).

When you say you tried SetActiveWindow (), what do you mean? How did it happen? What results did he give?

Another function you can try: SetForegroundWindow () in case you want to activate a window belonging to another application, but this has its own problems, as you see below (directly from the documentation):

The system limits which processes can set the foreground window. a process can set a foreground window only if one of the following conditions is true:

  • A process is a foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock timed out (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • There is no menu.
+3
source

Try using the SetForegroundWindow function.

However, keep in mind that there are limitations to this that are explained in the note sections on the MSDN page, and I copied it here.

The system limits which processes can set the foreground window. a process can set a foreground window only if one of the following conditions is true:

  • A process is a foreground process. The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock timed out (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • There is no menu.

An application cannot force a window to the forefront while a user is working with another window. Instead, Windows flashes the taskbar to notify the user.

This means that if you do not own the current foreground process (which probably occurs when the user logs in), you cannot set a new foreground window.

There are several hacks (google SetForegroundWindow, and you will find them), but they are hacks, not a good idea - they allow the user to decide what is in the foreground! (also, as Raymond Chen explains in his blog here, hacks can often cause a program to freeze)

+2
source

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


All Articles