#include <Windows.h>
#include "PrimaFereastra.h"

HINSTANCE hInst;
HWND pDlgNemod=NULL;

int WINAPI WinMain( HINSTANCE hInstance, 
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow
	)
{
	HWND hwndMain; 
	MSG msg;
 
	WNDCLASS wndCls;

	UNREFERENCED_PARAMETER(lpCmdLine);  
 
	wndCls.style = 0;
	wndCls.lpfnWndProc = (WNDPROC) WndProc;
	wndCls.cbClsExtra = 0;
	wndCls.cbWndExtra = 0; 
	wndCls.hInstance = hInstance;

	wndCls.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);

	wndCls.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);

	wndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

	wndCls.lpszMenuName =  NULL; 

	wndCls.lpszClassName = "MainWndClass"; 
 
	if (!RegisterClass(&wndCls)) 
   		return FALSE;     
	
	hwndMain = CreateWindow("MainWndClass", "PrimaFereastra", 
		WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, 
		CW_USEDEFAULT, CW_USEDEFAULT, (HWND) NULL, 
		(HMENU) NULL, hInstance, (LPVOID) NULL);

	hInst=hInstance;

	if (!hwndMain)         
		return FALSE;
	else
	{
		HMENU hnewMenu=LoadMenu(hInstance,"MENIU");
		SetMenu(hwndMain,hnewMenu);
	}


	ShowWindow(hwndMain, nCmdShow);
	UpdateWindow(hwndMain);  

/*   HWND hText = CreateWindow( "STATIC", "Eticheta Statica",
      WS_CHILD | WS_VISIBLE | SS_LEFT,20, 20, 100, 15,
      hwndMain,NULL, hInstance, NULL );
   HWND hEdit = CreateWindow( "EDIT", "",
      WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
      20, 40, 100, 20, hwndMain,NULL,hInstance, NULL );
   HWND hBtn = CreateWindow( "BUTTON", "Buton de Comanda", 
      WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
      20, 80, 150, 30, hwndMain,NULL, hInstance, NULL );
   HWND hListb = CreateWindow( "LISTBOX", "", 
      WS_CHILD | WS_VISIBLE | WS_DLGFRAME ,
      20, 120, 150, 50, hwndMain,NULL, hInstance, NULL );
   HWND hCombo = CreateWindow( "COMBOBOX", "", 
      WS_CHILD | WS_VISIBLE ,
      20, 180, 150, 30, hwndMain,NULL, hInstance, NULL );*/


	while (GetMessage(&msg, (HWND) NULL, 0, 0)) 
	{         

		TranslateMessage(&msg);
		DispatchMessage(&msg);     
	}  
	return msg.wParam; 
}


LRESULT CALLBACK WndProc( HWND hwnd, 
						  UINT uMsg,
						  WPARAM wParam, 
						  LPARAM lParam 
						)
{
	static HWND hText=NULL;
	static HWND hEdit=NULL;
	static HWND hBtn=NULL;
	static HWND hListb=NULL;
	static HWND hCombo=NULL;
  
	switch(uMsg){
     
	case WM_CLOSE:
		PostMessage(hwnd, WM_QUIT, 0L, 0L);
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
	case WM_KEYDOWN:
		LPCTSTR text;
		switch(wParam){
		case VK_BACK: text="Ati apasat tasta BackSpace";
					  break;
		case VK_TAB:text="Ati apasat tasta Tab";
					  break;
		case VK_LEFT:text="Ati apasat tasta SageataStinga";
					  break;
		case VK_RIGHT:text="Ati apasat tasta SageataDreapta";
					  break;
		case VK_UP:text="Ati apasat tasta SageataSus";
					  break;
		case VK_DOWN:text="Ati apasat tasta SageataJos";
					  break;
		case VK_RETURN:text="Ati apasat tasta Enter";
					  break;
		case VK_ESCAPE:text="Ati apasat tasta Escape";
					  break;
		case VK_CONTROL:text="Ati apasat tasta Control";
					  break;
		default: text="Ati apasat alta tasta";
				 break;
		}
		MessageBox(hwnd, text, "Mesaj",MB_OK|MB_ICONEXCLAMATION);
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
		break;
	case WM_COMMAND :
        switch( LOWORD( wParam ) )
        {
		case IDM_ARATA:
			hText = CreateWindow( "STATIC", "Eticheta Statica",
										WS_CHILD | WS_VISIBLE | SS_LEFT,20, 20, 100, 15,
										hwnd,NULL, hInst, NULL );
			hEdit = CreateWindow( "EDIT", "",WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
								   20, 40, 100, 20, hwnd,NULL,hInst, NULL );

			hBtn = CreateWindow( "BUTTON", "Buton de Comanda", 
									   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
									   20, 80, 150, 30, hwnd,NULL, hInst, NULL );

			hListb = CreateWindow( "LISTBOX", "", 
									   WS_CHILD | WS_VISIBLE | WS_DLGFRAME ,
									   20, 120, 150, 50, hwnd,NULL, hInst, NULL );

			hCombo = CreateWindow( "COMBOBOX", "", 
										WS_CHILD | WS_VISIBLE ,
										20, 180, 150, 30, hwnd,NULL, hInst, NULL );
			break;
		case IDM_ASCUNDE:
			DestroyWindow(hText);
			DestroyWindow(hEdit);
			DestroyWindow(hBtn);
			DestroyWindow(hListb);
			DestroyWindow(hCombo);
			break;
		case IDM_IESE:
			 DestroyWindow(hwnd);
			 return DefWindowProc(hwnd, uMsg, wParam, lParam);
		case IDM_START :
             DialogBox( hInst, "CASETAMODALA", hwnd, (DLGPROC)Modal );
             break;
		case IDM_DNEMOD:
		  if (!pDlgNemod)
			pDlgNemod=CreateDialog(hInst, "CASETAMODALA", hwnd, (DLGPROC)Nemodal);
			break;
		case IDM_INEMOD:
			if (pDlgNemod)
			{
			   DestroyWindow(pDlgNemod);
			   pDlgNemod=NULL;
			}
			break;
        }
	default:
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}
}

LRESULT CALLBACK Modal( HWND hDlg,           
                        UINT message,        
                        WPARAM wParam,       
                        LPARAM lParam)
{
   switch (message) 
   {
       case WM_INITDIALOG: 
               return (TRUE);

       case WM_COMMAND:                              
               if (   LOWORD(wParam) == IDOK         
                   || LOWORD(wParam) == IDCANCEL)    
               {
                       EndDialog(hDlg, TRUE);        
                       return (TRUE);
               }
               break;
   }

   return (FALSE); 
}

LRESULT CALLBACK Nemodal( HWND hDlg,           
                        UINT message,        
                        WPARAM wParam,       
                        LPARAM lParam)
{
   switch (message) 
   {
       case WM_INITDIALOG: 
               return (TRUE);

       case WM_COMMAND:                              
               if ( LOWORD(wParam) == IDOK         
                   || LOWORD(wParam) == IDCANCEL)    
			   {
					   DestroyWindow(hDlg);
				       pDlgNemod=NULL;
                       return (TRUE);
               }
               break;
   }

   return (FALSE); 
}