本文介绍了在Mac OS X上的Pure C中创建窗口应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mac OSX上用纯C语言创建一个应用程序.我想要在女巫中创建窗口,我的应用程序将被存储.

I'm creating an application in pure C on Mac OSX.What I want is to create window in witch my app will be stored.

最好我希望它是纯C解决方案,但是如果我必须使用Objective-C类来初始化窗口,然后将上下文发送到我的C代码中,那就没问题了.

Preferably I want it to be pure C solution, but if I have to use objective-c class to init window and then send context to my C code then it will be fine.

我没有使用xcode,只有一个简单的文本编辑器尝试导入可可,但它却产生了很多错误.

I'm not using xcode, only simple text editor in with I tried to import cocoa but it just generated a lot of errors.

所以总的来说,我的问题是:在简单的纯C语言中如何生成将显示osx窗口的代码?

So in summary my question is:How in simple pure C generate code that will display osx window?

推荐答案

您可以使用 Objective-C运行时API 示例(iOS)用纯C语言创建iOS应用

You can use Objective-C runtime API example (iOS) Creating an iOS app in pure C

与obj-c中的代码相同:

Alternative the same code in obj-c :

echo '#import <Cocoa/Cocoa.h>
int main ()
    {
        @autoreleasepool{
            [NSApplication sharedApplication];
            [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
            id applicationName = [[NSProcessInfo processInfo] processName];
            id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 120, 120)
                styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
            [window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
            [window setTitle: applicationName];
            [window makeKeyAndOrderFront:nil];
            [NSApp activateIgnoringOtherApps:YES];
            [NSApp run];
        }
        return 0;
}' | gcc -fobjc-arc -framework Cocoa -x objective-c -o MicroApp - ; ./MicroApp

这将在1个窗口中运行Cocoa应用程序.喜欢下面的屏幕截图

This will run Cocoa app with 1 window. Like on screenshot below

您实际上可以使用NSMenu添加菜单

You can actually add menu using NSMenu

    id applicationMenuBar = [NSMenu new];
    id appMenuItem        = [NSMenuItem new];
    [applicationMenuBar addItem:appMenuItem];
    [NSApp setMainMenu: applicationMenuBar];

这篇关于在Mac OS X上的Pure C中创建窗口应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:44