如何使用C ++在Mac OSX中显示输入框 [英] How to display an input box in Mac OSX using c++

查看:168
本文介绍了如何使用C ++在Mac OSX中显示输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.在这种情况下,我是一名普通的c ++开发人员.我以前没有使用Objective C或Cocoa的经验.

Ok. here is the situation, I am an average c++ developer. I do not have any prior experience of working with Objective C or Cocoa for that matter.

我正在与Carbon一起在OSX中进行一个项目[我在碳方面也同样是NOVICE],并且已经用纯C ++进行了3个月的编码.

I am working on a project currently in OSX with Carbon [I am NOVICE in carbon as well], and been coding in pure C++ for 3 months.

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

Now I am facing an issue where I have to display an input box to the user, and get some input from him, say USERNAME, having literally no knowledge of how input boxes are displayed in OSX, what are my options. I have done programming in win32, so, a couple of hours of reading and 1 hour of programming should do the job for me. I just need a little help pointing me in right direction.

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

Here is what I have got from Googling a little bit--

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

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

1- Use carbon, I have been able to display a simple dialog box using it. I don't know how I can use input boxes there.. Here is the code I tried for Input Box..

   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说它已被弃用.

I was not able to see anything when I ran the program, and Xcode showed a warning on CreateEditTextControl saying it is deprecated.

选项2 ,我要结合使用Objective C和C ++,但是我不知道目标C的工作原理,这是我在

Option 2 which I have is to combine Objective C and C++, but I do not know how objective C works, here is a little lead I got in doing this. I only have a few hours to accomplish this.

选项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!\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;
}

推荐答案

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

Using option2 : Cocoa framework provides you all sets of simple controls, messagbox and others. But there are no inputBox. :(

但是,您可以像我在这里一样创建自定义inputBox或修改Alertpanel:

However you can create your custom inputBox or modify the alertpanel as I did here:

- (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 ++在Mac OSX中显示输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆