Thread: Get user to input text

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Get user to input text

    I want to create a hang man game and need some way to have the user input text. Is there some function like MessageBox, but instead of outputting text you receive input, or do I have to make one myself.

    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You most likely want an edit control, which will have to be in a window or dialog of some kind.

    If you aren't familiar with Windows programming this can be quite the challenge...

    theForger's Win32 API Tutorial

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    You have to make one yourself. The function you want to write is called InputBox. It's not a standard function in C but surprisingly it is in other languages. Check out this code for ideas on how to implement it: C/C++ InputBox win32 implementation

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by unselected View Post
    You have to make one yourself. The function you want to write is called InputBox. It's not a standard function in C but surprisingly it is in other languages. Check out this code for ideas on how to implement it: C/C++ InputBox win32 implementation
    That is not a stand alone implementation, it is a wrapper on top of WIN32 (like MFC, which is direct from Microsoft and very well tested). It requires understanding of WIN32 apps to use.

    theForger's tutes are a much better starting point for what the OP wants.
    "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

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Here is the stand alone implementation that I use:

    Code:
    #include <windows.h>
    #include "resource.h"
    
    //Global variables
    int QueryLen;
    char *QueryCaption, *QueryText, *QueryDst;
    
    
    BOOL CALLBACK QueryDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) 
    {
    	switch (uMsg)
    	{
    	case WM_INITDIALOG:
    		{
    			SetWindowText(hwndDlg, QueryCaption);
    			SetDlgItemText(hwndDlg, IDC_QUERY_TEXT, QueryText);
    			SetDlgItemText(hwndDlg, IDC_QUERY_EDIT, QueryDst);
    			return TRUE;
    		}
    	case WM_COMMAND:
    		{
    			switch (LOWORD(wParam))
    			{
    			case IDOK:
    				{
    					GetWindowText(GetDlgItem(hwndDlg, IDC_QUERY_EDIT), QueryDst, QueryLen);
    					EndDialog(hwndDlg, IDOK);
    					return TRUE;
    				}
    			case IDCANCEL:
    				{
    					EndDialog(hwndDlg, IDCANCEL);
    					return TRUE;
    				}
    			}
    		}
    	}
    	return FALSE;
    }
    
    bool InputBox(char *caption, char *text, char *dst, int len)
    {
    	QueryCaption = caption;
    	QueryText = text;
    	QueryDst = dst;
    	QueryLen = len;
    	return (DialogBox(0, MAKEINTRESOURCE(IDD_DLG_QUERY), 0, QueryDlgProc) == IDOK);
    }
    This is the code for the dialog:
    Code:
    IDD_DLG_QUERY DIALOGEX 0, 0, 186, 90
    STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | 
        WS_SYSMENU
    CAPTION "Dialog"
    FONT 8, "MS Shell Dlg", 400, 0, 0x1
    BEGIN
        DEFPUSHBUTTON   "OK",IDOK,129,7,50,14
        PUSHBUTTON      "Cancel",IDCANCEL,129,24,50,14
        LTEXT           "Destination:",IDC_QUERY_TEXT,14,22,111,18
        EDITTEXT        IDC_QUERY_EDIT,15,52,155,15,ES_AUTOHSCROLL
    END
    And of course your resource.h should have:
    Code:
    #define IDD_DLG_QUERY                   1
    #define IDC_QUERY_TEXT                  1001
    #define IDC_QUERY_EDIT                  1002
    You can use the function like this:
    Code:
    char caption[MAX_PATH];
    strcpy(caption, "caption text");
    
    if (InputBox("Set Caption", "Title:", caption, sizeof(caption)))
    {
    	MessageBox(0, caption, caption, 0);
    }
    Last edited by unselected; 12-13-2010 at 11:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  3. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  4. User input into a text file
    By Starr in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2006, 08:52 PM