diff options
Diffstat (limited to 'Source/Main.cpp')
-rw-r--r-- | Source/Main.cpp | 225 |
1 files changed, 105 insertions, 120 deletions
diff --git a/Source/Main.cpp b/Source/Main.cpp index 4748b73..5c7cefa 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp | |||
@@ -2,177 +2,162 @@ | |||
2 | // | 2 | // |
3 | 3 | ||
4 | #include "stdafx.h" | 4 | #include "stdafx.h" |
5 | #include "Main.h" | 5 | #include "resource.h" |
6 | #include "tchar.h" | ||
7 | #include "Randomizer.h" | ||
8 | #include <Richedit.h> | ||
9 | #include <iostream> | ||
10 | #include <ctime> | ||
11 | #include <string> | ||
6 | 12 | ||
7 | #define MAX_LOADSTRING 100 | 13 | #define MAX_LOADSTRING 100 |
14 | #define IDC_RANDOMIZE 0x464 | ||
8 | 15 | ||
9 | // Global Variables: | 16 | HINSTANCE hInst; |
10 | HINSTANCE hInst; // current instance | 17 | WCHAR szWindowClass[MAX_LOADSTRING]; |
11 | WCHAR szTitle[MAX_LOADSTRING]; // The title bar text | 18 | HWND hwndSeed; |
12 | WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name | 19 | std::shared_ptr<Randomizer> randomizer; |
13 | 20 | ||
14 | // Forward declarations of functions included in this code module: | 21 | // Forward declares |
15 | ATOM MyRegisterClass(HINSTANCE hInstance); | 22 | ATOM MyRegisterClass(HINSTANCE hInstance); |
16 | BOOL InitInstance(HINSTANCE, int); | 23 | BOOL InitInstance(HINSTANCE, int); |
17 | LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); | 24 | LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); |
18 | INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); | 25 | INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); |
19 | 26 | ||
20 | int APIENTRY wWinMain(_In_ HINSTANCE hInstance, | 27 | int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) |
21 | _In_opt_ HINSTANCE hPrevInstance, | ||
22 | _In_ LPWSTR lpCmdLine, | ||
23 | _In_ int nCmdShow) | ||
24 | { | 28 | { |
25 | UNREFERENCED_PARAMETER(hPrevInstance); | 29 | LoadStringW(hInstance, IDC_SOURCE, szWindowClass, MAX_LOADSTRING); |
26 | UNREFERENCED_PARAMETER(lpCmdLine); | ||
27 | |||
28 | // TODO: Place code here. | ||
29 | |||
30 | // Initialize global strings | ||
31 | LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); | ||
32 | LoadStringW(hInstance, IDC_SOURCE, szWindowClass, MAX_LOADSTRING); | ||
33 | MyRegisterClass(hInstance); | 30 | MyRegisterClass(hInstance); |
34 | 31 | if (!InitInstance(hInstance, nCmdShow)) { | |
35 | // Perform application initialization: | ||
36 | if (!InitInstance (hInstance, nCmdShow)) | ||
37 | { | ||
38 | return FALSE; | 32 | return FALSE; |
39 | } | 33 | } |
40 | 34 | randomizer = std::make_shared<Randomizer>(); | |
41 | HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOURCE)); | 35 | if (randomizer == nullptr) { |
36 | return FALSE; | ||
37 | } | ||
42 | 38 | ||
43 | MSG msg; | 39 | MSG msg; |
44 | 40 | while (!GetMessage(&msg, nullptr, 0, 0) == 0) | |
45 | // Main message loop: | ||
46 | while (GetMessage(&msg, nullptr, 0, 0)) | ||
47 | { | 41 | { |
48 | if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) | 42 | TranslateMessage(&msg); |
49 | { | 43 | DispatchMessage(&msg); |
50 | TranslateMessage(&msg); | ||
51 | DispatchMessage(&msg); | ||
52 | } | ||
53 | } | 44 | } |
54 | 45 | ||
55 | return (int) msg.wParam; | 46 | return (int) msg.wParam; |
56 | } | 47 | } |
57 | 48 | ||
58 | |||
59 | |||
60 | // | ||
61 | // FUNCTION: MyRegisterClass() | ||
62 | // | ||
63 | // PURPOSE: Registers the window class. | ||
64 | // | ||
65 | ATOM MyRegisterClass(HINSTANCE hInstance) | 49 | ATOM MyRegisterClass(HINSTANCE hInstance) |
66 | { | 50 | { |
67 | WNDCLASSEXW wcex; | 51 | WNDCLASSEXW wcex = { |
68 | 52 | sizeof(WNDCLASSEX), | |
69 | wcex.cbSize = sizeof(WNDCLASSEX); | 53 | CS_HREDRAW | CS_VREDRAW, |
70 | 54 | WndProc, | |
71 | wcex.style = CS_HREDRAW | CS_VREDRAW; | 55 | 0, |
72 | wcex.lpfnWndProc = WndProc; | 56 | 0, |
73 | wcex.cbClsExtra = 0; | 57 | hInstance, |
74 | wcex.cbWndExtra = 0; | 58 | LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOURCE)), |
75 | wcex.hInstance = hInstance; | 59 | LoadCursor(nullptr, IDC_ARROW), |
76 | wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOURCE)); | 60 | (HBRUSH)(COLOR_WINDOW+1), |
77 | wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); | 61 | MAKEINTRESOURCEW(IDC_SOURCE), |
78 | wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); | 62 | szWindowClass, |
79 | wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_SOURCE); | 63 | LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)), |
80 | wcex.lpszClassName = szWindowClass; | 64 | }; |
81 | wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); | ||
82 | 65 | ||
83 | return RegisterClassExW(&wcex); | 66 | return RegisterClassExW(&wcex); |
84 | } | 67 | } |
85 | 68 | ||
86 | // | ||
87 | // FUNCTION: InitInstance(HINSTANCE, int) | ||
88 | // | ||
89 | // PURPOSE: Saves instance handle and creates main window | ||
90 | // | ||
91 | // COMMENTS: | ||
92 | // | ||
93 | // In this function, we save the instance handle in a global variable and | ||
94 | // create and display the main program window. | ||
95 | // | ||
96 | BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) | 69 | BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) |
97 | { | 70 | { |
98 | hInst = hInstance; // Store instance handle in our global variable | 71 | hInst = hInstance; // Store instance handle in our global variable |
99 | 72 | ||
100 | HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, | 73 | WCHAR szTitle[MAX_LOADSTRING]; |
74 | LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); | ||
75 | HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, | ||
101 | CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); | 76 | CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); |
102 | 77 | ||
103 | if (!hWnd) | 78 | if (!hWnd) |
104 | { | 79 | { |
105 | return FALSE; | 80 | return FALSE; |
106 | } | 81 | } |
107 | 82 | ||
108 | ShowWindow(hWnd, nCmdShow); | 83 | LoadLibrary(L"Msftedit.dll"); |
109 | UpdateWindow(hWnd); | 84 | HWND label = CreateWindow(L"BUTTON", L"Enter a seed:", |
110 | 85 | WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, | |
111 | return TRUE; | 86 | 10, 10, 100, 26, hWnd, NULL, hInst, NULL); |
87 | |||
88 | hwndSeed = CreateWindowEx(0, MSFTEDIT_CLASS, L"", | ||
89 | ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, | ||
90 | 120, 10, 50, 26, hWnd, NULL, hInst, NULL); | ||
91 | |||
92 | HWND hwndRandomize = CreateWindow(L"BUTTON", L"Randomize", | ||
93 | WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, | ||
94 | 180, 10, 100, 26, hWnd, (HMENU)IDC_RANDOMIZE, hInst, NULL); | ||
95 | |||
96 | HDC hdc = GetDC(hWnd); | ||
97 | RECT rect; | ||
98 | GetClientRect(hWnd, &rect); | ||
99 | LPCWSTR text = L"this Window cannot be used"; | ||
100 | DrawText(hdc, text, static_cast<int>(wcslen(text)), &rect, DT_CENTER | DT_VCENTER); | ||
101 | ReleaseDC(hWnd, hdc); | ||
102 | |||
103 | ShowWindow(hWnd, nCmdShow); | ||
104 | UpdateWindow(hWnd); | ||
105 | return TRUE; | ||
112 | } | 106 | } |
113 | 107 | ||
114 | // | ||
115 | // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) | ||
116 | // | ||
117 | // PURPOSE: Processes messages for the main window. | ||
118 | // | ||
119 | // WM_COMMAND - process the application menu | ||
120 | // WM_PAINT - Paint the main window | ||
121 | // WM_DESTROY - post a quit message and return | ||
122 | // | ||
123 | // | ||
124 | LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | 108 | LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
125 | { | 109 | { |
126 | switch (message) | 110 | if (WM_COMMAND == WM_DESTROY) { |
127 | { | 111 | PostQuitMessage(0); |
128 | case WM_COMMAND: | 112 | } else { |
129 | { | 113 | int wmId = LOWORD(wParam); |
130 | int wmId = LOWORD(wParam); | 114 | // Parse the menu selections: |
131 | // Parse the menu selections: | 115 | switch (wmId) |
132 | switch (wmId) | ||
133 | { | ||
134 | case IDM_ABOUT: | ||
135 | DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); | ||
136 | break; | ||
137 | case IDM_EXIT: | ||
138 | DestroyWindow(hWnd); | ||
139 | break; | ||
140 | default: | ||
141 | return DefWindowProc(hWnd, message, wParam, lParam); | ||
142 | } | ||
143 | } | ||
144 | break; | ||
145 | case WM_PAINT: | ||
146 | { | 116 | { |
147 | PAINTSTRUCT ps; | 117 | case IDM_ABOUT: |
148 | HDC hdc = BeginPaint(hWnd, &ps); | 118 | DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); |
149 | // TODO: Add any drawing code that uses hdc here... | 119 | break; |
150 | EndPaint(hWnd, &ps); | 120 | case IDC_RANDOMIZE: |
121 | { | ||
122 | std::wstring text(100, '\0'); | ||
123 | GetWindowText(hwndSeed, &text[0], 100); | ||
124 | int seed = 0; | ||
125 | if (wcslen(text.c_str()) == 0) { | ||
126 | srand(static_cast<unsigned int>(time(nullptr))); // Seed from the time in milliseconds | ||
127 | rand(); | ||
128 | seed = rand() % 100000; | ||
129 | std::wstring seedString = std::to_wstring(seed); | ||
130 | SetWindowText(hwndSeed, seedString.c_str()); | ||
131 | } else { | ||
132 | seed = _wtoi(text.c_str()); | ||
133 | } | ||
134 | randomizer->Randomize(seed); | ||
135 | |||
136 | break; | ||
137 | } | ||
138 | case IDM_EXIT: | ||
139 | DestroyWindow(hWnd); | ||
140 | break; | ||
141 | default: | ||
142 | return DefWindowProc(hWnd, message, wParam, lParam); | ||
151 | } | 143 | } |
152 | break; | 144 | } |
153 | case WM_DESTROY: | 145 | return DefWindowProc(hWnd, message, wParam, lParam); |
154 | PostQuitMessage(0); | ||
155 | break; | ||
156 | default: | ||
157 | return DefWindowProc(hWnd, message, wParam, lParam); | ||
158 | } | ||
159 | return 0; | ||
160 | } | 146 | } |
161 | 147 | ||
162 | // Message handler for about box. | ||
163 | INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) | 148 | INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) |
164 | { | 149 | { |
165 | UNREFERENCED_PARAMETER(lParam); | 150 | UNREFERENCED_PARAMETER(lParam); |
166 | switch (message) | 151 | switch (message) |
167 | { | 152 | { |
168 | case WM_INITDIALOG: | 153 | case WM_INITDIALOG: |
169 | return (INT_PTR)TRUE; | 154 | return TRUE; |
170 | 155 | ||
171 | case WM_COMMAND: | 156 | case WM_COMMAND: |
172 | if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) | 157 | if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) |
173 | { | 158 | { |
174 | EndDialog(hDlg, LOWORD(wParam)); | 159 | EndDialog(hDlg, LOWORD(wParam)); |
175 | return (INT_PTR)TRUE; | 160 | return TRUE; |
176 | } | 161 | } |
177 | break; | 162 | break; |
178 | } | 163 | } |