# Bring A Window to the Front

**URL:** <https://forum.xojo.com/t/bring-a-window-to-the-front/15109>\
**Category:** General\
**Created:** [March 20, 2014, 12:35pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109 "2014-03-20T12:35:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Dodd](https://forum.xojo.com/user_avatar/forum.xojo.com/stephen_dodd/32/530_2.png) [@Stephen\_Dodd](https://forum.xojo.com/u/Stephen_Dodd)\
**Post date:** [March 20, 2014, 12:35pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/1 "2014-03-20T12:35:38Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [March 20, 2014, 1:04pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/2 "2014-03-20T13:04:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Stephen\_Dodd](https://forum.xojo.com/user_avatar/forum.xojo.com/stephen_dodd/32/530_2.png) [@Stephen\_Dodd](https://forum.xojo.com/u/Stephen_Dodd)\
**Post date:** [March 20, 2014, 1:08pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/3 "2014-03-20T13:08:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [March 20, 2014, 1:35pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/4 "2014-03-20T13:35:00Z")

</div>

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

```auto
  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.

---

<div class="post-metadata">

**Author:** ![Lennox\_Jacob](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/c5a1d2/32.png) [@Lennox\_Jacob](https://forum.xojo.com/u/Lennox_Jacob)\
**Post date:** [May 24, 2015, 8:54pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/5 "2015-05-24T20:54:56Z")

</div>

Thanks Kem.

Any code for Win32?

Lennox

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 24, 2015, 9:04pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/6 "2015-05-24T21:04:54Z")

</div>

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

Any code for Win32?

Lennox[/quote]

[https://forum.xojo.com/4603-make-a-window-topmost](https://forum.xojo.com/4603-make-a-window-topmost)

---

<div class="post-metadata">

**Author:** ![Lennox\_Jacob](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/c5a1d2/32.png) [@Lennox\_Jacob](https://forum.xojo.com/u/Lennox_Jacob)\
**Post date:** [May 24, 2015, 9:20pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/7 "2015-05-24T21:20:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 24, 2015, 9:25pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/8 "2015-05-24T21:25:54Z")

</div>

[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]

---

<div class="post-metadata">

**Author:** ![Lennox\_Jacob](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/c5a1d2/32.png) [@Lennox\_Jacob](https://forum.xojo.com/u/Lennox_Jacob)\
**Post date:** [May 24, 2015, 10:14pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/9 "2015-05-24T22:14:06Z")

</div>

OK Michel Thanks,

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

Thanks again.

Lennox

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 21, 2020, 12:05pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/10 "2020-07-21T12:05:26Z")

</div>

> [@72896:@Kem Tekinay](#):
>
> It took a little looking around, but I found it in MacOSLib. Here is the code with that package installed:

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 ?

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [July 21, 2020, 2:59pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/11 "2020-07-21T14:59:35Z")

</div>

[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]
```

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 21, 2020, 6:24pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/12 "2020-07-21T18:24:47Z")

</div>

> [@497407:@Kevin Gale](#):
>
> This will bring the current process (your app) to the front using the MBS plugins.

Thank you Kevin 🙂

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [July 21, 2020, 11:56pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/13 "2020-07-21T23:56:40Z")

</div>

Global Floating Window.

---

<div class="post-metadata">

**Author:** ![Carlo\_Rubini](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/94ad74/32.png) [@Carlo\_Rubini](https://forum.xojo.com/u/Carlo_Rubini)\
**Post date:** [July 22, 2020, 1:18am UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/14 "2020-07-22T01:18:00Z")

</div>

@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](https://github.com/vidalvanbergen/macoslib/tree/master-64bit)

---

<div class="post-metadata">

**Author:** ![Lennox\_Jacob1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Lennox\_Jacob1](https://forum.xojo.com/u/Lennox_Jacob1)\
**Post date:** [July 22, 2020, 2:33am UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/15 "2020-07-22T02:33:00Z")

</div>

Hi Carlo,

Is this compatible with the API2?

Thanks.

Lennox

---

<div class="post-metadata">

**Author:** ![Carlo\_Rubini](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/94ad74/32.png) [@Carlo\_Rubini](https://forum.xojo.com/u/Carlo_Rubini)\
**Post date:** [July 22, 2020, 3:10am UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/16 "2020-07-22T03:10:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 22, 2020, 7:03am UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/17 "2020-07-22T07:03:49Z")

</div>

> [@497490:@Sam Rowlands](#):
>
> Global Floating Window.

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.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 22, 2020, 7:04am UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/18 "2020-07-22T07:04:34Z")

</div>

[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](https://github.com/vidalvanbergen/macoslib/tree/master-64bit) [/quote]

Thank you Carlo 🙂

---

<div class="post-metadata">

**Author:** ![Lennox\_Jacob1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Lennox\_Jacob1](https://forum.xojo.com/u/Lennox_Jacob1)\
**Post date:** [July 22, 2020, 12:16pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/19 "2020-07-22T12:16:54Z")

</div>

Thanks Carlo.  
Lennox

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:07pm UTC](https://forum.xojo.com/t/bring-a-window-to-the-front/15109/20 "2020-10-29T17:07:55Z")

</div>


