⽤纯CC++语⾔开发MacOSCocoa应⽤程序
本源码是⽤纯C/C++语⾔开发MacOS Cocoa应⽤程序,希望起到抛砖引⽟的作⽤。
本源码100%原创,绝对是国内外⽹上稀缺甚⾄没有的资料。作为开发MacOS Cocoa GUI的参考样例,值得你拥有。
编译:clang++ MacCocoaButton.cpp -std=c++2a -fobjc-arc -framework Cocoa -o mcb北京五行属什么
MacCocoaButton.cpp
#define OBJC_OLD_DISPATCH_PROTOTYPES 1 // 注意,这句很重要,否则不通过编译。
#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 NSWindowCloButton 0
#define NSButtonTypeMomentaryPushIn 7怎么粉碎文件
void ButtonClick(id idSender, SEL lCMD)
{
objc_msgSend(idSender, l_registerName("stop:"), idSender);
}
int main(int argc, char *argv[])
{
struct CPoint
子不嫌母丑{
double x;
double y;
};
struct CSize
{会计英语
double width;
double height;
};
struct CRect
{
CPoint origin;
CSize size;
};
id idAutoreleaPool = objc_msgSend(objc_msgSend((id)objc_getClass("NSAutoreleaPool"), l_registerName("alloc")), l_registerName("init"));
id idApplication = objc_msgSend((id)objc_getClass("NSApplication"), l_registerName("sharedApplication"));
CRect crBoundary = {0, 0, 640, 480};
unsigned long ulWindowStyleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
unsigned long ulBackingStoreType = NSBackingStoreBuffered;
id idWindow = objc_msgSend(objc_msgSend((id)objc_getClass("NSWindow"), l_registerName("alloc")),
l_registerName("initWithContentRect:styleMask:backing:defer:"), crBoundary, ulWindowStyleMask, ulBackingStoreType, NO);
objc_msgSend(idWindow, l_registerName("makeFirstResponder:"), idWindow);
objc_msgSend(idWindow, l_registerName("center"));
objc_msgSend(idWindow, l_registerName("tTitle:"), CFSTR("窗⼝测试例⼦"));
objc_msgSend(idWindow, l_registerName("makeKeyAndOrderFront:"), idWindow);
objc_msgSend(idWindow, l_registerName("makeMainWindow"));
objc_msgSend(idWindow, l_registerName("tLevel:"), CGShieldingWindowLevel());
class_addMethod(object_getClass(idApplication), l_registerName("ButtonClick"), (IMP)ButtonClick, "v@:");
id idCloButton = objc_msgSend(idWindow, l_registerName("standardWindowButton:"), NSWindowCloButton); objc_msgSend(idCloButton, l_registerName("tTarget:"), idApplication);
objc_msgSend(idCloButton, l_registerName("tAction:"), l_registerName("ButtonClick"));
crBoundary = {296, 228, 48, 24};
id idExitButton = objc_msgSend(objc_msgSend((id)objc_getClass("NSButton"), l_registerName("alloc")),
l_registerName("initWithFrame:"), crBoundary);
objc_msgSend(idExitButton, l_registerName("tButtonType:"), NSButtonTypeMomentaryPushIn);
外的词语objc_msgSend(idExitButton, l_registerName("tTitle:"), CFSTR("退出"));
objc_msgSend(idExitButton, l_registerName("tTarget:"), idApplication);
objc_msgSend(idExitButton, l_registerName("tAction:"), l_registerName("ButtonClick"));
objc_msgSend((id)objc_msgSend(idWindow, l_registerName("contentView")), l_registerName("addSubview:"), idExitButton);
objc_msgSend(idApplication, l_registerName("run"));
objc_msgSend(idApplication, l_registerName("terminate:"), idApplication);
objc_msgSend(idAutoreleaPool, l_registerName("drain"));
烧平菇return 0;
}