СПЗ отчеты (Власенко О.В.) / 6
.docМіністерство освіти і науки України
Житомирський державний технологічний університет
Кафедра ПЗОТ
Група ПІК-9
Лабораторна робота №6
“Таймер"
Виконав: Оленюк О.О.
Перевірив : Власенко О. В.
Житомир
2012 р.
Зміст
1.
Кафедра ПЗОТ 1
Група ПІК-9 1
Лабораторна робота №6 1
“Таймер" 1
Зміст 2
-
Завдання:
-
Програмування таймеру;
-
Робота з системною датою та часом;
-
Створення декількох таймерів, та обробка повідомлень від таймеру.
-
Програмний код
#include <windows.h>
#include <string>
std::string date, time, st1, st2, st3, st4, st5;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "Timers" ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (szAppName, // window class name
"Timers", // window caption
WS_OVERLAPPEDWINDOW, // window style
500, // initial x position
300, // initial y position
250, // initial x size
200, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (iMsg)
{
case WM_CREATE :
hdc = GetDC (hwnd) ;
date="";
char mass[20];
GetDateFormat(LOCALE_SYSTEM_DEFAULT, DATE_LONGDATE, NULL, NULL, mass, 20);
SetTimer(hwnd, 0, 1000, NULL);
date="Системная дата: "+(std::string)mass;
st1=st2=st3=st4=st5="ВЫКЛ";
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
ReleaseDC (hwnd, hdc) ;
return 0 ;
case WM_PAINT :
InvalidateRect (hwnd, NULL, TRUE) ;
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
TextOut(hdc,10,10, date.c_str(), date.length());
TextOut(hdc,10,30, time.c_str(), time.length());
TextOut(hdc, 10, 50, "Таймер №1 (5 сек.) ", 22);
TextOut(hdc, 10, 70, "Таймер №2 (10 сек.) ", 21);
TextOut(hdc, 10, 90, "Таймер №3 (15 сек.) ", 21);
TextOut(hdc, 10, 110, "Таймер №4 (20 сек.) ", 21);
TextOut(hdc, 10, 130, "Таймер №5 (25 сек.) ", 21);
TextOut(hdc, 150, 50, st1.c_str(), st1.length());
TextOut(hdc, 150, 70, st2.c_str(), st2.length());
TextOut(hdc, 150, 90, st3.c_str(), st3.length());
TextOut(hdc, 150, 110, st4.c_str(), st4.length());
TextOut(hdc, 150, 130, st5.c_str(), st5.length());
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_LBUTTONDOWN:
if((LOWORD(lParam)>150)&&(LOWORD(lParam)<190)&&(HIWORD(lParam)>50)&&(HIWORD(lParam)<67))
if(st1=="ВИКЛ")
SetTimer(hwnd, 1, 5000, NULL), st1="ВКЛ";
else
KillTimer(hwnd, 1), st1 = "ВЫКЛ";
if((LOWORD(lParam)>150)&&(LOWORD(lParam)<190)&&(HIWORD(lParam)>70)&&(HIWORD(lParam)<87))
if(st2=="ВЫКЛ")
SetTimer(hwnd, 2, 10000, NULL), st2="ВКЛ";
else
KillTimer(hwnd, 2), st2 = "ВЫКЛ";
if((LOWORD(lParam)>150)&&(LOWORD(lParam)<190)&&(HIWORD(lParam)>90)&&(HIWORD(lParam)<107))
if(st3=="ВЫКЛ")
SetTimer(hwnd, 3, 15000, NULL), st3="ВКЛ";
else
KillTimer(hwnd, 3), st3 = "ВЫКЛ";
if((LOWORD(lParam)>150)&&(LOWORD(lParam)<190)&&(HIWORD(lParam)>110)&&(HIWORD(lParam)<127))
if(st4=="ВЫКЛ")
SetTimer(hwnd, 4, 20000, NULL), st4="ВКЛ";
else
KillTimer(hwnd, 4), st4 = "ВЫКЛ";
if((LOWORD(lParam)>150)&&(LOWORD(lParam)<190)&&(HIWORD(lParam)>130)&&(HIWORD(lParam)<1477))
if(st5=="ВЫКЛ")
SetTimer(hwnd, 5, 25000, NULL), st5="ВКЛ";
else
KillTimer(hwnd, 5), st5 = "ВЫКЛ";
WndProc (hwnd, WM_PAINT, wParam, lParam);
return 0;
case WM_TIMER:
switch(wParam)
{
case 0: char tm[20]; GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, NULL, NULL, tm, 20);
time="Системное время: "+(std::string)tm; WndProc (hwnd, WM_PAINT, wParam, lParam);
break;
case 1: ::MessageBeep (0); st1 += " ВEEP"; KillTimer(hwnd, 1); break;
case 2: ::MessageBeep (0); st2 += " ВEEP";KillTimer(hwnd, 2); break;
case 3: ::MessageBeep (0); st3 += " ВEEP";KillTimer(hwnd, 3); break;
case 4: ::MessageBeep (0); st4 += " ВEEP";KillTimer(hwnd, 4); break;
case 5: ::MessageBeep (0); st5 += " ВEEP";KillTimer(hwnd, 5); break;
}
WndProc (hwnd, WM_PAINT, wParam, lParam);
return 0;
case WM_DESTROY :
PostQuitMessage (0) ;
KillTimer(hwnd,6);
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
-
Висновок
Отже, при виконанні даної лабораторної роботи були здобуті навики роботи з таймером. В результаті, програма здатна працювати з системною датою та часом, а також створювати декілька таймерів, та обробляти повідомлення від таймеру.