-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
591 lines (489 loc) · 15.6 KB
/
main.cpp
File metadata and controls
591 lines (489 loc) · 15.6 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#include <windows.h> // sisteminiai
#include <tchar.h>
#include "constants.h" // projekto
////----
//Glogal variables
HWND g_hToolbar = NULL; // toolbar
////-----------------
// animacijai kamuoliukas
//
const int ID_TIMER = 1;
const int BALL_MOVE_DELTA = 0; //2
const int PUSH = 10 ;
typedef struct _BALLINFO
{
int width;
int height;
int x;
int y;
int dx;
int dy;
}BALLINFO;
BALLINFO g_ballInfo = {5,5,30,30,0,0 }; // create structure with default values
HBITMAP g_hbmBall = NULL;
HBITMAP g_hbmMask = NULL;
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hbmColour);
SelectObject(hdcMem2, hbmMask);
SetBkColor(hdcMem, crTransparent);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return hbmMask;
}
void DrawBall(HDC hdc, RECT* prc)
{
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmMask);
FillRect(hdcBuffer, prc, (HBRUSH)GetStockObject(WHITE_BRUSH));
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, g_hbmBall);
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT);
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
}
void UpdateBall(RECT* prc)
{
g_ballInfo.x += g_ballInfo.dx;
g_ballInfo.y += g_ballInfo.dy;
if(g_ballInfo.x < 0)
{
g_ballInfo.x = 0;
g_ballInfo.dx = BALL_MOVE_DELTA;
}
else if(g_ballInfo.x + g_ballInfo.width > prc->right)
{
g_ballInfo.x = prc->right - g_ballInfo.width;
g_ballInfo.dx = -BALL_MOVE_DELTA;
}
if(g_ballInfo.y < 0)
{
g_ballInfo.y = 0;
g_ballInfo.dy = BALL_MOVE_DELTA;
}
else if(g_ballInfo.y + g_ballInfo.height > prc->bottom)
{
g_ballInfo.y = prc->bottom - g_ballInfo.height;
g_ballInfo.dy = -BALL_MOVE_DELTA;
}
}
BOOL LoadTextFileToEdit(LPCTSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
if(&g_ballInfo != NULL)
{
LPSTR pszText;
DWORD dwBufferSize = sizeof(BALLINFO) + 1;
DWORD dwWritten;
if(ReadFile(hFile,&g_ballInfo, sizeof(BALLINFO), &dwWritten, NULL))
bSuccess = TRUE;
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL SaveTextFileFromEdit( LPCTSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
if(&g_ballInfo != NULL)
{
LPSTR pszText;
DWORD dwBufferSize = sizeof(BALLINFO) + 1;
DWORD dwWritten;
if(WriteFile(hFile, (LPCVOID)&g_ballInfo, sizeof(BALLINFO), &dwWritten, NULL))
bSuccess = TRUE;
}
CloseHandle(hFile);
}
return bSuccess;
}
void DoFileOpen(HWND hwnd)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
//HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
LoadTextFileToEdit( szFileName);
}
}
void DoFileSave(HWND hwnd)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if(GetSaveFileName(&ofn))
{
//HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SaveTextFileFromEdit(szFileName);
}
}
////---------
// DIALOGA ABOUT apdorojantis switchas
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
////------------------
//toolbar message switch
BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
// This is where we set up the dialog box, and initialise any default values
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_PRESS:
MessageBox(hwnd, "Hi!", "This is a message",
MB_OK | MB_ICONEXCLAMATION);
break;
case IDC_OTHER:
MessageBox(hwnd, "Bye!", "wuuups", MB_OK | MB_ICONEXCLAMATION);
break;
////////////////////////
// buttons
//////////////////
case IDC_B_R :
{
g_ballInfo.x += PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
// siunciu pagal gluobalu, BET NIEKAS NEVEIKIA
SendMessage (g_hToolbar , WM_COMMAND, (WPARAM)IDC_EVENTAS,(LPARAM)0);
}
break;
// KODEL NEVEIKIA !??!?!!??!?!!
case IDC_EVENTAS:
{
MessageBox(hwnd, "issikvieciau eventa,jeeeeeee", "wuuups", MB_OK | MB_ICONEXCLAMATION);
}
break;
case IDC_B_L :
{
g_ballInfo.x -= PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
}
break;
case IDC_B_TL :
{
g_ballInfo.x -= PUSH;
g_ballInfo.y -= PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
}
break;
case IDC_B_T :
{
g_ballInfo.y -= PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
}
break;
case IDC_B_TR :
{
g_ballInfo.x += PUSH;
g_ballInfo.y -= PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
}
break;
case IDC_B_B :
{
g_ballInfo.y += PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
}
break;
case IDC_B_BL :
{
g_ballInfo.x -= PUSH;
g_ballInfo.y += PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
}
break;
case IDC_B_BR :
{
g_ballInfo.x += PUSH;
g_ballInfo.y += PUSH;
SetDlgItemInt(hwnd, IDC_EDIT_X, g_ballInfo.x, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, g_ballInfo.y, FALSE);
}
//--------------------------
break;
case IDC_UPDATE:
{
switch(HIWORD(wParam))
{
case IDC_EVENTAS:
MessageBox(hwnd, "Bye!", "Tsssssa message",
MB_OK | MB_ICONEXCLAMATION);
break;
}
}
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
//----------------
/* pagrindinis switch'as_*/
//-----------------
long __stdcall WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
/* right click'inant issoka langas*/
case WM_RBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_YESNO | MB_ICONINFORMATION );
}
// programos pabaiga
break;
case WM_DESTROY:
DestroyWindow(g_hToolbar); // destroy toolbar
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
//--------
// Min/MAX size of window
// !!error!! pjaunasi su toolbaru
/* case WM_GETMINMAXINFO:
{
LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
lpMMI->ptMinTrackSize.x = 300; // min x
lpMMI->ptMinTrackSize.y = 300;
lpMMI->ptMaxTrackSize.x = 300; // max x
lpMMI->ptMaxTrackSize.y = 300;
}
*/
//------------
// on window creation
case WM_CREATE:
{
g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR),
hwnd, ToolDlgProc);
if(g_hToolbar != NULL)
{
ShowWindow(g_hToolbar, SW_SHOW);
}
else
{
MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",
MB_OK | MB_ICONINFORMATION);
}
//---------
// animation
UINT ret;
BITMAP bm;
g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
if(g_hbmBall == NULL)
MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);
g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0));
if(g_hbmMask == NULL)
MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
GetObject(g_hbmBall, sizeof(bm), &bm);
//ZeroMemory(&g_ballInfo, sizeof(g_ballInfo));
g_ballInfo.width = bm.bmWidth;
g_ballInfo.height = bm.bmHeight;
g_ballInfo.dx = BALL_MOVE_DELTA;
g_ballInfo.dy = BALL_MOVE_DELTA;
ret = SetTimer(hwnd, ID_TIMER, 50, NULL);
if(ret == 0)
MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
}
break;
case WM_PAINT: //pasileidzia tik pirma karta
{
RECT rcClient;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rcClient);
DrawBall(hdc, &rcClient);
EndPaint(hwnd, &ps);
}
break;
case WM_TIMER:
{
RECT rcClient;
HDC hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClient);
UpdateBall(&rcClient);
DrawBall(hdc, &rcClient);
ReleaseDC(hwnd, hdc);
}
/*
case WM_INITDIALOG:
// This is where we set up the dialog box, and initialise any default values
SetDlgItemInt(hwnd, IDC_EDIT_X, 5, FALSE);
SetDlgItemInt(hwnd, IDC_EDIT_Y, 15, FALSE);
break;
*/
////------------
// PAGRINDIS - apdorojami messagai kaip mygtuku paspaudimai
/////
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
// show/hide toolbar
case ID_DIALOG_SHOW:
ShowWindow(g_hToolbar, SW_SHOW);
break;
case ID_DIALOG_HIDE:
ShowWindow(g_hToolbar, SW_HIDE);
break;
//--------------
//file I/O
case ID_FILE_OPEN :
DoFileOpen(hwnd);
break;
case ID_FILE_SAVEAS :
DoFileSave(hwnd);
break;
//---
case IDM_FILE_EXIT:
{
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
}
break;
case IDM_FILE_OPEN:
{
//cia irgi kaskodel neveikia
SendMessage (g_hToolbar , IDC_EVENTAS, (WPARAM)0,(LPARAM)0);
/*
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK |
MB_ICONINFORMATION);
*/
}
break;
/* dialogas ABOUT */
case ID_HELP_ABOUT:
{
int ret = DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
if(ret == IDOK){
MessageBox(hwnd, "Dialog exited with IDOK.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(ret == IDCANCEL){
MessageBox(hwnd, "Dialog exited with IDCANCEL.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(ret == -1){
MessageBox(hwnd, "Dialog failed!", "Error",
MB_OK | MB_ICONINFORMATION);
}
}
break;
// END dialogas
}
}
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
//--------------------------
// pagrindinio lango inicilizacija
//--------------------------
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpszArgument, int nCmdShow)
{
char szClassName[ ] = "Zygio super aplikacija";
MSG messages;
WNDCLASS wc;
HWND hwnd;
//--------
// acceleratoriai
HACCEL hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCEL1));
wc.hInstance = hInstance, wc.lpszClassName = szClassName;
wc.lpfnWndProc = WindowProcedure, wc.style = 0;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION), wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAIN_MENU), wc.cbClsExtra = 0;
wc.cbWndExtra = 0, wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
RegisterClass(&wc);
hwnd=CreateWindow(szClassName,"Zygio super aplikacija",WS_OVERLAPPEDWINDOW,50,50,544,375,HWND_DESKTOP,NULL,hInstance,NULL);
ShowWindow (hwnd, nCmdShow);
while(GetMessage (&messages, NULL, 0, 0)>0)
{
if (!TranslateAccelerator(hwnd, hAccel, &messages) && !IsDialogMessage(g_hToolbar, &messages) )
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
return messages.wParam; // zinute kuria apdorosime auksciau esancioje funkcijoje
}