Thursday, September 22, 2011

Simplified Version of CoreFoundation MessageBox

My previous example of using CoreFoundation demonstrating a rather complicated code for just showing a MessageBox, but actually we can do it very straightforward by just making a single call like the following example:

/*
test3.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 Test3 test3.c
mkdir -pv Test2.app/Contents/MacOS && mv -v Test3 Test3.app/Contents/MacOS
Run the resulting by double-clicking Test3 from the Finder or by issuing
command:
open Test3.app
from the Terminal.
*/

#include <CoreFoundation/CoreFoundation.h>

int main(int argc, const char** argv)
{
CFOptionFlags nRet;
SInt32 nRes = CFUserNotificationDisplayAlert(0, kCFUserNotificationPlainAlertLevel,
NULL, NULL, NULL,
CFSTR("Testing"),
CFSTR("This is a test for direct dialog."),
CFSTR("OK"), NULL, NULL, &nRet);
return 0;
}

Compile the code using the above command lines, you will get the same result as before.

Now, if you don't even care about any user response hold by nRet variable (just like most Information MessageBox), you can simply replace the content of function main() above with these lines:

return CFUserNotificationDisplayNotice(0, kCFUserNotificationPlainAlertLevel,
NULL, NULL, NULL,         CFSTR("Testing"),
                        CFSTR("This is a test for direct dialog"),
CFSTR("OK"));

And of course, if you want, you can make a simple function just to be able to make a call like the following:

MessageBox(CFSTR("Testing"), CFSTR("This is a test"), CFSTR("OK"));

But I don't think that's necessary :-)


No comments:

Post a Comment