Wednesday, September 21, 2011

Message Box Using CoreFoundation Framework

My previous example was created using Cocoa framework, but actually the same MessageBox can be displayed just by linking against CoreFoundation framework (not using Cocoa and/or Objective-C API).

This approach is useful when you're developing a minimalist GUI applications (maybe some Command-Line application, or a core utilities that doesn't require heavy load of GUI), but still want to use MessageBox for the purpose of information like error messages to the users.

Most user would prefer a simple but clear MessageBox message, rather than a plain text from the command line, which sometimes rather difficult to read and understand.


/* 
 test2.c 
 Mac OS X Message Box using only CoreFoundation framework.
 This code doesn't use Objective-C or Cocoa, just plain C.
 Build using commands:

gcc -framework CoreFoundation -o Test2 test2.c
mkdir -pv Test2.app/Contents/MacOS && mv -v Test2 Test2.app/Contents/MacOS

 Run the resulting by double-clicking Test2 from the Finder or by issuing
 command:
open Test2.app
 from the Terminal.

 */

#include <CoreFoundation/CoreFoundation.h>

int main(int argc, const char** argv)
{
SInt32 nRes = 0;
CFUserNotificationRef pDlg = NULL;
const void* keys[] = { kCFUserNotificationAlertHeaderKey,
kCFUserNotificationAlertMessageKey };
const void* vals[] = {
CFSTR("Test Foundation Message Box"),
CFSTR("Hello, World!")
};
CFDictionaryRef dict = CFDictionaryCreate(0, keys, vals,
                sizeof(keys)/sizeof(*keys), 
                &kCFTypeDictionaryKeyCallBacks
                &kCFTypeDictionaryValueCallBacks);
pDlg = CFUserNotificationCreate(kCFAllocatorDefault0
                     kCFUserNotificationPlainAlertLevel
                     &nRes, dict);
return 0;
}


The resulting application should look like the following:

Test2.app running.

Now you've learned how to display MessageBox without using Cocoa on Mac OS X.


2 comments: