#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


//BACKGROUND BITMAP "background.bmp"

#define IDC_MAIN_BUTTON1 101        
#define IDC_MAIN_BUTTON2 102
#define IDC_MAIN_BUTTON3 103        
#define IDC_MAIN_BUTTON4 104
#define IDC_MAIN_BUTTON5 105        
#define IDC_MAIN_BUTTON6 106
#define IDC_MAIN_BUTTON7 107        
#define IDC_MAIN_BUTTON8 108
#define IDC_MAIN_BUTTON9 109

char version[8];       // Software version
signed int randomnumber = 0;
signed int randomnumber2 = 0;
signed int randomnumber3 = 0;
signed int randomnumber4 = 0;
 
char a_num[30]="aaa";
char a_num2[4]="aaa";
char a_num3[4]="aaa";
char a_num4[4]="aaa";


// Add new popup menu
#define ADDPOPUPMENU(hmenu, string) \
  HMENU hSubMenu = CreatePopupMenu(); \
  AppendMenu(hmenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, string);

//////////////////////////////////////////////////////
//Set Window Handler, Message paramaters
/////////////////////////////////////////////////////
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);


///////////////////////////////////////////////////////////////////////////////////
// WinMain
// create windows and widget's parent
///////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{

 strcpy(version, "1.2.0");

 //Define the Window's classes
 WNDCLASSEX wClass;
 ZeroMemory(&wClass,sizeof(WNDCLASSEX));
 wClass.cbClsExtra=0;                             // some compilers need NULL 
 wClass.cbSize=sizeof(WNDCLASSEX);
 wClass.cbWndExtra=0;                             // some compilers need NULL


  wClass.hCursor=LoadCursor(NULL,IDC_ARROW);       // use default cursor arrow
  wClass.hIcon=NULL;                               // icon not set
  wClass.hIconSm=NULL;                             // small icon not set
  wClass.hInstance=hInst;
  wClass.lpfnWndProc=(WNDPROC)WinProc;
  wClass.lpszClassName="Window Class";             // set name for window class
  wClass.lpszMenuName=NULL;
  wClass.style=CS_HREDRAW|CS_VREDRAW;

  // if there is an error setting up classes
  if(!RegisterClassEx(&wClass)){              
    int nResult=GetLastError();
    MessageBox(NULL,"Window class creation failed - error 0001 \r\n","Window Class Failed",MB_ICONERROR);
  }

  //Create Main window 
  //par 1 replaced NULL with 0 for my compiler


  HWND hWnd=CreateWindowEx(0,"Window Class",     // Window class to use defined above
                           "DDD Dice Roller",    // Title
			   WS_OVERLAPPEDWINDOW,  // Window Type
			   200,                  // X placement
			   200,                  // Y placement
			   710,                  // X width
			   420,                  // Y height
			   NULL,
			   NULL,
			   hInst,                // assign to main (WinMain)
			   NULL);
  
  // if error creating window 
  if(!hWnd){
    int nResult=GetLastError();
    MessageBox(NULL,"Window creation failed - error 0002\r\n","Window Creation Failed",MB_ICONERROR);
  }


  // Create the main menu
  //CreateMainMenu(hWnd);

  // display window
  ShowWindow(hWnd,nShowCmd);

  // define message handler to listen to widgets
  MSG msg;
  ZeroMemory(&msg,sizeof(MSG));

  //listen to widgets
  while(GetMessage(&msg,NULL,0,0)){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return 0;
}


////////////////////////////////////////////////////////////////////////
// Windows processing
// WinProc
////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){
  switch(msg){


case WM_PAINT:
{
    PAINTSTRUCT     ps;
    HDC             hdc;
    BITMAP          bitmap;
    HDC             hdcMem;
    HGDIOBJ         oldBitmap;
    HWND hBitmap01 = (HBITMAP)LoadImage(NULL, "c:\\Temp\\DDD.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    hdc = BeginPaint(hWnd, &ps);

    hdcMem = CreateCompatibleDC(hdc);
    oldBitmap = SelectObject(hdcMem, hBitmap01);

    GetObject(hBitmap01, sizeof(bitmap), &bitmap);
    BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, oldBitmap);
    DeleteDC(hdcMem);

    EndPaint(hWnd,&ps);
    }



//////////////////////////////


    case WM_CREATE:{
      // Set application to use system default font
      HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);


     //Column 1 x=65  Column 2 x= 295
     //Row 1 y=15  Row 2 y=45  Row 3 y=75  Row 4 y=105

     // Create a 'roll d4' push button
      HWND hWndButton=CreateWindowEx(0,
                                     "BUTTON",                 // General button
                                     "roll dice - one d4",     // Text of button
                                     WS_TABSTOP|WS_VISIBLE|
                                     WS_CHILD|BS_DEFPUSHBUTTON,
                                     10,                       // x cord
                                     10,                       // y cord
                                     110,                      // width
                                     24,                       // height 
                                     hWnd,                     // connect to main window
                                     (HMENU)IDC_MAIN_BUTTON1,
                                     GetModuleHandle(NULL),
                                     NULL);


                                     //NULL,
                                     //GetModuleHandle(NULL),
                                     //NULL);
      SendMessage(hWndButton,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));

      // Create a 'roll d6' push button
      HWND hWndButton2=CreateWindowEx(0,
                                      "BUTTON",                // General button
                                      "roll dice - one d6",    // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                      // x cord
                                      40,                      // y cord
                                      110,                     // width
                                      24,                      // height 
                                      hWnd,                    // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON2,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton2,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));


      // Create a 'roll d8' push button
      HWND hWndButton3=CreateWindowEx(0,
                                      "BUTTON",                // General button
                                      "roll dice - one d8",    // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                      // x cord
                                      70,                      // y cord
                                      110,                     // width
                                      24,                      // height 
                                      hWnd,                    // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON3,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton3,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));


      // Create a 'roll d10' push button
      HWND hWndButton4=CreateWindowEx(0,
                                      "BUTTON",                // General button
                                      "roll dice - one d10",    // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                      // x cord
                                      100,                     // y cord
                                      110,                     // width
                                      24,                      // height 
                                      hWnd,                    // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON4,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton4,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));




      // Create a 'roll d12' push button
      HWND hWndButton5=CreateWindowEx(0,
                                      "BUTTON",                 // General button
                                      "roll dice - one d12",    // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                       // x cord
                                      130,                      // y cord
                                      110,                      // width
                                      24,                       // height 
                                      hWnd,                     // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON5,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton5,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));


      // Create a 'roll d20' push button
      HWND hWndButton6=CreateWindowEx(0,
                                      "BUTTON",                 // General button
                                      "roll dice - one d20",    // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                       // x cord
                                      160,                      // y cord
                                      110,                      // width
                                      24,                       // height 
                                      hWnd,                     // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON6,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton6,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));


      // Create a 'roll four d6' push button
      HWND hWndButton7=CreateWindowEx(0,
                                      "BUTTON",                 // General button
                                      "roll dice - four d6",    // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                       // x cord
                                      190,                      // y cord
                                      110,                      // width
                                      24,                       // height 
                                      hWnd,                     // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON7,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton7,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));

      // Create a 'roll two d20' push button
      HWND hWndButton8=CreateWindowEx(0,
                                      "BUTTON",                 // General button
                                      "roll dice - two d20",    // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                       // x cord
                                      220,                      // y cord
                                      110,                      // width
                                      24,                       // height 
                                      hWnd,                     // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON8,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton8,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));


      // Create a 'roll d4 and d6' push button
      HWND hWndButton9=CreateWindowEx(0,
                                      "BUTTON",                 // General button
                                      "roll dice - d4 and d6",  // Text of button
                                      WS_TABSTOP|WS_VISIBLE|
                                      WS_CHILD|BS_DEFPUSHBUTTON,
                                      10,                       // x cord
                                      250,                      // y cord
                                      110,                      // width
                                      24,                       // height 
                                      hWnd,                     // connect to main window
                                      (HMENU)IDC_MAIN_BUTTON9,
                                      GetModuleHandle(NULL),
                                      NULL);
      SendMessage(hWndButton9,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(FALSE,0));




      } // end create
      break;



////////////////////////////////////////



    case WM_COMMAND:
      switch(LOWORD(wParam)){

       case IDC_MAIN_BUTTON1:{

         srand(time(NULL));
         randomnumber =  (rand() % 4) +1;
         itoa(randomnumber,a_num,10);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled a single d4!",MB_ICONINFORMATION);
       }
       break;
       
       case IDC_MAIN_BUTTON2:{

         srand(time(NULL));
         randomnumber = ( rand() % 6) +1;
         itoa(randomnumber,a_num,10);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled a single d6!",MB_ICONINFORMATION);
       }
       break;

       case IDC_MAIN_BUTTON3:{

         srand(time(NULL));
         randomnumber =  (rand() % 8) +1;
         itoa(randomnumber,a_num,10);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled a single d8!",MB_ICONINFORMATION);
       }
       break;
       
       case IDC_MAIN_BUTTON4:{

         srand(time(NULL));
         randomnumber = ( rand() % 10) +1;
         itoa(randomnumber,a_num,10);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled a single d10!",MB_ICONINFORMATION);
       }
       break;

       case IDC_MAIN_BUTTON5:{

         srand(time(NULL));
         randomnumber =  (rand() % 12) +1;
         itoa(randomnumber,a_num,10);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled a single d12!",MB_ICONINFORMATION);
       }
       break;
       
       case IDC_MAIN_BUTTON6:{

         srand(time(NULL));
         randomnumber = ( rand() % 20) +1;
         itoa(randomnumber,a_num,10);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled a single d20!",MB_ICONINFORMATION);
       }
       break;

       case IDC_MAIN_BUTTON7:{
         srand(time(NULL));
         randomnumber = ( rand() % 6) +1;
         itoa(randomnumber,a_num,10);
         randomnumber2 = ( rand() % 6) +1;
         itoa(randomnumber2,a_num2,10);
         randomnumber3 = ( rand() % 6) +1;
         itoa(randomnumber3,a_num3,10);
         randomnumber4 = ( rand() % 6) +1;
         itoa(randomnumber4,a_num4,10);
         strcat(a_num,", ");
         strcat(a_num,a_num2);
         strcat(a_num,", ");
         strcat(a_num,a_num3);
         strcat(a_num,", and ");
         strcat(a_num,a_num4);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled four d6!",MB_ICONINFORMATION);
       }
       break;

     case IDC_MAIN_BUTTON8:{
         srand(time(NULL));
         randomnumber = ( rand() % 20) +1;
         itoa(randomnumber,a_num,10);
         randomnumber2 = ( rand() % 20) +1;
         itoa(randomnumber2,a_num2,10);
         strcat(a_num," and ");
         strcat(a_num,a_num2);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled two d20!",MB_ICONINFORMATION);
       }
       break;

   case IDC_MAIN_BUTTON9:{
         srand(time(NULL));
         randomnumber = ( rand() % 4) +1;
         itoa(randomnumber,a_num,10);
         randomnumber2 = ( rand() % 6) +1;
         itoa(randomnumber2,a_num2,10);
         strcat(a_num," and ");
         strcat(a_num,a_num2);
         strcat(a_num," was rolled");
         MessageBox(NULL,a_num,"You rolled d4 and d6!",MB_ICONINFORMATION);
       }
       break;


  
    } // end WM_COMMAND
    break;

/////////////////////



      case WM_DESTROY:{
        PostQuitMessage(0);
        return 0;
      }// end destroy

      break;

    }

  // end switch

return DefWindowProc(hWnd,msg,wParam,lParam);
}