summary refs log tree commit diff stats
path: root/Source/Main.cpp
blob: 8a7a5bb5be243913dda620a4d32ddfc07ad8d377 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Source.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "tchar.h"
#include "Randomizer.h"
#include <Richedit.h>
#include <iostream>
#include <ctime>
#include <string>

#define MAX_LOADSTRING 100
#define IDC_RANDOMIZE 0x464

HINSTANCE hInst;
WCHAR szWindowClass[MAX_LOADSTRING];
HWND hwndSeed;

// Forward declares
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
	LoadStringW(hInstance, IDC_SOURCE, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);
    if (!InitInstance(hInstance, nCmdShow)) {
        return FALSE;
    }

    MSG msg;
    while (!GetMessage(&msg, nullptr, 0, 0) == 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex = {
		sizeof(WNDCLASSEX),
		CS_HREDRAW | CS_VREDRAW,
		WndProc,
		0,
	    0,
		hInstance,
		LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOURCE)),
		LoadCursor(nullptr, IDC_ARROW),
		(HBRUSH)(COLOR_WINDOW+1),
		MAKEINTRESOURCEW(IDC_SOURCE),
		szWindowClass,
		LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)),
	};

    return RegisterClassExW(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hInst = hInstance; // Store instance handle in our global variable

	WCHAR szTitle[MAX_LOADSTRING];
	LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

	if (!hWnd)
	{
		return FALSE;
	}

	LoadLibrary(L"Msftedit.dll");
	HWND label = CreateWindow(L"BUTTON", L"Enter a seed:",
		WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
		10, 10, 100, 26, hWnd, NULL, hInst, NULL);

	hwndSeed = CreateWindowEx(0, MSFTEDIT_CLASS, L"",
        ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, 
        120, 10, 50, 26, hWnd, NULL, hInst, NULL);

	HWND hwndRandomize = CreateWindow(L"BUTTON", L"Randomize",
		WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
		180, 10, 100, 26, hWnd, (HMENU)IDC_RANDOMIZE, hInst, NULL);

	HDC hdc = GetDC(hWnd);
	RECT rect;
	GetClientRect(hWnd, &rect);
	LPCWSTR text = L"this Window cannot be used";
	DrawText(hdc, text, static_cast<int>(wcslen(text)), &rect, DT_CENTER | DT_VCENTER);
	ReleaseDC(hWnd, hdc);

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_DESTROY) {
		PostQuitMessage(0);
	} else if (message == WM_COMMAND) {
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
		if (wmId == IDC_RANDOMIZE) {
			std::wstring text(100, '\0');
			GetWindowText(hwndSeed, &text[0], 100);
			int seed = 0;
			if (wcslen(text.c_str()) == 0) {
				srand(static_cast<unsigned int>(time(nullptr))); // Seed from the time in milliseconds
				rand();
				seed = rand() % 100000;
				std::wstring seedString = std::to_wstring(seed);
				SetWindowText(hwndSeed, seedString.c_str());
			} else {
				seed = _wtoi(text.c_str());
			}
			srand(seed);
			Randomizer().Randomize();
        }
	}
    return DefWindowProc(hWnd, message, wParam, lParam);
}

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

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