0

Hi,
I've been trying for the past couple of hours to get this modal dialog box working, it is basically supposed to ask for text input and once entered will open a message box with that text. At this point though I'd be happy to just get the box up and go from there. I can compile this in visual c++ but it only brings up my main window and nothing else.
I can't see why this isnt working and as Im just learning this now I doubt I will see it myself. Sorry for the length. Im sure it is something stupid that I am doing or not doing, but it would be much appreciated if any is able to read it over and give me any tips!

Thanks in advance..

#define WIN32_LEAN_AND_MEAN

#include <windows.h> // Include all the windows headers
#include <windowsx.h>// Include useful macros.
#include "resource.h"

#define WINDOW_CLASS_NAME L"WINCLASS1"

HINSTANCE g_hAppInstance = 0;


LRESULT CALLBACK WindowProc(HWND _hwnd,
			 UINT _msg,
			 WPARAM _wparam,
			 LPARAM _lparam)

{	
	// This is the main message handler of the system.
	PAINTSTRUCT ps; // use in WM_PAINT.
	HDC hdc;        // Handle to a device context.

	//What is the message?
	switch(_msg)
	{
	case WM_CREATE:
		{
			//Do initialisation stuff here.

			//Return Success.
			return(0);
		}
		break;

	case WM_PAINT:
		{
			//Simply validate the window.
			hdc = BeginPaint(_hwnd, &ps);

			//You would do all your painting here...

			EndPaint(_hwnd, &ps);
			
			//Return Success.
			return(0);
		}
		break;

	case WM_DESTROY:
		{
			//Kill the application, this sends a WM_QUIT message.
			PostQuitMessage(0);

			//Return Success.
			return(0);
		}
		break;

	default:break;
	}//End switch.

	//Process any messages that we did not take care of...

	return(DefWindowProc(_hwnd, _msg, _wparam, _lparam));
}

BOOL CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    switch(message)
    {
	case WM_INITDIALOG:
	      {
		 return TRUE;
		 case WM_COMMAND:
		     switch (LOWORD(wparam))
		     {
		         case ID_OK:
			    {

			       HWND hEditBox = GetDlgItem(hwnd, IDC_MSGTEXT);
						      wchar_t cMsgText[256];
						      GetWindowText(hEditBox,
                                                             cMsgText, 256);

 				EndDialog(hwnd, 0);

				return true;

				MessageBox(NULL, L"Simple Program",
					  L"First Window",
					  MB_OK | MB_ICONINFORMATION);
					  EndDialog(hwnd, ID_OK);
					  break;
			   }
			   break;

			   default:
			   {
			      return FALSE;
			   }
			    break;
		     }
	      } 
     }
}



int WINAPI WinMain(HINSTANCE _hInstance,
				   HINSTANCE _hPrevInstance, 
				   LPSTR _lpCmdLine, 
				   int _nCmdShow)


{
	WNDCLASSEX winclass; // This will hold the class we create.
	HWND hwnd;			 // Generic window handle.
	MSG msg;			 // Generic message.
	
	//First fill in the window class structure.
	winclass.cbSize	= sizeof(WNDCLASSEX);
	winclass.style	= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc	= WindowProc;
	winclass.cbClsExtra		= 0;
	winclass.cbWndExtra		= 0;
	winclass.hInstance		= _hInstance;
	winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground  = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
	winclass.lpszMenuName   = NULL;
	winclass.lpszClassName  = WINDOW_CLASS_NAME;
	winclass.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);

	//register the window class
	if(!RegisterClassEx(&winclass))
	{
		return(0);
	}

	hwnd = CreateWindowExWINDOW_CLASS_NAME,     //Class
		             L"Your Basic Window",  //Title
			    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
						  0, 0,								//initial x,y.
						  400, 400,							//initial width, height.
						  NULL,								//Handle to parent
						  NULL,								//Handle to menu
						  _hInstance,						//Instance of this application
						  NULL);							//

	// Check the window was created successfully.
	if (!hwnd)
	{
		return(0);
	}

	//Enter main event loop.
	while (GetMessage(&msg, NULL, 0, 0))
	{
		//Translate any accelerator keys...
		TranslateMessage(&msg);

		//Send the message to window proc.
		DispatchMessage(&msg);
	}

	//Return to Windows like this...

	return(static_cast<int>(msg.wParam));
}
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by cool_zephyr
0

you need to make a call to the DialogBox() function with appropriate parameters
e.g.

DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_ABOUT),hwnd,AboutDlgProc);

IDD_ABOUT is the dialogbox template you want to show..you should include it in a resource file

#define IDD_ABOUT 1000

IDD_ABOUT DIALOG DISCARDABLE 0,0,250,80
STYLE DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU
CAPTION "About"
FONT 8,"MS Sans Serif"
BEGIN
	DEFPUSHBUTTON		"&OK",ID_OK,175,18,50,14
	PUSHBUTTON		"&Cancel",ID_CANCEL,174,35,50,14
	GROUPBOX		"About this program..",IDC_STATIC,7,7,225,50
	CTEXT			"An example program showing how to use dialog boxes",IDC_STATIC,16,18,144,33
END
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.