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
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.)
Not sure if this is the way to do it, but we usually use the HWND handle like you described to do just that:
HWND nativeWindow = (HWND)getWindow()->getNative();
Have been using it in production for a while now w/o issues.
Yeah, that’s what I’m doing for now, too. Plus one more step of also setting the focus:
// at top of file, add #include <windows.h> (also within a CINDER_MSW block)
auto nativeWindow = static_cast<HWND>(getWindow()->getNative());
#endif // CINDER_MSW
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 view = static_cast<NSView*>(getWindow()->getNative());
NSWindow* win = [view window];
[win makeKeyAndOrderFront: nil];
#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++.)