Sign in to follow this  
phil05

win32: Need help on storing combo box item to variable

Recommended Posts

Well, I just got done reading this long tutorial on writing dialog boxes and combo boxes ontop of them. It teaches everything but storing that item in a variable. Aka a player chooses a playable race from a combo box, and I want that stored somehow. I assume the player should hit okay, and the button saves the value in a variable. Can you give me an example on how to do this?
// PREPROCESSOR DIRECTIVES
#include <windows.h>
#include <windowsx.h>
#include <string>
#include "resource.h"

// GLOBALS
#define WIN32_LEAN_AND_MEAN
const char g_szClassName[] = "myClassWindow";
char WinTitle[] = "Win32 API Programming";
#define WIDTH 800
#define HEIGHT 600
HWND dWnd = NULL;
HWND cbWnd = NULL;
HINSTANCE hInstance;

// PROTOTYPES
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK About(HWND dWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

// WINMAIN
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hWnd;
	MSG Msg;

	// Setting Background Color on Parent Window
	LOGBRUSH Brush;
	Brush.lbColor	= RGB(255, 255, 255);
	Brush.lbHatch	= 0;
	Brush.lbStyle	= BS_SOLID;
	HBRUSH hBrush	= CreateBrushIndirect(&Brush);

	// Registering Class Window
	wc.cbClsExtra		= 0;
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= hBrush;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hInstance		= hInstance;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= g_szClassName;
	wc.lpszMenuName		= NULL;
	wc.style			= 0;

	/* Error Check -> If Registering Class Window Fails, Alert User and Close Application. */
	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Registering Class Window Failed!", "Error...", MB_ICONERROR);
		return 0;
	}

	// Creating Window
	hWnd = CreateWindowEx(NULL, g_szClassName, WinTitle, WS_SYSMENU | WS_MINIMIZEBOX 
		| WS_CAPTION | WS_VISIBLE | WS_BORDER, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);

	/* Error Check -> If Creating Window Fails, Alert User and Close Application. */
	if (hWnd == NULL)
	{
		MessageBox(NULL, "Creating Window Failed!", "Error...", MB_ICONERROR);
		return 0;
	}

	// Show & Update Window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	// Message Loop
	while (GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}


// WINDOW PROCEDURE
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_CREATE:
			{
				// Centering Parent Window
				RECT rect;
				GetWindowRect(hWnd, &rect);
				int xSize = rect.right - rect.left;
				int ySize = rect.bottom - rect.top;
				int x = (GetSystemMetrics(SM_CXSCREEN) - xSize) / 2;
				int y = (GetSystemMetrics(SM_CYSCREEN) - ySize) / 2;
				SetWindowPos(hWnd, 0, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);


			}
			break;

		case WM_LBUTTONDOWN:
			{
				dWnd = CreateDialog(NULL, "IDD_ABOUT", hWnd, (DLGPROC)About);
				ShowWindow(dWnd, SW_SHOW);
				
				// Combo Box Window
				cbWnd = GetDlgItem(dWnd, IDD_SIZE_CBO);

				const char *LIST[] = { "Item1", "Item2", "Item3" };

				for (int i = 0; i < 3; i++)
				{
					SendMessage(cbWnd, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(LIST[i]));
				}
			}
			break;

		case WM_CLOSE:   DestroyWindow(hWnd);
					     break;
		case WM_DESTROY: PostQuitMessage(WM_QUIT);
						 break;
		default:		 return DefWindowProc(hWnd, Msg, wParam, lParam);
	}
	return 0;
}

BOOL CALLBACK About(HWND dWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDOK:
					// Does Nothing for Now

					EndDialog(dWnd, wParam);
					return TRUE;
				case IDCANCEL:
					// Does Nothing for Now
					EndDialog(dWnd, wParam);
					return TRUE;
			}
	}
	return FALSE;
}





Resource:

IDD_ABOUT DIALOG DISCARDABLE 0, 0, 400, 270
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
CAPTION "Win32 API Programming"
FONT 8, "MS Sans Serif"
BEGIN
	DEFPUSHBUTTON "&OK", IDOK, 335, 5, 60, 15
	PUSHBUTTON "&Cancel", IDCANCEL, 335, 25, 60, 15
	CTEXT "Welcome to the Dialog!", IDC_STATIC, 5, 5, 150, 10
	COMBOBOX        IDD_SIZE_CBO, 20, 20, 90, 60, WS_TABSTOP | CBS_DROPDOWNLIST | CBS_SORT


END





Share this post


Link to post
Share on other sites
Send the WM_GETTEXT message to get the text in the combo box and CB_GETCURSEL to get the index of the currently selected item in the combo box's list.

Skizz

Share this post


Link to post
Share on other sites
I'm getting an error...


char Buffer;
BOOL CALLBACK About(HWND dWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
// Does Nothing for Now
SendDlgItemMessage (dWnd, IDD_SIZE_CBO, WM_GETTEXT, 10, (LPARAM) Buffer);
MessageBox(NULL, Buffer, "Message", MB_OK);
EndDialog(dWnd, wParam);
return TRUE;
case IDCANCEL:
// Does Nothing for Now
EndDialog(dWnd, wParam);
return TRUE;
}

}




C:\Program Files\Microsoft Visual Studio\MyProjects\Win32 API Programming\Win32 API Programming.cpp(177) : error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Share this post


Link to post
Share on other sites

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

Sign in to follow this