Raport: Lucrare de Laborator Nr. 4-5
Raport: Lucrare de Laborator Nr. 4-5
Raport: Lucrare de Laborator Nr. 4-5
Raport
Programarea pilotată de evenimente
Lucrare de laborator nr. 4-5
Tema : Ceasul sistemului Windows. Ferestre
descendent
Realizat de :
Verificat de :
Chișinau 2021
Sarcina: Scrieţi un program care afişează un test, utilizând radio
butoane, check boxes, butoane push și pentru cronometrare de folosit timer-ul.
Codul sursă :
#include "framework.h"
#include "WindowsProject4.h"
#define NUM 10
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
MSG msg;
return (int)msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
int DisplayResourceNAMessageBox()
{
int msgboxID = MessageBox(
NULL,
(LPCWSTR)L"Timpul a expirat",
(LPCWSTR)L"Alert",
MB_ICONWARNING
);
return msgboxID;
}
int Congrats()
{
int msgboxID = MessageBox(
NULL,
(LPCWSTR)L"Correct",
(LPCWSTR)L"Congrats",
MB_OK
);
return msgboxID;
}
int Retry()
{
int msgboxID = MessageBox(
NULL,
(LPCWSTR)L"Incorrect. Try again.",
(LPCWSTR)L"Incorrect",
MB_RETRYCANCEL
);
return msgboxID;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
static HWND hwndButton[NUM];
static RECT rect;
static int cxChar, cyChar;
HDC hdc;
PAINTSTRUCT ps;
int i;
TEXTMETRIC tm;
case WM_CREATE:
uResult = SetTimer(hWnd, // handle to main window
0, // timer identifier
10000, // 10-second interval
(TIMERPROC)NULL); // no timer callback
hdc = GetDC(hWnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hWnd, hdc);
hwndButton[0] = CreateWindowW(TEXT("button"), TEXT("Care este cel mai lung fluviu
din Europa"), WS_CHILD | WS_VISIBLE |
BS_GROUPBOX, cxChar, cyChar * 1,
20 * cxChar, 7 * cyChar / 4, hWnd, (HMENU)0,
hInst, NULL);
hwndButton[1] = CreateWindowW(TEXT("button"), TEXT("Eden"), WS_CHILD | WS_VISIBLE
|
BS_AUTORADIOBUTTON, cxChar, cyChar * (1 + 2),
20 * cxChar, 7 * cyChar / 4, hWnd, (HMENU)1,
hInst, NULL);
hwndButton[2] = CreateWindowW(TEXT("button"), TEXT("Onega"), WS_CHILD |
WS_VISIBLE |
BS_AUTORADIOBUTTON, cxChar, cyChar * (1 + 2 * 2),
20 * cxChar, 7 * cyChar / 4, hWnd, (HMENU)2,
hInst, NULL);
hwndButton[3] = CreateWindowW(TEXT("button"), TEXT("Volga"), WS_CHILD |
WS_VISIBLE |
BS_AUTORADIOBUTTON, cxChar, cyChar * (1 + 2 * 3),
20 * cxChar, 7 * cyChar / 4, hWnd, (HMENU)3,
hInst, NULL);
hwndButton[4] = CreateWindowW(TEXT("button"), TEXT("Lee"), WS_CHILD | WS_VISIBLE
|
BS_AUTORADIOBUTTON, cxChar, cyChar * (1 + 2 * 4),
20 * cxChar, 7 * cyChar / 4, hWnd, (HMENU)4,
hInst, NULL);
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case 10:
if (SendMessage(hwndButton[3], BM_GETCHECK, BST_CHECKED, 1) &&
SendMessage(hwndButton[8], BM_GETCHECK, BST_CHECKED, 1))
Congrats();
else {
Retry();
for (int i = 0; i < NUM; i++) {
SendMessage(hwndButton[i], BM_SETCHECK, BST_UNCHECKED, 0);
}
}
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_TIMER:
DisplayResourceNAMessageBox();
PostQuitMessage(0);
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
KillTimer(hWnd, uResult);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Concluzie :
În această lucrare de laborator am creat o aplicație Windows care afișează o
fereastră, în centrul zonei de client fiind un test. În cazul în care selectăm
răspunsurile corecte, ne va afișa o fereastră care va afișa o felicitare, în caz
contrar, butoanele se deselectează și utilizatorul mai are o șansă de a încerca
testul. La expirarea a 10 secunde, apare o fereastră alert după care se așteaptă ca
utilizatorul să apese pe ok iar testul să se închidă.