Link to home
Start Free TrialLog in
Avatar of BeefJerky8732
BeefJerky8732

asked on

Code for Win32 InputBox

I'm looking for code for a Win32 InputBox.

Something like a MessageBox, but that can take an input like the VB InputBox.

I'm hoping someone has already done this, or knows where to get the code.
Avatar of alexo
alexo
Flag of Antarctica image

There's no such control in windows but you can make one yourself from an edit box and a couple of buttons.

Since you come from VB background you should probably use MFC (so better post your questions in the MFC area).

Check http://www.codeguru.com for tons of examples.
Avatar of AlexVirochovsky
AlexVirochovsky

What is your compiler: BC, VC++, BCB ... ?
Avatar of BeefJerky8732

ASKER

alexo,
>>There's no such control in windows but you can make one
>>yourself
I know that there is no such control, and I know I can make one.
I'm looking for one that is already made.
If someone has already done this, there's no use in me reinventing the wheel.

I'm looking for any available finished code.

>>Since you come from VB background you should probably
>>use MFC
I'm sorry, but I don't want an MFC solution.  I want something that can work in both Win32 and MFC code, and that's why I want a Win32 function/code.  No MFC.

AlexVirochovsky
>>What is your compiler: BC, VC++, BCB ... ?
I have all three compilers (BC, VC++, BCB).
I'm looking for something that can work with all three compilers, and that's why I want a Win32 solution.

I'm not asking anyone to do the code, I just looking if anyone has finished code for this requirement.

But if someone wants to do the code for me, I certainly will not complain, and I can increase this up to 300 points.

Thanks in advance.
BCB already has an InputBox function. It's declared in Dialogs.


extern PACKAGE AnsiString __fastcall InputBox(const AnsiString ACaption, const AnsiString APrompt, const
     AnsiString ADefault);


Good luck!!
DrDelphi,
Thanks, but I'm looking for something that will work in all three compilers.
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A self-contained little beauty.  No need to have a dialog box resource for it--- it makes its own.

To use it, simply...

#include InputBox.h


    CInputBox cImpBox( 0, "Prompt", "Title" );
    int nRet= cImpBox.DoModal();
// or
    char szBuf[100]="(initial value)";
    nRet= InputBox( 0, "Input something:", "Title", szBuf, sizeof(szBuf) );

If you want some pretty icons or other stuff, just modify the WM_INITDIALOG case code in the DlgProc.


-- Dan

Thanks Dan!
That is what I was looking for.
I doubled the points.
Thanks again.
Hi Dan,
I just have one question.  How can I modify your code to make it so that I can move the dialog.
The current code will not let me move the window.

Thanks in advance.
Also, is there a safe value to make IDC_EDIT1 equal to?

It's not defined in your code, so I made it equal to 1001 in the header.  But I'm worried that if I forget about this value, and I try using it in another project that is already using 1001 for a EDIT window, that this would cause problems.
Would this only be a problem if the conflicting IDC_??? where in the same window?
After playing with the code, I see that IDC_EDIT1 is wrong, and it should be CNUM_EditID.

But my question still stands.  What if I have another dialog, and it has an IDC_???? defined as 1002?
And or a read-only text field with ID of 1003?
Will there be a confict?
Also, how can I get the OK and Cancel button centered automatically?
>>The current code will not let me move the window.

You will need to use the MoveWindow API Fn in the WM_INITDIALOG case of the InputBoxDialogProc.  You could add member variables to the CInputDlg class and then use pcThis-> to access them.

>> Also, is there a safe value to make IDC_EDIT1 equal to

Use the constants that I have defined:

   const int CNUM_EditID= 1002;
   const int CNUM_PromptID= 1003;

>> What if I have another dialog, and it has an IDC_???? defined as 1002? And or a read-only text field with ID of 1003? Will there be a confict?

No.  There is no conflict because the control IDs are local to the dialog (think about it... IDOK and IDCANCEL exist for nearly all dlgs!).  There is no resource ID for the dialogbox itself, so there *can't* be a conflct with that.

>> Also, how can I get the OK and Cancel button centered automatically?

You will need to use the MoveWindow API Fn in the WM_INITDIALOG case of the InputBoxDialogProc.  For instance:

HWND hwndOK= GetDlgItem( hDlg, IDOK );
RECT rc;
GetWindowRect( hwndOK...
ScreenToClient(  hwndOK, ....
MoveWindow( hwndOK, ...

If this is difficult for you to do, make a new 100 pt question and I'll write the code (it is really outside the scope of this question, don't you think?).

-- Dan
>>If this is difficult for you to do, make a new 100 pt
>>question and I'll write the code

I think I can figure it out.

What I can not figure out, is how did you determine what setting to put in the pabDlgTmplt field.
What is your source???

If you can just point me to the right direction, I can find the info myself.

I figured out that it's an array of DLGTEMPLATE items.
But how do you know which one is for the buttons and which one is for the text fields and main window?
Where did you get your information from?