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

Ryan picture Ryan · Nov 17, 2010 · Viewed 36.4k times · Source

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.

Answer

Chris Becke picture Chris Becke · Nov 17, 2010

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.