[WinAPI] Digital Clock

 

 

// definition of windows.h application file contains the desired data type and data structure 
#include <windows.h> 
#include <stdlib.h> 
#include <time.h> 
LRESULT CALLBACK the WndProc (the HWND, UINT, WPARAM, LPARAM); // window function description
 // --------- following initialization window class -------------- 
int WINAPI the WinMain (hInstance the hINSTANCE, the hINSTANCE hPrevInst, LPSTR the lpszCmdLine, int the nCmdShow) 
{ 
    the HWND HWND; 
    the MSG Msg; 
    the WNDCLASS WNDCLASS; 
    char , lpszClassName [] = " window " ;      // window class name 
    char lpszTitle [] = " My_Windows " ;    // window title name 

    // window class definition 
    wndclass.style = CS_HREDRAW | CS_VREDRAW;                   // window type is the default type 
    wndclass.lpfnWndProc = the WndProc;       // window handler for the WndProc 
    wndclass.cbClsExtra = 0 ;              // window class None extended 
    wndclass.cbWndExtra = 0 ;              // window instance no extension 
    wndclass.hInstance = hInstance;      // this instance handle 

    wndclass.hIcon = the LoadIcon (NULL, IDI_APPLICATION); // minimized icon in the window to the default icon 
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); // window using the cursor arrow
    = wndclass.hbrBackground (HBRUSH) the GetStockObject (WHITE_BRUSH); // window background is white 
    wndclass.lpszMenuName = NULL; // window no menu 
    wndclass.lpszClassName =, lpszClassName; // window class named "sample window"
 // - be --------- following registration window class 
    IF (! the RegisterClass (& WNDCLASS)) // if the registration fails sound an alarm sound Gao 
    { 
        the MessageBeep ( 0 );
         return FALSE; 
    } 

// create a window 
    HWND = the CreateWindow ( 
                        lpszClassName, // window class name 
                        lpszTitle,       // title name of the window instance
                        WS_OVERLAPPEDWINDOW, // window style 
                        CW_USEDEFAULT, 
                        CW_USEDEFAULT,   // upper left corner of the window coordinates to the default value 
                        CW_USEDEFAULT, 
                        CW_USEDEFAULT,   // window of Gao and wide to the default value 
                        NULL,            // the window no parent window 
                        NULL,            // this window without main menu 
                        hInstance,       // create the window of the application currently handle 
                        NULL             // do not use this value 
                        ); 



    ShowWindow (hwnd, nCmdShow);         //Display window 
    the UpdateWindow (HWND);                  // draw a user area 
    the while (the GetMessage (& Msg, NULL, 0 , 0 )) // message loop 
    { 
        TranslateMessage ( & Msg); 
        the DispatchMessage ( & Msg); 
    } 
    return msg.wParam;                   // program termination information back to the system 
}
 #define the LR (+ rect.left rect.right)
 #define the TD (rect.bottom + rect.top)
 #define x_eps (the LR *. 4/18 is /. 8)
 #define y_eps (the TD * 3/5/8)
 void CreateNumbers (HDC hDC, RECT RECT, int number, int pos)
{
    int st_x, st_y;
    if (pos == 1)
        st_x =  LR/18+x_eps, st_y = TD/5+y_eps;
    else if (pos == 2)
        st_x =  LR*5/18+x_eps, st_y = TD/5+y_eps;
    else if (pos == 3)
        st_x =  LR*9/18+x_eps, st_y = TD/5+y_eps;
    else if (pos == 4)
        st_x =  LR*13/18+x_eps, st_y = TD/5+y_eps;
    switch(number)
    {
    case 0:
        MoveToEx(hDC, st_x, st_y, NULL);//1
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y);//2
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);//6
        LineTo(hDC, st_x, st_y+TD*3/5-2*y_eps);//5
        LineTo(hDC, st_x, st_y);//1
        break;
    case 1:
        MoveToEx(hDC, st_x+LR*4/18-2*x_eps, st_y, NULL);//2
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);//6
        break;
    case 2:
        MoveToEx(hDC, st_x, st_y, NULL);//1
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y);//2
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps);//4
        LineTo(hDC, st_x, st_y+TD*3/5/2-y_eps);//3
        LineTo(hDC, st_x, st_y+TD*3/5-2*y_eps);//5
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);//6
        break;
    case 3:
        MoveToEx(hDC, st_x, st_y, NULL);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps);
        LineTo(hDC, st_x, st_y+TD*3/5/2-y_eps);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);
        LineTo(hDC, st_x, st_y+TD*3/5-2*y_eps);
        break;
    case 4:
        MoveToEx(hDC, st_x, st_y, NULL);
        LineTo(hDC, st_x, st_y+TD*3/5/2-y_eps);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps);
        MoveToEx(hDC, st_x+LR*4/18-2*x_eps, st_y, NULL);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);
        break;
    case 5:
        MoveToEx(hDC, st_x+LR*4/18-2*x_eps, st_y, NULL);
        LineTo(hDC, st_x, st_y);
        LineTo(hDC, st_x, st_y+TD*3/5/2-y_eps);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);
        LineTo(hDC, st_x, st_y+TD*3/5-2*y_eps);
        break;
    case 6:
        MoveToEx(hDC, st_x+LR*4/18-2*x_eps, st_y, NULL);
        LineTo(hDC, st_x, st_y);
        LineTo(hDC, st_x, st_y+TD*3/5-2*y_eps);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps);
        LineTo(hDC, st_x, st_y+TD*3/5/2-y_eps);
        break;
    case 7:
        MoveToEx(hDC, st_x, st_y, NULL);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y);
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);
        break;
    case 8:
        MoveToEx(hDC, st_x, st_y, NULL);//1
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y);//2
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);//6
        LineTo(hDC, st_x, st_y+TD*3/5-2*y_eps);//5
        LineTo(hDC, st_x, st_y);//1
        MoveToEx(hDC, st_x, st_y+TD*3/5/2-y_eps, NULL);//3
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps);//4
        break;
    case 9:
        MoveToEx(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5/2-y_eps, NULL);//4
        LineTo(hDC, st_x, st_y+TD*3/5/2-y_eps);//3
        LineTo(hDC, st_x, st_y);//1
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y);//2
        LineTo(hDC, st_x+LR*4/18-2*x_eps, st_y+TD*3/5-2*y_eps);//6
        break;
    };

}


//窗口函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hDC;
    HBRUSH hBrush;
    RECT rect;
    HPEN hPen;
    PAINTSTRUCT PtStr;
    static int m = 0, s = 0;
    COLORREF color[7] = {RGB(255,0,0), RGB(255,165,0), RGB(255,255,0), RGB(0,255,0), RGB(0,255,255), RGB(0,0,255), RGB(139,0,255)};
    switch(message)
    {
    case WM_PAINT:
        GetClientRect(hWnd, &rect);
        hDC = BeginPaint(hWnd, &PtStr);
        hPen = CreatePen(PS_SOLID, 10, color[6]);
        SelectObject(hDC, hPen);
        Rectangle(hDC, LR/18, TD/5, LR*17/18, TD*4/5);
        Rectangle(hDC, LR/18, TD/5, LR*9/18, TD*4/5);
        CreateNumbers(hDC, rect, m / 10, 1);
        CreateNumbers(hDC, rect, m % 10, 2);
        CreateNumbers(hDC, rect, s / 10, 3);
        CreateNumbers(hDC, rect, s % 10, 4);

        Sleep(1000);
        ++s;
        if (s == 60)
            s = 0, ++m;
        InvalidateRect(hWnd, NULL, 1);


        DeleteObject(hBrush);
        EndPaint(hWnd, &PtStr);

        return 0;

    case WM_DESTROY:
        PostQuitMessage (0 );                  // call PostQuitMessage WM_QUIT message sent 
        return  0 ;
     default :
         return the DefWindowProc (the hWnd, Message, the wParam, the lParam); // use the system default message handler default 

    } 

}

 

Guess you like

Origin www.cnblogs.com/Vikyanite/p/12453841.html