Hi, I'm not sure if this is the right forum to post this thread in, but here it is.

Even though I program on the Mac OS X platform, and am quite good at it, I'm really doing game programming, so I never really learned much Cocoa or Carbon. Can someone tell me the Carbon function for a message box? Thanks a lot.

Dani AI

Generated

If you want the Carbon-style equivalent of a Windows MessageBox, use the Dialog Manager’s Standard Alert APIs. In 32-bit Carbon you either call StandardAlert (one-shot) or create and run it explicitly with CreateStandardAlert + RunStandardAlert. The button you get back is a DialogItemIndex such as kAlertStdAlertOKButton, kAlertStdAlertCancelButton, etc. (leopard-adc.pepas.com)

#include <Carbon/Carbon.h>

static void ShowCarbonAlert(void) {
    AlertStdCFStringAlertParamRec params;
    GetStandardAlertDefaultParams(&params, kStdCFStringAlertVersionOne);
    DialogRef alert = NULL;
    if (CreateStandardAlert(kAlertNoteAlert,
                            CFSTR("Hello from Carbon"),
                            CFSTR("Choose an option."),
                            &params, &alert) == noErr) {
        DialogItemIndex item = 0;
        RunStandardAlert(alert, NULL, &item);
        // item == kAlertStdAlertOKButton, kAlertStdAlertCancelButton, etc.
    }
}

That said, is right about sheets being the norm on macOS. The modern approach is AppKit’s NSAlert, shown as a sheet attached to your window: beginSheetModalForWindow:completionHandler:. This matches the Human Interface Guidelines for alerts on macOS. (developer.apple.com)

Important caveat: Carbon UI is not available to 64-bit apps, and since macOS Catalina (10.15, released October 2019) macOS no longer runs 32-bit apps at all. If you need to ship on current macOS, prefer NSAlert (Objective-C/Swift) or a cross‑platform wrapper. (leopard-adc.pepas.com)

For C-only utilities or background tools, there is also CFUserNotificationDisplayAlert, intended for simple system-styled alerts without pulling in AppKit. It is handy for daemons or login items where you just need a modal prompt. (developer.apple.com)

Since you mentioned game dev, a lightweight, cross‑platform option is SDL’s SDL_ShowSimpleMessageBox, which gives you a quick modal dialog without adopting Cocoa. Useful if your engine already uses SDL. (wiki.libsdl.org)

And ’s idea of mapping Windows MessageBox semantics is fine: just translate your button set and return codes to the NSAlert or Carbon constants above.

Recommended Answers

All 2 Replies

Message box? In Mac OS X you generally don't use a message box, or when you do, they fold down from the top of the window. Assuming you have a window already, this is the way you should do things. You'll probably have better luck looking in that direction. Maybe somebody will come along with a slicker answer than that.

I've done a MessageBox function for mac, you will find the code in my there you will find a function that create a modal message box and returns the selected value depending on the pressed button.
The code, with few more lines of code will be a complete equivalent of window's MessageBox function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.