Pushok icon

Visual Stop-Watch (selfmade 100%)

Pushok | PRO | 04/22/11 04:00:22 PM UTC | 0 ⭐ | 459 👁️ | Never ⏰ | []
C |

3.45 KB

|

None

|

0 👍

/

0 👎

#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <cmath>
 
#define PI 3.14159265
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
VOID CALLBACK TimerEvent( HWND window, UINT Msg, UINT_PTR EventID, DWORD SYSTime);
 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow )
{
    MSG  msg ;    
    WNDCLASS wc;
    ZeroMemory(&wc, sizeof(wc));
 
    wc.lpszClassName = TEXT( "Timer" );
    wc.hInstance     = hInstance ;
    wc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
    wc.lpfnWndProc   = WndProc ;
    wc.hCursor       = LoadCursor(0, IDC_ARROW);
 
 
    RegisterClass(&wc);
    CreateWindow( wc.lpszClassName, TEXT("Stop Watch"),
        WS_SYSMENU | WS_VISIBLE,
        150, 150, 230, 230, 0, 0, hInstance, 0);  
 
    while( GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int) msg.wParam;
}
 
const double mdeg = 270;
double SecDeg = mdeg;
double MinDeg = mdeg;
int globTime = 0;
 
int CalculateTime(int time)
{
    int csec = time;
    do 
    {
 
        if(csec - 60 <= 0)
            break;
        csec -= 60;
 
    } while (1);
 
    return csec << 16 | ((time - csec) / 60);
}
 
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    static HWND MenuItems[3] = {0};
    static POINT pt = {230/2, 230/2 - 40};
    static int widthLine = 70;
 
    switch(msg)  
    {
    case WM_CREATE:
        {
            MenuItems[0] = CreateWindow(TEXT("button"), TEXT("Старт"),    
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                120, 150, 80, 25,        
                hwnd, (HMENU) 1, NULL, NULL);  
 
            MenuItems[1] = CreateWindow(TEXT("button"), TEXT("Стоп"),    
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20, 150, 80, 25,        
                hwnd, (HMENU) 2, NULL, NULL);
 
            break;
        }
 
    case WM_COMMAND:
        {
            if(HIWORD(wParam) == BN_CLICKED)
            {
                if (LOWORD(wParam) == 1) 
                {
                    SecDeg = mdeg;
                    globTime = 0;
                    KillTimer(hwnd, 1);
                    SetTimer(hwnd, 1, 1000, TimerEvent);
                } 
                else if(LOWORD(wParam) == 2)
                {
                    SecDeg = mdeg;
                    globTime = 0;
                    InvalidateRect(hwnd, 0, true);
                    KillTimer(hwnd, 1);
                }
            }
            break;
        }
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
 
            HDC hd = BeginPaint(hwnd, &ps);
 
            MoveToEx(hd, pt.x, pt.y, 0);
            for(int i=0;i<360;i+=6)
            {
                SetPixel(hd, pt.x + (cos(i*PI/180)*(widthLine+1)), pt.y + (cos((180-(i+90))*PI/180)*(widthLine + 1)), RGB(0,0,0));
            }
 
            for(int i=0;i<360;i+=30)
            {
                Rectangle(hd, pt.x + (cos(i*PI/180)*(widthLine + 1)), pt.y + (cos((180-(i+90))*PI/180)*(widthLine + 1)), pt.x + (cos(i*PI/180)*(widthLine + 4)), pt.y + (cos((180-(i+90))*PI/180)*(widthLine + 4)));
            }
            
            SecDeg = mdeg + (CalculateTime(globTime) >> 16) * 6;
            MinDeg = mdeg + (CalculateTime(globTime) & 0xFFFF) * 6;
 
            SelectPen(hd, CreatePen(PS_SOLID, 2, RGB(0,0,255)));
            LineTo(hd,pt.x + (cos(SecDeg*PI/180)*widthLine),pt.y + (cos((180-(SecDeg+90))*PI/180)*(widthLine)));
 
            SelectPen(hd, CreatePen(PS_SOLID, 2, RGB(255,0,0)));
            MoveToEx(hd, pt.x, pt.y, 0);
 
            LineTo(hd,pt.x + (cos(MinDeg*PI/180)*widthLine / 2),pt.y + (cos((180-(MinDeg+90))*PI/180)*(widthLine)/2));
 
            EndPaint(hwnd, &ps);
            break;
        }
    case WM_DESTROY:
        {
            KillTimer(hwnd, 1);
            PostQuitMessage(0);
            break;
        }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}
 
VOID CALLBACK TimerEvent( HWND window, UINT Msg, UINT_PTR EventID, DWORD SYSTime)
{
    globTime++;
    InvalidateRect(window, 0, true);
}

Comments