schmich / win32-window

Ruby interface to the Win32 window management APIs.
https://rubygems.org/gems/win32-window
MIT License
4 stars 0 forks source link

Window#foregroundize does not show the window #9

Open Andrek25 opened 4 years ago

Andrek25 commented 4 years ago

When I try to show the window while the CMD window where the program is running is inactive, it does not show the window, it seems that the CMD must be active for it to work.

Example:

require 'win32/window'

window = Win32::Window.find(title: /Notepad/i).first
sleep(3) # Time to click on another window.
window.foregroundize
schmich commented 4 years ago

The documentation in the library here is poor. The behavior you're seeing is expected.

#foregroundize calls SetForegroundWindow which, according to that MSDN doc, has the following limitations:

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

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The process is being debugged.
  • The foreground process is not a Modern Application or the Start Screen.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

In this case, clicking on another window prevents the cmd window from being able to successfully call SetForegroundWindow.

Andrek25 commented 4 years ago

There is no foreground process.

Is there a way to set foreground to null or something? i tried:

Win32::Window::SetForegroundWindow.call(nil)
Win32::Window::SetForegroundWindow.call(0)
Win32::Window::SetForegroundWindow.call(-1)

But none of those worked.

So if my process is not the foreground process i will not be able to show my window again?