好的。情况是这样的,我是一名普通的 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;
}
最佳答案
所有 Carbon UI 都已弃用,不能用于 64 位开发。
第三种选择是 CFUserNotification .
关于c++ - 如何使用 C++ 在 Mac OSX 中显示输入框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20392802/