0

Is it possible to combine two windows (Forms) so that they have single input focus, and one form don't become inactive when switching to another form? Focus here (and only here) is an Active or Foreground state of Win API, not the cursor.

jsmith
  • 175
  • 1
  • 10
  • I'm not sure I understand what you are looking to achieve, so please [read answers of this post](https://stackoverflow.com/questions/3940346/foreground-vs-active-window). Then reformulate your question, e.g. to explain why you are asking. – Tom Brunberg Aug 08 '20 at 07:43
  • I use parent form, and control form of different application using `SetParent`. So, if a "child" form of different application activates, the "parent" deactivates. It is possible to return it manually, but some items (popup menus will disappear), for example. In **this** question I asked not exactly that, but that's "I'm looking to achieve". – jsmith Aug 08 '20 at 08:07
  • Can work without returning focus, but want to parent window not become inactive and changed their header, and Alt-Tab. Did I mentioned Alt-Tab? – jsmith Aug 08 '20 at 08:08
  • 1
    As far as I know, only the *Foreground application* can have an *Active window*. The foreground application / active window is the one that receives input events (e.g. keyboard events). Other applications also have active windows, but they do not receive input events (as they are not foreground applications). When focus is taken away from a window, any currently open menu of that window, is closed. So, it seems the answer is *not possible*. – Tom Brunberg Aug 08 '20 at 08:46
  • you mean like a tool window? – whosrdaddy Aug 08 '20 at 10:24
  • 1
    @jsmith "*I use parent form, and control form of different application using `SetParent`.*" - [that will not work out well](https://devblogs.microsoft.com/oldnewthing/20130412-00/?p=4683). – Remy Lebeau Aug 08 '20 at 20:24
  • @whosrdaddy like MDI child window, but without header and restore/minimize capability. In a single process, parent and child window can have like-a-single focus. – jsmith Aug 09 '20 at 19:03

2 Answers2

2

The answer is no! You can't have focus on multiple windows at the same time - that's the way how Windows is designed.

There are still some ways for your application(s) to react to input (keyboard, mouse...) even if your application does not have focus.

If you only need to react to a keystroke you can register a global hotkey with your application and then handle what happens when that specific hotkey is pressed, even when your application is not active.

But if you need to react to every keyboard or mouse input then you will have to register system-wide hooks accordingly. The main advantage of hooks is that you will be able to detect keyboard and mouse events even before the active application does and thus, if needed, also intercept them entirely so that other applications won't receive them at all/anymore.

But beware that when implementing hooks incorrectly you could cause havoc to the computer since you are working on a low level approach that could even lead to system crashes if not done correctly.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • i'm just translating wm_keydown messages from parent to child without hooks (and some messages to prevent loops) – jsmith Aug 09 '20 at 18:16
  • A misbehaving input hook can render the system unusable, but it cannot cause the system to crash. – IInspectable Aug 09 '20 at 21:33
  • how MDI windows can be both active (parent and child)? at least it shows header color of both as active. is there a way to "emulate" MDI windows through different applications? – jsmith Aug 12 '20 at 03:14
1

According to the Active Window Document:

An active window is the top-level window of the application with which the user is currently working. To allow the user to easily identify the active window, the system places it at the top of the z-order and changes the color of its title bar and border to the system-defined active window colors. Only a top-level window can be an active window. When the user is working with a child window, the system activates the top-level parent window associated with the child window.

Only one top-level window in the system is active at a time.

The MDI windows also has only one active window.

To create a window like MDI(which will show header color of both as active), you could try to add the WS_CHILD style to the child window.

If you also want to realize that one thread shares its input state with another thread, you can try AttachThreadInput.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30