【问题标题】:How to display an input box in Mac OSX using c++如何使用 C++ 在 Mac OSX 中显示输入框
【发布时间】:2013-12-21 23:14:58
【问题描述】:

好的。情况就是这样,我是一名普通的 C++ 开发人员。我之前没有任何使用 Objective C 或 Cocoa 的经验。

我目前正在使用 Carbon 在 OSX 中开展一个项目 [我也是 carbon 方面的新手],并且已经使用纯 C++ 进行了 3 个月的编码。

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

这是我从谷歌上搜索到的一点点--

在 OSX 中可以使用 3 种方式来实现输入框

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

   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 是如何工作的,这是我在做 this 时得到的一点线索。我只有几个小时来完成这项工作。

选项 3 我找到了这个here

//
// 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!\nAborting\n";
    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;
}

【问题讨论】:

    标签: c++ xcode macos cocoa macos-carbon


    【解决方案1】:

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

    第三种选择是CFUserNotification

    【讨论】:

    • 我也不会选择 Carbon。我找到了另一种选择,使用来自 Cocoa 的 NSRunAlertPanel,我能够使用相同的按钮显示一个对话框,我现在正在检查如何将它与输入框结合起来。
    • 我也会查找 CFUserNotification,这看起来也很有趣;)干杯。
    【解决方案2】:

    使用 option2 : Cocoa 框架为您提供了所有的简单控件集、messagbox 等。但是没有输入框。 :(

    但是,您可以像我在这里所做的那样创建自定义输入框或修改警报面板:

    - (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;
        }
    }
    

    【讨论】:

    • 这个怎么转换成C++?
    • 我不认为你可以将它转换成 c++。但是你可以在你的项目中使用 obj-c 和 c++ 文件。
    • 看看我对问题所做的更改 :) 我能够使用它至少显示一个对话框,现在我想做的就是从用户那里获取输入
    • 您使用的 NSAlertPanel 现在您需要自定义,如我在回答中显示的那样。但我认为您需要将 cocoa-framework 添加到您的项目中。
    • 是的,我在 Xcode 中的构建中添加了 Cocoa 框架。我正在尝试自定义将其与您的答案混合的代码。我不太确定如何在 C++ 中使用 NSTextField 以及 initWithFrame 和 NSMakeRect,因为我说过我对 Obj C 的了解很少,你能指出我的正确资源吗?谢谢你和问候,Ashutosh。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多