How to force a global window to go frontmost? (in focus)

How to force a global window to go frontmost? It does not work after activating the app from the TrayItem. I’m mainly targeting Windows at the moment if suggestions are platform specific. Here are things I tried:
myWindow.hide and then
Setting a 1 second timer past the TrayItem click to execute the above.

Nothing sets the focus to that global window once it’s lost.

Hi Ben,

welcome to the Forum. Look for a window of type Floating Window in your documentation and in the forum. If that does not work as expected, a Declare may be used. A Declare is a way to call an OS API that is not readily available in Xojo.

Hi, I’m not asking what a global floating window is. I’ve been using RealBasic for over 20 years, so I know what it is. I’m asking how to procedurally force a global floating window to appear on top of all other global floating windows, including the Windows taskbar once that floating window has been deactivated. When I use the global floating window shows but it does not focus and a mouse click has to be made in the window to get it to focus.

To be honest if you really refer to Xojo as “RealBasic” in 2022 then you need to take some time and read over the newest documentation

Have a look a that these, maybe it could help you:

Can I make the Plan Box window stay above all other windows? - General - Xojo Programming Forum

App always on top - Targets / Windows - Xojo Programming Forum

I will be taking a look at both suggestions tomorrow and I’ll report back. Thanks.

I just had a quick try at it and using the following code on demand (e.g. after the taskbar’s TrayItem has been clicked) resulted in the app window quickly flashing and YES it worked! The window got the focus. But the issue is that it only works the first time this code is executed. Subsequent calls to it only make the window briefly flash, but it is not brought topmost anymore :frowning: So it works, but only once. I feel like the solution is very close!

Dim NowAPI As Integer
const HWND_TOPMOST = -1
Soft Declare Function SetWindowPosAPI Lib "user32" Alias "SetWindowPos" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
NowAPI = SetWindowPosAPI(self.Handle, HWND_TOPMOST, self.left,, self.width + 20, self.height + 20, 0)

The solution in the end was the following code, which doesn’t seem to be talked about on this forum, at least I couldn’t find it here. But it works beautifully and reliably! Obviously the downside is that it’s platform specific, only is good for Windows. I have not tested the Cocoa declares that are on this forum, but if I have to compile for macOS at some point, I’ll be sure to try it tough.

For Windows:

#if TargetWin32 Then
  dim b as boolean
  Soft Declare Function SetForegroundWindowAPI Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Integer) As Boolean
  b = SetForegroundWindowAPI(self.handle)

Hope it helps somebody in the future! Now onward with RealBasic, I mean Xojo!

1 Like