如何使用c++在Mac OSX中显示输入框
How to display an input box in Mac OSX using c++
好的。情况是这样的,我是一个普通的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;
}
}
相关文章:
- 显示基于用户输入的整数的字符
- 输入值后,如何在一个括号下显示值,例如"{6,7,8,9}
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 我要求用户输入大小和数组,但是当我打印矢量时,它仅显示'0'作为输出
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- 下划线不会与"发送输入"一起显示C++
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- 通过功能进行2D矢量输入和显示
- 在数组中显示第一个元素用户输入
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- 程序输入名称和高度并显示它。我不知道如何显示列表中最高人的姓名和身高
- 如何向用户显示特定错误,要求他/她使用循环再次提供输入?
- isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 如何让我的输出显示输入金额的星号
- 使用ifstreamwhile循环,如何显示输入错误并在下一行继续
- C++:帮助存储和显示输入文件变量
- 如何使用c++在Mac OSX中显示输入框
- 不显示输入的第二组信息