Thread: Win32 API (inputbox)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    40

    Win32 API (inputbox)

    Hello there;
    I was wondering, in windows programming, if there was a simple dialoge box or message box that takes the user input (as a string from keyboard) to be processed later on. I know there is no such thing as simple in win32API, so if you could explain to me just a bit of how it works so I can implement it. Any help would be most appreciated.........Thanks.
    Last edited by Salem; 06-11-2011 at 10:23 AM. Reason: colour didn't add anything useful

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Do you already have a dialog? What IDE / compiler?

    Add a textbox or edit control to the dialog (using the resource editor) and give it the ID of IDC_EDIT1 (or something more meaningful to your app)
    When you need the text call GetDlgItemText() or GetWindowText()
    Or process the edit's EN_CHANGE msgs to get each character as the user types them in (in the callbacks WM_COMMAND handler for the edit).

    Make sure you use static variables if the variable is declared inside the callback.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    I am using Visual C++ 2010 Express Edition

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Do any of GetDlgItemText() or GetWindowText() take any arguments.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Blacky Ducky View Post
    Do any of GetDlgItemText() or GetWindowText() take any arguments.
    You should either download the Windows SDK or look these things up on MSDN as you are coding... Yes, both take arguments.

    You might also find the forger's win32 api tutorial helpful... theForger's Win32 API Tutorial

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    so if id of text box is IDC_EDIT1 ,function call is GetWindowText(IDC_EDIT1,name,sizeof(name)) ?

    char name[20];

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nutzu2010 View Post
    so if id of text box is IDC_EDIT1 ,function call is GetWindowText(IDC_EDIT1,name,sizeof(name)) ?

    char name[20];
    If it's in a dialog, you want to use the GetDlgItemText() instead... GetWindowText() requires a window handle not an id number.

    Code:
    char txt[100] = {0};
    
    GetDlgItemText(hDialog,IDC_EDIT1,txt,99);
    You also can't use sizeof() in this situation because size of txt is going to amount to the size of the pointer (2 or 4) not the length of the array (100).

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by CommonTater View Post
    You also can't use sizeof() in this situation because size of txt is going to amount to the size of the pointer (2 or 4) not the length of the array (100).
    No it isn't.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    hDialog-handle for a dialog then How do you get the handle for a dialog?is his id number?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nutzu2010 View Post
    hDialog-handle for a dialog then How do you get the handle for a dialog?is his id number?
    The dialog's handle is passed in the message loop for the dialog box...

    Code:
    INT_PTR CALLBACK DialogProc(
        HWND hwndDlg,    
        UINT uMsg,
        WPARAM wParam,
        LPARAM lParam
    );
    So when ever the user changes something, a message is passed and you can hand your function the dialog handle...

    Here's an example from some live code to give you the idea...

    Code:
    
    
    // message tosser
    BOOL CALLBACK SetupTosser(HWND Dlg, UINT Msg, WPARAM Wparm, LPARAM Lparm)
      { switch (Msg)
          { case WM_INITDIALOG :
             SendMessage(GetDlgItem(Dlg,4002),EM_LIMITTEXT,20,0); 
             ImportSettings(Dlg,Lparm);
              return 1;
            case WM_HELP :
              ShowHelp(Dlg,L"setup");
              return 1;
            case WM_COMMAND:          // buttons
              switch (LOWORD(Wparm))
                { case 4001 :
                    if (HIWORD(Wparm) == EN_KILLFOCUS)
                      CheckRange(Dlg,4001,1000,60000);
                    return FALSE;
                  case 4003 :
                    if (HIWORD(Wparm) == EN_KILLFOCUS)
                      CheckRange(Dlg,4003,5,200);
                    return FALSE;
                  case 4004 :
                    if (HIWORD(Wparm) == EN_KILLFOCUS)
                      CheckRange(Dlg,4004,5,200);
                    return FALSE;          
                  case 4005 :
                    if (HIWORD(Wparm) == EN_KILLFOCUS)
                      CheckRange(Dlg,4005,1,60);
                    return FALSE;
                  case 4006 :
                    if (HIWORD(Wparm) == EN_KILLFOCUS)
                      CheckRange(Dlg,4006,100,10000);
                    return FALSE;           
                  case 4050 :         // help
                    ShowHelp(Dlg,L"setup");
                    return 1;
                  case IDOK :
                  case 4051 :         // OK
                    ExportSettings(Dlg);
                    return FALSE;
                  case IDCANCEL :
                  case 4052 :         // Cancel
                    EndDialog(Dlg,0);
                    return FALSE; } } 
        return FALSE; }
    Notice how the dialog's messageloop (SetupTosser() ) passes the various parts of the call to each individual function... including the dialog box handle; Dlg.

    You really should visit the forger's tutorial I linked you to in a previous message... he explains it much better than I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  2. Win32 with C++
    By Jaken Veina in forum Windows Programming
    Replies: 5
    Last Post: 07-06-2005, 12:16 PM
  3. WIN32 Help
    By St0rmTroop3er in forum Windows Programming
    Replies: 13
    Last Post: 09-30-2003, 05:52 PM
  4. InputBox
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 09-24-2003, 12:45 PM
  5. Win32 API Example?
    By drdroid in forum Windows Programming
    Replies: 4
    Last Post: 09-16-2002, 10:59 AM