Code:
#include <windows.h>
#include <ctime>
#include <iostream>
#include <fstream>
using namespace std;
#define BOX0 500
#define BUTTON1 501
#define GBOX3 502
#define EDIT5 503
#define BUTTON7 505
#define EDIT10 506
#define EDIT11 507
const char g_szClassName[] = "WindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Open Database"),
WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,
15,
25,
200,
50,
hwnd,
(HMENU)BOX0,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Start"),
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
255,
25,
100,
40,
hwnd,
(HMENU)BUTTON1,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("CordGen 2.2"),
WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
5,
0,
480,
100,
hwnd,
(HMENU)GBOX3,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Quit"),
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
375,
25,
100,
40,
hwnd,
(HMENU)BUTTON7,
NULL,
NULL);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case BUTTON1:
{
srand ((unsigned) time(0));
for(int gen = 0; gen < 100; gen++)
{
int cordx = rand()%1600;
int cordy = rand()%1600;
int cordz = rand()%3000;
int cordxr = rand()%2;
int cordyr = rand()%2;
int cordzr = rand()%2;
if(cordxr == 1)
{
cordx = cordx - 1600;
}
if(cordyr == 1)
{
cordy = cordy - 1600;
}
if(cordzr == 1)
{
cordz = cordz - 3000;
}
SetDlgItemInt(hwnd,EDIT5,cordx,0); // send cords to edit boxes
SetDlgItemInt(hwnd,EDIT10,cordy,0);
SetDlgItemInt(hwnd,EDIT11,cordz,0);
ofstream open_cdata("Cords.txt", ios::app); // send cords to text file
open_cdata <<cordx<<" "<<cordy<<" "<<cordz<<endl;
open_cdata.close();
if(BOX0 == BST_CHECKED)
{
system("Cords.txt");
}
}
}
break;
case BUTTON7:
SendMessage(hwnd,WM_CLOSE,0,0);
return 0;
break;
}
return 0;
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"CordGen",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500,150,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}