• entries
    707
  • comments
    1173
  • views
    432943

Untitled

Sign in to follow this  
Programmer16

94 views

I wrote this up for someone on the forums and figured maybe someone could use it. It doesn't use templates or anything, so evidentially it's "not good enough and a complete waste of my time", but wtfever. I don't see the point in wasting my time to learn dialog resources in C++ when I don't plan on using them. Ever. Anyway, here it is:

#include
#include

#ifdef UNICODE
typedef std::wstring InputBoxStringType;
typedef LPWSTR InputBoxCharPointerType;
typedef wchar_t InputBoxCharType;
#define InputBoxString(x) L##x
#else
typedef std::string InputBoxStringType;
typedef LPSTR InputBoxCharPointerType;
typedef char InputBoxCharType;
#define InputBoxString(x) x
#endif

LRESULT CALLBACK InputBoxWndProc(HWND WndHandle, UINT Message, WPARAM wParam, LPARAM lParam)
{
static InputBoxStringType *Results = 0;
static HWND InputLabelHandle = 0;
static HWND InputHandle = 0;

switch(Message)
{
case WM_NCCREATE:
{
Results = (InputBoxStringType*)((CREATESTRUCT*)lParam)->lpCreateParams;
break;
}

case WM_DESTROY:
case WM_CLOSE:
{
PostQuitMessage(0);
break;
}

case WM_COMMAND:
{
int NotifyCode = HIWORD(wParam);
int ControlId = LOWORD(wParam);
HWND ControlHandle = (HWND)lParam;

switch(ControlId)
{
case 1001:
{
InputHandle = ControlHandle;
break;
}

case 1002:
{
if(InputHandle == 0 || !Results)
{
PostQuitMessage(0);
break;
}

InputBoxCharPointerType Buffer = new InputBoxCharType[256];
GetWindowText(InputHandle, Buffer, 256);
*Results = Buffer;
PostQuitMessage(0);
return 0;
}

case 1003:
{
if(InputHandle == 0 || !Results)
{
PostQuitMessage(0);
return 0;
}

*Results = InputBoxString("");
PostQuitMessage(0);
return 0;
}
}
break;
}
}
return DefWindowProc(WndHandle, Message, wParam, lParam);
}

static InputBoxStringType ShowInputBox(HWND OwnerWindowHandle, const InputBoxStringType &InputLabel, const InputBoxStringType &InputText, const InputBoxStringType &Title)
{
WNDCLASS WndClass;
HWND DialogHandle = 0;
HWND InputLabelHandle = 0;
HWND InputHandle = 0;
HWND OkButtonHandle = 0, CancelButtonHandle = 0;
InputBoxStringType Result = InputBoxString("");

ZeroMemory(&WndClass, sizeof(WNDCLASS));
WndClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
WndClass.hIcon = LoadIcon(0, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(0, IDC_ARROW);
WndClass.hInstance = GetModuleHandle(0);
WndClass.lpszClassName = InputBoxString("InputBox");
WndClass.lpfnWndProc = InputBoxWndProc;
RegisterClass(&WndClass);

int X = 0, Y = 0;
int Width = 0, Height = 0;
RECT TempRect;
GetWindowRect(GetDesktopWindow(), &TempRect);
Width = (int)((float)(TempRect.right - TempRect.left) * 0.25f);
Height = 150;
X = ((TempRect.right - TempRect.left) / 2) - (Width / 2);
Y = ((TempRect.bottom - TempRect.top) / 2) - (Height / 2);
DialogHandle = CreateWindowEx(0, InputBoxString("InputBox"), Title.c_str(), WS_DLGFRAME | WS_VISIBLE, X, Y, Width, Height, OwnerWindowHandle, 0, GetModuleHandle(0), (LPVOID)&Result);

int LabelX = 5, LabelY = 5;
int LabelWidth = Width - 10, LabelHeight = 24;
InputLabelHandle = CreateWindowEx(0, InputBoxString("STATIC"), InputLabel.c_str(), WS_CHILD | WS_VISIBLE, LabelX, LabelY, LabelWidth, LabelHeight, DialogHandle, 0, GetModuleHandle(0), 0);

int TBoxX = 5, TBoxY = 29;
int TBoxWidth = Width - 20, TBoxHeight = 24;
InputHandle = CreateWindowEx(0, InputBoxString("EDIT"), InputText.c_str(), WS_BORDER | WS_CHILD | WS_VISIBLE, TBoxX, TBoxY, TBoxWidth, TBoxHeight, DialogHandle, (HMENU)1001, GetModuleHandle(0), 0);

int OkBtnX = (Width / 2) - 75, OkBtnY = Height - 60;
int OkBtnWidth = 75, OkBtnHeight = 24;
OkButtonHandle = CreateWindowEx(0, InputBoxString("BUTTON"), InputBoxString("Ok"), WS_CHILD | WS_VISIBLE, OkBtnX, OkBtnY, OkBtnWidth, OkBtnHeight, DialogHandle, (HMENU)1002, GetModuleHandle(0), 0);

int CancelBtnX = (Width / 2) + 25, CancelBtnY = Height - 60;
int CancelBtnWidth = 75, CancelBtnHeight = 24;
CancelButtonHandle = CreateWindowEx(0, InputBoxString("BUTTON"), InputBoxString("Cancel"), WS_CHILD | WS_VISIBLE, CancelBtnX, CancelBtnY, CancelBtnWidth, CancelBtnHeight, DialogHandle, (HMENU)1003, GetModuleHandle(0), 0);

MSG Message;
ZeroMemory(&Message, sizeof(MSG));
while(Message.message != WM_QUIT)
{
if(PeekMessage(&Message, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
return Result;
}

#undef InputBoxString

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
std::string Result = ShowInputBox(0, "Please enter your name:", "Unknown", "Name Entry");
MessageBox(0, Result.c_str(), "Your name is:", MB_OK);
return 0;
}



Notes:
* I'm not the best Win32 programmer. I used to be better, but I have no reason to be seeing as how WinForms is 10x better and easier. All I need to know is how to create a window for use with DirectX.

* I realize that PostQuitMessage(0) is "supposed" to be used for exiting the app completely. Ask me if I care.

Back to programming for me.
Sign in to follow this  


2 Comments


Recommended Comments

Do you care? lol

but yea, i gave up on C++ just because of how hard it is to get a window up and shit, and i can never find anyone to actually explain to me wth is going on :D..

but yea i just felt like asking if you care..:D

Share this comment


Link to comment
Quote:
Original post by Swattkidd
Do you care? lol

but yea, i gave up on C++ just because of how hard it is to get a window up and shit, and i can never find anyone to actually explain to me wth is going on :D..

but yea i just felt like asking if you care..:D


Lol, I expected someone to ask.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now