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

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.

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

#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.

1 Like

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

#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.

3 Likes

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

#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++.)

2 Likes