# How to bring a window to foreground and/or have focus

**URL:** <https://discourse.libcinder.org/t/how-to-bring-a-window-to-foreground-and-or-have-focus/1176>\
**Category:** Using Cinder\
**Created:** [May 25, 2018, 6:43pm UTC](https://discourse.libcinder.org/t/how-to-bring-a-window-to-foreground-and-or-have-focus/1176 "2018-05-25T18:43:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 25, 2018, 6:43pm UTC](https://discourse.libcinder.org/t/how-to-bring-a-window-to-foreground-and-or-have-focus/1176/1 "2018-05-25T18:43:14Z")

</div>

Hi there, I don’t see any “Cinder App” way to set a window’s state to foreground, and to have mouse/keyboard focus. There is code (e.g. in `AppImplMsw` and `AppImplMswBasic`) that does this internally, but it’s not exposed…nor do I see it exposed on Mac. Am I missing something?

The only Cinder-API way to do both seems to be to call `setFullScreen(true)` (even if only temporarily), but of course this actually makes the window go full-screen, which I don’t want. I just want to give it focus and show it on top of other windows.

(There is also `setAlwaysOnTop(true)`, which brings it on top but does not give it focus…also, I don’t want it to be _always_ on top, just to set it there initially.)

Is there a workaround someone can suggest? I (on Windows) guess get the underlying HWND handle using `getNative()` and then call `::SetForegroundWindow(hwnd)` myself? (And whatever the equivalent is on macOS.)

Thanks,  
Glen.

---

<div class="post-metadata">

**Author:** ![benjaminbojko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/benjaminbojko/32/591_2.png) [@benjaminbojko](https://discourse.libcinder.org/u/benjaminbojko)\
**Post date:** [May 25, 2018, 6:49pm UTC](https://discourse.libcinder.org/t/how-to-bring-a-window-to-foreground-and-or-have-focus/1176/2 "2018-05-25T18:49:27Z")

</div>

Not sure if this is _the_ way to do it, but we usually use the HWND handle like you described to do just that:

```auto
#if defined(CINDER_MSW)
	HWND nativeWindow = (HWND)getWindow()->getNative();
	::SetForegroundWindow(nativeWindow);
#endif

```

Have been using it in production for a while now w/o issues.

---

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 25, 2018, 6:56pm UTC](https://discourse.libcinder.org/t/how-to-bring-a-window-to-foreground-and-or-have-focus/1176/3 "2018-05-25T18:56:12Z")

</div>

Yeah, that’s what I’m doing for now, too. Plus one more step of also setting the focus:

```auto
#if defined(CINDER_MSW)
    // at top of file, add #include <windows.h> (also within a CINDER_MSW block)
    auto nativeWindow = static_cast<HWND>(getWindow()->getNative());
    ::SetForegroundWindow(nativeWindow);
    ::SetFocus(nativeWindow);
#endif // CINDER_MSW

```

Thanks.

---

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 27, 2018, 5:57pm UTC](https://discourse.libcinder.org/t/how-to-bring-a-window-to-foreground-and-or-have-focus/1176/4 "2018-05-27T17:57:08Z")

</div>

For completeness, here’s the equivalent I’m doing on macOS…may not be “recommended practice”, but it seems to do what I need:

```auto
#if defined(CINDER_MAC)
    auto view = static_cast<NSView*>(getWindow()->getNative());
    NSWindow* win = [view window];
    [win makeKeyAndOrderFront: nil];
    [NSApp activateIgnoringOtherApps:YES];
#endif // CINDER_MAC

```

(Obviously the above needs to be in a .mm file in your Xcode project, or in a file set up to compile as Objective-C++.)
