How to force Mac window to foreground?
Asked Answered
S

2

6

How can I programmatically force a mac window to be the front window? I have the window handle, and want to ensure that my window is displayed above all other windows. I can use both Carbon & Cocoa for this.

Sulfide answered 4/5, 2010 at 8:29 Comment(2)
Are you talking about a window in your own program? Or do you want to deal with other app's windows?Reconstruct
Raymond Chen wrote about this very problem, when he asked the important question: "What if two programs did this?" Once you realize that, you'll understand that never can force this, only request this.Aura
I
13

For Cocoa, you can set the window level using:

[window setLevel:NSFloatingWindowLevel];

A floating window will display above all other regular windows, even if your app isn't active.

If you want to make your app active, you can use:

[NSApp activateIgnoringOtherApps:YES];

and

[window makeKeyAndOrderFront:nil];
Intake answered 4/5, 2010 at 16:49 Comment(0)
K
0

If you can (32 bit only) use kOverlayWindowClass:

WindowRef carbon_window = NULL;
CreateNewWindow( kOverlayWindowClass , ... , &carbon_window );
// if you need cocoa:
NSWindow *cocoa_window = [[NSWindow alloc] initWithWindowRef:carbon_window];

Otherwise create an NSWindow and set the window level to kCGOverlayWindowLevel.

Note that this will be above the dashboard as well. If you want to be below the dashbord use kCGUtilityWindowLevel.

Khat answered 4/5, 2010 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.