纯c++编写macos窗口程序

				
					//文件MacCocoaApplication.cpp


#define    OBJC_OLD_DISPATCH_PROTOTYPES    1 // 注意,这句很重要,否则不通过编译。

#include <stdio.h>
#include <objc/message.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreFoundation/CoreFoundation.h>

#define    NSWindowStyleMaskTitled    1

#define    NSWindowStyleMaskClosable    2

#define    NSWindowStyleMaskMiniaturizable    4

#define    NSWindowStyleMaskResizable    8

#define    NSBackingStoreBuffered    2

#define    NSWindowCloseButton    0

#define    NSTerminateNow    1

void ApplicationWillFinishLaunching(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Will Finish Launching\n");
}

void ApplicationDidFinishLaunching(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Did Finish Launching\n");
}

bool ApplicationShouldAutomaticallyLocalizeKeyEquivalents(id idSender, SEL selCMD, id idApplication)
{
    printf("Application Should Automatically Localize Key Equivalents\n");

    return YES;
}

void ApplicationWillBecomeActive(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Will Become Active\n");
}

void ApplicationDidBecomeActive(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Did Become Active\n");
}

void ApplicationWillResignActive(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Will Resign Active\n");
}

void ApplicationDidResignActive(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Did Resign Active\n");
}

unsigned long ApplicationShouldTerminate(id idSender, SEL selCMD, id idApplication)
{
    printf("Application Should Terminate\n");

    return NSTerminateNow;
}

bool ApplicationShouldTerminateAfterLastWindowClosed(id idSender, SEL selCMD, id idApplication)
{
    printf("Application Should Terminate After Last Window Closed\n");

    return YES;
}

void ApplicationWillTerminate(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Will Terminate\n");
}

void ApplicationWillHide(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Will Hide\n");
}

void ApplicationDidHide(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Did Hide\n");
}

void ApplicationWillUnhide(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Will Unhide\n");
}

void ApplicationDidUnhide(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Did Unhide\n");
}

void ApplicationWillUpdate(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Will Update\n");
}

void ApplicationDidUpdate(id idSender, SEL selCMD, id idNotification)
{
    printf("Application Did Update\n");
}

bool ApplicationShouldHandleReopen_HasVisibleWindows(id idSender, SEL selCMD, id idApplication, bool blFlag)
{
    printf("Application Should Handle Reopen Has Visible Windows\n");

    return YES;
}

void CloseClick(id idSender, SEL selCMD)
{
    objc_msgSend(idSender, sel_registerName("stop:"), idSender);
}

int main(int argc, char *argv[])
{
    extern id NSApp;

    id idAutoreleasePool = objc_msgSend(objc_msgSend((id)objc_getClass("NSAutoreleasePool"), sel_registerName("alloc")), sel_registerName("init"));

    objc_msgSend((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication"));

    if(NSApp == NULL)
    {
        fprintf(stderr, "Failed to initialized NSApplication.\n");

        return -1;
    }

    class_addMethod(object_getClass(NSApp), sel_registerName("CloseClick"), (IMP)CloseClick, "v@:");

    Class clsAppDelegate = objc_allocateClassPair((Class)objc_getClass("NSObject"), "AppDelegate", 0);

    class_addMethod(clsAppDelegate, sel_registerName("applicationWillFinishLaunching:"), (IMP)ApplicationWillFinishLaunching, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationDidFinishLaunching:"), (IMP)ApplicationDidFinishLaunching, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationShouldAutomaticallyLocalizeKeyEquivalents:"),
        (IMP)ApplicationShouldAutomaticallyLocalizeKeyEquivalents, "B@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationWillBecomeActive:"), (IMP)ApplicationWillBecomeActive, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationDidBecomeActive:"), (IMP)ApplicationDidBecomeActive, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationWillResignActive:"), (IMP)ApplicationWillResignActive, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationDidResignActive:"), (IMP)ApplicationDidResignActive, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationShouldTerminate:"), (IMP)ApplicationShouldTerminate, "L@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationShouldTerminateAfterLastWindowClosed:"),
        (IMP)ApplicationShouldTerminateAfterLastWindowClosed, "B@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationWillTerminate:"), (IMP)ApplicationWillTerminate, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationWillHide:"), (IMP)ApplicationWillHide, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationDidHide:"), (IMP)ApplicationDidHide, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationWillUnhide:"), (IMP)ApplicationWillUnhide, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationDidUnhide:"), (IMP)ApplicationDidUnhide, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationWillUpdate:"), (IMP)ApplicationWillUpdate, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationDidUpdate:"), (IMP)ApplicationDidUpdate, "v@:@");

    class_addMethod(clsAppDelegate, sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:"),
        (IMP)ApplicationShouldHandleReopen_HasVisibleWindows, "B@:@B");

    objc_registerClassPair(clsAppDelegate);

    objc_msgSend(NSApp, sel_registerName("setDelegate:"), objc_msgSend(objc_msgSend((id)objc_getClass("AppDelegate"), sel_registerName("alloc")),
        sel_registerName("init")));

    unsigned long ulWindowStyleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;

    unsigned long ulBackingStoreType = NSBackingStoreBuffered;

    id idWindow = objc_msgSend(objc_msgSend((id)objc_getClass("NSWindow"), sel_registerName("alloc")),
        sel_registerName("initWithContentRect:styleMask:backing:defer:"), CGRect{0, 0, 640, 480}, ulWindowStyleMask, ulBackingStoreType, NO);

    objc_msgSend(idWindow, sel_registerName("makeFirstResponder:"), idWindow);

    objc_msgSend(idWindow, sel_registerName("center"));

    objc_msgSend(idWindow, sel_registerName("setTitle:"), CFSTR("MacOS Cocoa程序通用模版"));
    
    objc_msgSend(idWindow, sel_registerName("makeKeyAndOrderFront:"), idWindow);

    objc_msgSend(idWindow, sel_registerName("makeMainWindow"));

    objc_msgSend(idWindow, sel_registerName("setLevel:"), CGShieldingWindowLevel());

    id idCloseButton = objc_msgSend(idWindow, sel_registerName("standardWindowButton:"), NSWindowCloseButton);
    
    objc_msgSend(idCloseButton, sel_registerName("setTarget:"), NSApp);

    objc_msgSend(idCloseButton, sel_registerName("setAction:"), sel_registerName("CloseClick"));

    objc_msgSend(NSApp, sel_registerName("run"));

    objc_msgSend(NSApp, sel_registerName("terminate:"), NSApp);

    objc_msgSend(idAutoreleasePool, sel_registerName("drain"));

    return 0;
}
				
			
				
					#编译命令

clang++ MacCocoaButton.cpp -std=c++2a -fobjc-arc -framework Cocoa -o mcb
				
			

发表评论

您的电子邮箱地址不会被公开。