View Full Version: Guess The Number

C++ Learning Community > C++ Creations > Guess The Number


Title: Guess The Number


Xception - June 28, 2003 04:11 PM (GMT)
Compile as Win32 GUI project. It shows how to create a button, editbox and how to change the font of a control and how to get the text of a control.
CODE

/* Guess The Number */
/* made by */
/* Xception */

#include <windows.h>
#include <stdlib.h>

//  Declare Windows procedure
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

HWND hButton; // button handle
HWND hEdit; // edit control handle
int number; // random number
HFONT newfont; // font handle

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{
   HWND hwnd;                   // This is the handle for our window
   MSG messages;               // Here messages to the application are saved
   WNDCLASSEX wincl;          // Data structure for the windowclass
   // The Window structure
   wincl.hInstance = hInstance;
   wincl.lpszClassName = "MainWnd";
   wincl.lpfnWndProc = WindowProcedure;   // This function is called by windows
   wincl.style =0;          
   wincl.cbSize = sizeof (WNDCLASSEX);

   // Use default icon and mouse-pointer
   wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
   wincl.lpszMenuName = NULL;         // No menu
   wincl.cbClsExtra = 0;              // No extra bytes after the window class
   wincl.cbWndExtra = 0;              // structure or the window instance
   wincl.hbrBackground = (HBRUSH) COLOR_WINDOWFRAME; // background color

   // Register the window class, and if it fails quit the program
   if (!RegisterClassEx (&wincl))
       return 0;
   // Create the window
   hwnd = CreateWindowEx (
          0,                 // no extended window style
          "MainWnd",         // Window Classname
          "Guess The Number",           // Window Title Text
          WS_SYSMENU|WS_MINIMIZEBOX, // Window style: Systemmenu+Minimizebox
          (int)GetSystemMetrics(SM_CXFULLSCREEN)/2-120,       // center window horizontally
          (int)GetSystemMetrics(SM_CYFULLSCREEN)/2-60,      // and vertically
          240,                 // Window width
          120,                 // Window height
          NULL,        // Window has no parent window
          NULL,        // no menu
          hInstance,       // Window Instance handle
          NULL);                 // No Window Creation data
         
   ShowWindow (hwnd, nCmdShow); // Show the window
   // Run the message loop. It will run until GetMessage() returns 0
   while (GetMessage (&messages, NULL, 0, 0))
   {
       // Translate virtual-key messages into character messages
       TranslateMessage(&messages);
       // Send message to WindowProcedure
       DispatchMessage(&messages);
   }

   // The program return-value is 0 - The value that PostQuitMessage() gave
   return messages.wParam;
}

//  This function is called by the Windows function DispatchMessage()
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
char e_number[5];  
switch (message) // handle the messages
{
 HDC hdc;        // handle to a device context
case WM_CREATE:
   srand(GetTickCount());
   number=rand()%1000;
   newfont=CreateFont(22,0,0,0,400,0,0,0,0,0,0,0,0,"Dauphin");
   hButton = CreateWindow("BUTTON","Guess",WS_VISIBLE|WS_CHILD,120, 50,60, 20, hwnd, (HMENU)1,
   ((LPCREATESTRUCT)lParam)->hInstance, NULL);
   hEdit = CreateWindow("EDIT",NULL,WS_VISIBLE|WS_CHILD|ES_UPPERCASE|WS_BORDER,40, 50,60, 20, hwnd, NULL,
   ((LPCREATESTRUCT)lParam)->hInstance, NULL);
   SendDlgItemMessage(hwnd,1,WM_SETFONT,(WPARAM)newfont,true);      
break;
case WM_COMMAND:
       if ((HWND)lParam==hButton)
       {
       GetWindowText(hEdit,e_number,5);
       if (atoi(e_number)==number) {
       MessageBox(hwnd,"You are great!","Winner!",MB_OK);
       number=rand()%1000;}
       else
       if (atoi(e_number)>number) MessageBox(hwnd,"Your number is too high!","Oh no!",MB_OK);
       else
       MessageBox(hwnd,"Your number is too low!","Oh no!",MB_OK);
       }
break;
case WM_DESTROY:
           DeleteObject(newfont); // cleanup    
           PostQuitMessage (0);   // send a WM_QUIT to the message queue
break;
case WM_PAINT:
           PAINTSTRUCT ps;
           hdc=BeginPaint(hwnd,&ps);
           TextOut(hdc,10,10,"Guess the number from 0 to 1000",31);
           EndPaint(hwnd,&ps);
break;
default:    
           return DefWindowProc (hwnd, message, wParam, lParam);
   }
   return 0;
}

Dragon - June 29, 2003 03:58 AM (GMT)
Thanks a lot Xception (for my question in the win32 programming forum).




* Hosted for free by InvisionFree