简体   繁体   中英

How to display a modal message box in C++ on Mac?

CFUserNotificationDisplayAlert and CFUserNotificationDisplayNotice creates a non-modal window and this is bad because it could bring your application UI in a very undesired state if you select the original application window (the message box is hidden but the applicaton does not respond).

The old SystemAlert was modal but this one doesn't fully support Unicode strings.

How can i display a message box as a modal window under Mac? I'm looking for someting similar to MessageBox from Windows?

I've implemented it with CFUserNotificationDisplayAlert and it doesn't return until the user closes the MessageBox.

If you want to take a look of the code, I have it in MessageBox function in Mac there you will find a MessageBox function implemented for mac, it's only implemented for MB_OKCANCEL, but with little more code could cover the entire MessageBox flags and return values, is a good starting point.

It looks that CreateStandardAlert is the right solution because this one is modal .


DialogRef theItem;
DialogItemIndex itemIndex;
CreateStandardAlert(kAlertNoteAlert, CFSTR("aaa"), CFSTR("bbb"), NULL, &theItem);
RunStandardAlert(theItem, NULL, &itemIndex);

Have a look at NSBeginAlertSheet function or at NSApp's:

- (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow
      modalDelegate:(id)modalDelegate didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo

may be its what you want. Here is also a nice article about working with sheets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM