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.
Printable View
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.
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
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
Here is the stand alone implementation that I use:
This is the code for the dialog: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);
}
And of course your resource.h should have: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
You can use the function like this:Code:#define IDD_DLG_QUERY 1
#define IDC_QUERY_TEXT 1001
#define IDC_QUERY_EDIT 1002
Code:char caption[MAX_PATH];
strcpy(caption, "caption text");
if (InputBox("Set Caption", "Title:", caption, sizeof(caption)))
{
MessageBox(0, caption, caption, 0);
}