如何使用c++在Mac OSX中显示输入框

How to display an input box in Mac OSX using c++

本文关键字:显示 输入 OSX Mac 何使用 c++      更新时间:2023-10-16

好的。情况是这样的,我是一个普通的c++开发人员。我之前没有任何与Objective C或Cocoa合作的经验。

我目前正在OSX与Carbon合作一个项目[我也是Carbon方面的新手],并且已经用纯C++编码了3个月。

现在我面临的问题是,我必须向用户显示一个输入框,并从他那里获得一些输入,比如USERNAME,但我实际上不知道输入框在OSX中是如何显示的,我的选项是什么。我已经在win32中完成了编程,所以,几个小时的阅读和一个小时的编程应该可以帮我完成这项工作。我只需要一些帮助,为我指明正确的方向。

以下是我从谷歌上搜索到的内容——

有3种方法可用于在OSX中实现输入框

1-使用carbon,我已经能够使用它显示一个简单的对话框。我不知道如何使用那里的输入框。。这是我为输入框尝试的代码。。

   DialogRef    dialog = GetNewDialog (128, NULL, (WindowRef)-1);
   WindowRef lay;
   ControlRef   outControl;
   Rect boundsRect;
   boundsRect.top = 0;
   boundsRect.left = 0;
   boundsRect.right = 200;
   boundsRect.bottom = 99;

   lay = GetDialogWindow(dialog);
   CreateEditTextControl (lay, &boundsRect, NULL, false, true, NULL, &outControl);
   InstallStandardEventHandler(GetWindowEventTarget (lay));
   ShowWindow (lay);

当我运行程序时,我什么都看不见,Xcode在CreateEditTextControl表示已弃用。

选项2我的选择是将Objective C和C++结合起来,但我不知道Objective C是如何工作的,下面是我在这方面获得的一些线索。我只有几个小时的时间来完成这件事。

选项3我在这里找到的。

//
// test1.cpp
// This program shows how to access Cocoa GUI from pure C/C++
// and build a truly functional GUI application (although very simple).
// Compile using:
//   g++ -framework Cocoa -o test1 test1.cpp
//
// that will output 'test1' binary.
//

#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <iostream>
extern "C" int NSRunAlertPanel(CFStringRef strTitle, CFStringRef strMsg,
                           CFStringRef strButton1, CFStringRef strButton2, 
                           CFStringRef strButton3, ...);

int main(int argc, char** argv)
{
id app = NULL;
id pool = objc_getClass("NSAutoreleasePool");
if (!pool)
{
    std::cerr << "Unable to get NSAutoreleasePool!nAbortingn";
    return -1;
}
pool = objc_msgSend(pool, sel_registerName("alloc"));
if (!pool)
{
    std::cerr << "Unable to create NSAutoreleasePool...nAborting...n";
    return -1;
}
pool = objc_msgSend(pool, sel_registerName("init"));
app = objc_msgSend(objc_getClass("NSApplication"),
                   sel_registerName("sharedApplication"));
NSRunAlertPanel(CFSTR("Testing"),
                CFSTR("This is a simple test to display NSAlertPanel."),
                CFSTR("OK"), NULL, NULL);
objc_msgSend(pool, sel_registerName("release"));
return 0;
}

所有Carbon UI都已弃用,不能用于64位开发。

第三种选择是CFUSER通知。

使用option2:Cocoa框架为您提供了所有简单控件集、messagbox和其他控件。但是没有inputBox.:(

然而,你可以创建你的自定义inputBox或修改警报面板,就像我在这里做的那样:

- (NSString *)inputBox: (NSString *)prompt{
    NSAlert *alert = [NSAlert alertWithMessageText: prompt
                                     defaultButton:@"OK"
                                   alternateButton:@"Cancel"
                                       otherButton:nil
                         informativeTextWithFormat:@""];
    NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
    [alert setAccessoryView:input];
    NSInteger button = [alert runModal];
    if (button == NSAlertDefaultReturn) {
        [input validateEditing];
        return [input stringValue];
    }
    else if (button == NSAlertAlternateReturn) {
        return nil;
    }
    else {
        return nil;
    }
}