Bring A Window to the Front

Anyone know of a Cocoa declare to bring a specific window (not all the windows) to the front?

( Window.show only works if the application is already in the front.)

BTW, you can do this on Windows using the MBS Plugin:

dim h as integer = theWindow.Handle call RemoteControlMBS.WinBringWindowToTop h

So let’s say the app has 5 open windows, and you’re in Safari. You want the app to activate, but only, say, window3 to come to the front? The layering would be: window3, Safari windows, window5, window4, window2, window1?

That’s right. Essentially I want to display an interactive dialog for someone to answer which then closes itself to let them get back to what they were doing.

It took a little looking around, but I found it in MacOSLib. Here is the code with that package installed:

  dim nsw as NSWindow = windowToActivate
  
  dim cntrl as new NSWindowController( nsw )
  cntrl.ShowWindow
  NSApplication.App.Activate( true )

Otherwise, look at the declares behind those calls.

Thanks Kem.

Any code for Win32?

Lennox

[quote=189569:@Lennox Jacob]Thanks Kem.

Any code for Win32?

Lennox[/quote]

https://forum.xojo.com/4603-make-a-window-topmost

Thanks Michel,

I haven’t tried it as yet, but reading the code it appears that the window will always be topmost. I will try that code and see what happens…

What I was hoping for was that if the window is behind another window, e.g. I have myAppWindow in front then I open another app e.g. Internet Explorer, then Internet Explorer will open a window in front of myAppWindow.

I now forgot that I have myAppWindow opened, but it is in the background, completely hidden by Internet Explorer, and I now try to open myApp.

I already have a mutex to prevent myApp from creating a new window, so the message indicating same is show.

When I dismiss that msgbox, I would like myAppWindow to now be topmost.

Kem’s code above does that on Mac.

Thanks again.

Lennox

[quote=189572:@Lennox Jacob]Thanks Michel,

I haven’t tried it as yet, but reading the code it appears that the window will always be topmost. I will try that code and see what happens…

What I was hoping for was that if the window is behind another window, e.g. I have myAppWindow in front then I open another app e.g. Internet Explorer, then Internet Explorer will open a window in front of myAppWindow.

I now forgot that I have myAppWindow opened, but it is in the background, completely hidden by Internet Explorer, and I now try to open myApp.

I already have a mutex to prevent myApp from creating a new window, so the message indicating same is show.

When I dismiss that msgbox, I would like myAppWindow to now be topmost.

Kem’s code above does that on Mac.

Thanks again.

Lennox[/quote]

That code does not make the window global floating, it just brings it to front when you run it. If any other app is run afterward or another window is displayed by the app, it works as usual.

Place the code in the place where you need it to bring the window to front. In what you describe, that would be after the MsgBox.

I placed it in a 10 seconds timer Action, it does bring the window to the front from behind. It does not activate it, though. You may need some additional code for that.

[code] dim l,t,h,w as int32
l = self.left
t=self.top
h=self.Height
w=self.Width

Declare Function SetWindowPos Lib “user32” (ByVal hwnd As Int32, ByVal hWndInsertAfter As Int32, ByVal x As Int32, ByVal y As Int32, ByVal cx As Int32, ByVal cy As Int32, ByVal uFlags As Int32) As Int32

const HWND_BOTTOM = 1
const HWND_NOTOPMOST = -2
const HWND_TOP = 0
const HWND_TOPMOST = -1

call SetWindowPos(Self.Handle, HWND_TOPMOST, l, t,w,h,0)[/code]

OK Michel Thanks,

I am not on PC today, so I will try it tomorrow.

Thanks again.

Lennox

Sorry to resurrect an old thread like this, but I precisely need to bring a Mac app to the front, and could not find any valid, recent link in github.

The one I found is dated 2017.

I will appreciate a link to the most recent version of MacOSLib, possibly 64 bit.

Unless Christian as some MBS magic to bring a Mac app to the front ?

[quote=497375:@Michel Bujardet]Sorry to resurrect an old thread like this, but I precisely need to bring a Mac app to the front, and could not find any valid, recent link in github.

The one I found is dated 2017.

I will appreciate a link to the most recent version of MacOSLib, possibly 64 bit.

Unless Christian as some MBS magic to bring a Mac app to the front ?[/quote]
This will bring the current process (your app) to the front using the MBS plugins.

[code] Dim processObj As ProcessMBS

processObj = New ProcessMBS
processObj.GetCurrentProcess
processObj.SetFrontProcessWithOptions(0)[/code]

Thank you Kevin :slight_smile:

Global Floating Window.

@Michel Bujardet I will appreciate a link to the most recent version of MacOSLib, possibly 64 bit.

I link to the following, then, in my apps, I remove all the folders that can be removed.

https://github.com/vidalvanbergen/macoslib/tree/master-64bit

Hi Carlo,

Is this compatible with the API2?

Thanks.

Lennox

Hi Lennox,
I build my apps with Xojo2019v2.1 without problems (after removing the unecessary stuff (folders)). Although v2.1 supports API2, I still carry on using API1.
Regards.

Unfortunately, this is available only at design time.

I have a small windowless Mac app which runs at system launch.

It needs to pop a MsgBox as alert when some conditions are met on a web site. It appears the Msgbox won’t show if the app is not in front. Hence my question.

Now, thanks to the MBS plugins, it works as expected.

Declares or MacOSLIb probably will do the same. Unfortunately I had the 32 bits version of MacOSLib which, of course, is inadequate for Catalina or Big Sur.

[quote=497495:@Carlo Rubini]I link to the following, then, in my apps, I remove all the folders that can be removed.

https://github.com/vidalvanbergen/macoslib/tree/master-64bit [/quote]

Thank you Carlo :slight_smile:

Thanks Carlo.
Lennox