View difference between Paste ID: fGEKHYp9 and 5d9b6rZJ
SHOW: | | - or go back to the newest paste.
1
#define SCREEN_WIDTH 700
2
#define SCREEN_HEIGHT 600
3
#define TIMER_ID 1
4
#define BUFF_SIZE 128
5
#define ID_BUTTON1 1
6
#define ID_BUTTON2 2
7
#define ID_BUTTON3 3
8
#define ID_TEXTFIELD1 4
9
10
#include <windows.h>
11
#include <tchar.h>
12
13
TCHAR className[] = TEXT("Class");
14
TCHAR appName[] = TEXT("Stoper");
15
16
HWND hwnd;
17
MSG msg;
18
19
void SetTimeInWindow(HWND& hwnd, UINT miliSeconds) {
20
	UINT value = miliSeconds;
21
	UINT minute = 0, second = 0, miliSecond = 0;
22
23
	if (value / 60000 >= 1) {
24
		minute = value / 60000;
25
		value -= (60000 * minute);
26
	}
27
28
	if (value / 1000 >= 1) {
29
		second = value / 1000;
30
		value -= (1000 * second);
31
	}
32
33
	miliSecond = value;
34
35
	TCHAR timeBuf[BUFF_SIZE];
36
37
38
39
	_swprintf(timeBuf, TEXT("%i:%i:%i"), minute, second, miliSecond);
40
41
	SetWindowText(hwnd, timeBuf);
42
}
43
44
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
45
	static HWND valueTextField, button1, button2, button3;
46
	static RECT clientRect;
47
	static SYSTEMTIME zeroTime, currentTime;
48
	static UINT miliSeconds = 0;
49
	static UINT test = 0;
50
	static bool isStart = false;
51
52
	switch (msg){
53
		case WM_CREATE: {
54
			SetTimer(hwnd, TIMER_ID, 1, NULL);
55
			GetClientRect(hwnd, &clientRect);
56
57
			HINSTANCE &hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
58
59
			valueTextField = CreateWindowEx(0, TEXT("STATIC"), TEXT("00:00:00"), WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0, hwnd, (HMENU) ID_TEXTFIELD1, hInstance, 0);
60
			if (valueTextField == NULL) {
61
				MessageBox(NULL, TEXT("Some problem with CreateWindowEx by valueTextField"), className, MB_OK);
62
				return 1;
63
			}
64
65
			button1 = CreateWindowEx(0, TEXT("BUTTON"), TEXT("START"), WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0, hwnd, (HMENU)ID_BUTTON1, hInstance, 0);
66
			if (button1 == NULL) {
67
				MessageBox(NULL, TEXT("Some problem with CreateWindowEx by button"), className, MB_OK);
68
				return 1;
69
			}
70
71
			button2 = CreateWindowEx(0, TEXT("BUTTON"), TEXT("STOP"), WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0, hwnd, (HMENU)ID_BUTTON2, hInstance, 0);
72
			if (button2 == NULL) {
73
				MessageBox(NULL, TEXT("Some problem with CreateWindowEx by button1"), className, MB_OK);
74
				return 1;
75
			}
76
77
			button3 = CreateWindowEx(0, TEXT("BUTTON"), TEXT("RESET"), WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0, hwnd, (HMENU)ID_BUTTON3, hInstance, 0);
78
			if (button3 == NULL) {
79
				MessageBox(NULL, TEXT("Some problem with CreateWindowEx by button2"), className, MB_OK);
80
				return 1;
81
			}
82
			}break;
83
84
		case WM_TIMER: {
85
			if (isStart) {
86
				GetLocalTime(&currentTime);
87
88
				miliSeconds += (((currentTime.wMinute - zeroTime.wMinute) * 60000) + ((currentTime.wSecond - zeroTime.wSecond) * 1000) + (currentTime.wMilliseconds - zeroTime.wMilliseconds) - test);
89
				test = (((currentTime.wMinute - zeroTime.wMinute) * 60000) + ((currentTime.wSecond - zeroTime.wSecond) * 1000) + (currentTime.wMilliseconds - zeroTime.wMilliseconds));
90
91
				SetTimeInWindow(valueTextField, miliSeconds);
92
			}
93
			}break;
94
95
		case WM_SIZE: {
96
			GetClientRect(hwnd, &clientRect);
97
			
98
			MoveWindow(valueTextField, clientRect.right / 3, clientRect.bottom / 2.5, clientRect.right / 7, clientRect.bottom / 20, 1);
99
			MoveWindow(button1, clientRect.right / 3, clientRect.bottom / 2, clientRect.right / 7, clientRect.bottom / 20, 1);
100
			MoveWindow(button2, clientRect.right / 2, clientRect.bottom / 2, clientRect.right / 7, clientRect.bottom / 20, 1);
101
			MoveWindow(button3, clientRect.right / 2, clientRect.bottom / 2.5, clientRect.right / 7, clientRect.bottom / 20, 1);
102
			}break;
103
104
		case WM_GETMINMAXINFO:
105
			((MINMAXINFO*)lParam)->ptMinTrackSize.x = 600;
106
			((MINMAXINFO*)lParam)->ptMinTrackSize.y = 500;
107
			break;
108
109
		case WM_COMMAND:
110
			switch (wParam) {
111
				case ID_BUTTON1:
112
					test = 0;
113
					isStart = true;
114
					GetLocalTime(&zeroTime);
115
					break;
116
117
				case ID_BUTTON2:
118
					isStart = false;
119
					break;
120
121
				case ID_BUTTON3:
122
					miliSeconds = test = 0;
123
					isStart = false;
124
					SetTimeInWindow(valueTextField, miliSeconds);
125
					break;
126
			}
127
			break;
128
			
129
		case WM_CLOSE:	
130
			DestroyWindow(hwnd);	
131
			break;
132
133
		case WM_DESTROY: {
134
			KillTimer(hwnd, TIMER_ID);
135
			PostQuitMessage(0);
136
			}break;
137
138
		default:	
139
			return DefWindowProc(hwnd, msg, wParam, lParam);
140
	}
141
142
	return 0;
143
}
144
145
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
146
{
147
	WNDCLASSEX wc;
148
	wc.cbSize = sizeof(WNDCLASSEX);
149
	wc.style = 0;
150
	wc.cbClsExtra = 0;
151
	wc.cbWndExtra = 0;
152
	wc.hInstance = hInstance;
153
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
154
	wc.lpfnWndProc = WndProc;
155
	wc.lpszClassName = className;
156
	wc.lpszMenuName = NULL;
157
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
158
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
159
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
160
161
	if (RegisterClassEx(&wc) == 0) {
162
		MessageBox(NULL, TEXT("Some problem with RegisterClassEx"), className, MB_OK);
163
		return 1;
164
	}
165
166
	hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_CLIENTEDGE, className, appName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, hInstance, 0);
167
	if (hwnd == NULL) {
168
		MessageBox(NULL, TEXT("Some problem with CreateWindowEx by hwnd"), className, MB_OK);
169
		return 1;
170
	}
171
172
	ShowWindow(hwnd, nCmdShow);
173
	UpdateWindow(hwnd);
174
175
	while (GetMessage(&msg, NULL, 0, 0) > 0)
176
	{
177
		TranslateMessage(&msg);
178
		DispatchMessage(&msg);
179
	}
180
181
	UnregisterClass(className, hInstance);
182
183
	return msg.wParam;
184
}