Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompting a user with an input box? [C++]

My goal is to simply use a pop-up box to ask the user for an input. I've searched around quite a bit and pretty much all the results say that creating a messageBox is really easy:

MessageBox (NULL, "Hello World" , "Hello", MB_OKCANCEL);

But that creating a pop-up that takes input is more involved and there isn't a straight forward way to do it. All of the results I could find on Google were dated somewhere from 2001 to 2005. I guess I'm here asking if some more straight forward solution has come about in recent years.

Hopefully something nice and straight forward like in Java:

int number = JOptionPane.showInputDialog ("Enter an integer");

If that isn't the case, could I get a brief explanation of how to do it?


Edit: I couldn't get anything to work. :( I ended up writing the code to do the work in Java, and then wrote one line of C++ code to call the .jar file. :-/ Since the issue was time sensitive, it was better than nothing.

like image 826
Ryan Avatar asked Nov 17 '10 04:11

Ryan


2 Answers

If you are using Visual C++ Express there are a number of free resource editors that can be used to create dialogs. ResEdit is one of the better ones I've found.

You need to create a dialog resource in a .RC file that you add to your project.

Then, It is a very simple case of calling DialogBox - which will load the dialog box from your resource file and place it on the screen. The passed in DialogProc will be called with a number of notifications. Typically you would want to return FALSE for everything, but handle WM_INITDIALOG as a place to initialize the edit control with text, and WM_COMMAND will be sent when a button is clicked.

like image 81
Chris Becke Avatar answered Sep 28 '22 04:09

Chris Becke


There is nothing like that for pure C++. Basically what you're trying to do can only be achieved by using an API call to the OS or by using some GUI library like Qt (which I recommend cause it's waaaaay easier then calling native APIs and it's also multi-platform)

Using Qt you can show an input dialog pretty much the same way you do it on java:

bool ok;
QString text = QInputDialog::getText(
        "MyApp 3000", "Enter your name:", QLineEdit::Normal,
        QString::null, &ok, this );
if ( ok && !text.isEmpty() ) {
    // user entered something and pressed OK
} else {
    // user entered nothing or pressed Cancel
}

You can download the Qt library here: qt.nokia.com/products/developer-tools/

like image 33
Raphael Avatar answered Sep 28 '22 04:09

Raphael