Code:
#include <windows.h>
#include <iostream>
#include <fstream>
//control identifiers
enum
{
IDC_LABEL=400,
IDC_PERSON,
IDC_TICKET,
IDC_SUBMIT,
IDC_ANSWER
};
const int MAX_EDIT_TEXT=256;
const int MAX_DIGITS=5;
BOOL CALLBACK ChangeFonts(HWND,LPARAM);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void OnCommand(const HWND,const HWND,const int,const int);
int OnCreate(const HWND,CREATESTRUCT*);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
WNDCLASSEX wincl={0};
char chClassName[] = "RaffleWindow";
wincl.hInstance=hInstance;
wincl.lpszClassName=chClassName;
wincl.cbSize=sizeof(WNDCLASSEX);
wincl.lpfnWndProc=(WNDPROC)WndProc;
wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
wincl.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
if(!RegisterClassEx(&wincl))
{
MessageBox(0,"Window Registration Failed!","Error!",MB_ICONSTOP|MB_OK);
return 0;
}
HWND hwnd=CreateWindowEx(0,chClassName,"Dark_Ride Raffle",
WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX, //no point in resizing it
CW_USEDEFAULT,CW_USEDEFAULT,380,205,HWND_DESKTOP,NULL,
hInstance,NULL);
if(hwnd==NULL)
{
MessageBox(0,"Window Creation Failed!","Error",MB_ICONSTOP|MB_OK);
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
MSG Msg;
while(GetMessage(&Msg,0,0,0)>0)
{
if(!IsDialogMessage(hwnd,&Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return static_cast<int>(Msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
return OnCreate(hwnd,reinterpret_cast<CREATESTRUCT*>(lParam));
case WM_COMMAND:
OnCommand(hwnd,reinterpret_cast<HWND>(lParam),LOWORD(wParam),
HIWORD(wParam));
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
void OnCommand(const HWND hwnd,const HWND hCntrl,const int id,const int msg)
{
if (id==IDCANCEL)
{
//escape key pressed, just quit
DestroyWindow(hwnd);
}
if (hCntrl)
{
if (msg==BN_CLICKED && id==IDC_SUBMIT)
{
char person[MAX_EDIT_TEXT];
int len=GetWindowText(GetDlgItem(hwnd,IDC_PERSON),person,MAX_EDIT_TEXT);
if (!len)
{
MessageBox(0,"Please enter a name","Error",MB_OK|MB_ICONEXCLAMATION);
return;
}
int count=GetDlgItemInt(hwnd,IDC_TICKET,0,0);
if (!count)
{
MessageBox(0,"Please enter a number","Error",MB_OK|MB_ICONEXCLAMATION);
return;
}
char buffer[8];
wsprintf(buffer,"%d",count);
std::ofstream file("people.ini",std::ios::app);
file<<person<<'\t'<<count<<std::endl;
}
}
}
int OnCreate(const HWND hwnd,CREATESTRUCT *cs)
{
//create some labels
CreateWindowEx(0,"static",
"Well here it is, thanks for putting me through hell!",
WS_CHILD|WS_VISIBLE|SS_SIMPLE,
25,25,340,25,
hwnd,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
cs->hInstance,0);
CreateWindowEx(0,"static",
"Add person and number of tickets",
WS_CHILD|WS_VISIBLE|SS_SIMPLE,
25,50,340,25,
hwnd,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
cs->hInstance,0);
CreateWindowEx(0,"static",
"Person:",
WS_CHILD|WS_VISIBLE|SS_SIMPLE,
25,75,340,25,
hwnd,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
cs->hInstance,0);
CreateWindowEx(0,"static",
"Tickets:",
WS_CHILD|WS_VISIBLE|SS_SIMPLE,
25,100,340,25,
hwnd,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
cs->hInstance,0);
HWND hPerson=CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 0,
WS_CHILD|WS_VISIBLE|WS_TABSTOP,
75, 75, 200, 25,
hwnd,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_PERSON)),
cs->hInstance, 0);
SendMessage(hPerson,EM_SETLIMITTEXT,MAX_EDIT_TEXT,0);
HWND hTicket=CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 0,
WS_CHILD|WS_VISIBLE|ES_NUMBER|WS_TABSTOP,
75, 100, 75, 25,
hwnd,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_TICKET)),
cs->hInstance, 0);
SendMessage(hTicket,EM_SETLIMITTEXT,MAX_DIGITS,0);
CreateWindowEx(0,"Button","Submit",
WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_TABSTOP,
275, 75, 75, 25,
hwnd, reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_SUBMIT)),
cs->hInstance, 0);
CreateWindowEx (0, "Button", "And the lucky winner is",
WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_TABSTOP,
150, 100, 200, 25,
hwnd, reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_ANSWER)),
cs->hInstance, 0);
EnumChildWindows(hwnd,ChangeFonts,0); //change the font to typical control font
SetFocus(hPerson);
return 0;
}
BOOL CALLBACK ChangeFonts(HWND hwnd,LPARAM lParam)
{
//EnumChildProc
SendMessage(hwnd,WM_SETFONT,
reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)),
MAKELPARAM(1,0));
return TRUE;
}