Deutsch

Часики тик-так 10.02.2024

304  
uscheswoi_82 коренной житель10.02.24 09:54
uscheswoi_82
10.02.24 09:54 

Будем сегодня программировать часы.

1. Запустим среду Dev-C++:



2. Создадим новый проект Datei->Neu:



3. Приложение должно быть Windows (Windows Application), а язык Си (C-Projekt):



4. Проект назавём uhr:



Вот так выглядет проект с кодом:



Вот сам исходный код:

#include <windows.h>

void draw_vline(HDC hdc, int x, int y, int n, int h, COLORREF c) 
{
     int i=0;
     int m = n;
     HPEN hPen = CreatePen(PS_SOLID, 1, c);
     HPEN hOldPen = SelectObject(hdc, hPen); 
         
     for(i=0; i<n; i++) {
     MoveToEx(hdc, x+i, y+m, (LPPOINT)NULL);
     LineTo(hdc, x+i, (h-m)+y);
     m--;
     }


     m=0;
     for(i=0; i<n; i++) {
     MoveToEx(hdc, n+x+i, y+m, (LPPOINT)NULL);
     LineTo(hdc, n+x+i, (h-m)+y);
     m++;
     }
}


void draw_hline(HDC hdc, int x, int y, int n, int w, COLORREF c) 
{
     int i=0;
     int m = n;
     HPEN hPen = CreatePen(PS_SOLID, 1, c);
     HPEN hOldPen = SelectObject(hdc, hPen); 
     
     for(i=0; i<n; i++)
     {
        MoveToEx(hdc, x+m, y+i, (LPPOINT)NULL);
        LineTo(hdc, (x+w)-m, y+i);
        m--;
     }


     m=0;
     for(i=0; i<n; i++)
     {
        MoveToEx(hdc, x+m, y+i+n, (LPPOINT)NULL);
        LineTo(hdc, (x+w)-m, y+i+n);
        m++;
     }
}


void draw_number(HDC hdc, int number, int x, int y, COLORREF cn, COLORREF c)
{
    draw_hline(hdc, x+7, y+10, 5, 50, cn);
    draw_hline(hdc, x+7, y+70, 5, 50, cn);
    draw_hline(hdc, x+7, y+130, 5, 50, cn);
    
    draw_vline(hdc, x+3, y+20, 5, 50, cn);
    draw_vline(hdc, x+3, y+80, 5, 50, cn);


    draw_vline(hdc, x+53, y+20, 5, 50, cn);
    draw_vline(hdc, x+53, y+80, 5, 50, cn);
    
    if(number == 0)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_hline(hdc, x+7, y+130, 5, 50, c);    
    draw_vline(hdc, x+3, y+20, 5, 50, c);
    draw_vline(hdc, x+3, y+80, 5, 50, c);
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);
    }


    if(number == 1)
    {
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);
    }
    
    if(number == 2)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_hline(hdc, x+7, y+70, 5, 50, c);
    draw_hline(hdc, x+7, y+130, 5, 50, c);    
    draw_vline(hdc, x+3, y+80, 5, 50, c);
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    }
    if(number == 3)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_hline(hdc, x+7, y+70, 5, 50, c);
    draw_hline(hdc, x+7, y+130, 5, 50, c);    
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);       
    }
    if(number == 4)
    {
    draw_hline(hdc, x+7, y+70, 5, 50, c);   
    draw_vline(hdc, x+3, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);
    }
    if(number == 5)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_hline(hdc, x+7, y+70, 5, 50, c);
    draw_hline(hdc, x+7, y+130, 5, 50, c);
    draw_vline(hdc, x+3, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);    
    }
    if(number == 6)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_hline(hdc, x+7, y+70, 5, 50, c);
    draw_hline(hdc, x+7, y+130, 5, 50, c);    
    draw_vline(hdc, x+3, y+20, 5, 50, c);
    draw_vline(hdc, x+3, y+80, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);
    }
    if(number == 7)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);
    }
    if(number == 8)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_hline(hdc, x+7, y+70, 5, 50, c);
    draw_hline(hdc, x+7, y+130, 5, 50, c);
    draw_vline(hdc, x+3, y+20, 5, 50, c);
    draw_vline(hdc, x+3, y+80, 5, 50, c);
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);
    }


    if(number == 9)
    {
    draw_hline(hdc, x+7, y+10, 5, 50, c);
    draw_hline(hdc, x+7, y+70, 5, 50, c);
    draw_hline(hdc, x+7, y+130, 5, 50, c);
    draw_vline(hdc, x+3, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+20, 5, 50, c);
    draw_vline(hdc, x+53, y+80, 5, 50, c);
    }
}


int draw_number2(HDC hdc, int number, int x, int y, COLORREF cn, COLORREF c){
    int number2 = 0;
    int rest=0;
    if(number > 9 && number < 100)
    {
      number2 = number / 10;
      rest = number % 10;
      draw_number(hdc, rest, x+65, y, cn, c);
    }
    else
      draw_number(hdc, number, x+65, y, cn, c);


    draw_number(hdc, number2, x, y, cn, c);
}


/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    HDC hdc;
    PAINTSTRUCT ps; 
    SYSTEMTIME st, lt;


 switch(Message) {
        
        case WM_TIMER:
          InvalidateRect(hwnd, 0, TRUE);
        break;
        case WM_CREATE:
             SetTimer(hwnd, 100, 1000, (TIMERPROC)NULL);
        break;
        case WM_DESTROY:
         KillTimer(hwnd, 100);
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps); 
            GetLocalTime(&lt);
            draw_number2(hdc, lt.wHour, 6, 10, RGB(0, 0, 0), RGB(96, 222, 252));
            draw_number2(hdc, lt.wMinute, 160, 10, RGB(0, 0, 0), RGB(96, 222, 252));
            draw_number2(hdc, lt.wSecond, 310, 10, RGB(0, 0, 0), RGB(96, 222, 252));
            EndPaint(hwnd, &ps); 
            break;
                    
        /* All other messages (a lot of them) are processed using default procedures */
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
 }
 return 0;
}


/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
 WNDCLASSEX wc; /* A properties struct of our window */
 HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
 MSG msg; /* A temporary location for all messages */


 /* zero out the struct and set the stuff we want to modify */
 memset(&wc,0,sizeof(wc));
 wc.cbSize         = sizeof(WNDCLASSEX);
 wc.lpfnWndProc  = WndProc; /* This is where we will send messages to */
 wc.hInstance  = hInstance;
 wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
 
 /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wc.lpszClassName = "WindowClass";
 wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
 wc.hIconSm         = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */


 if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
 }


 hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Uhr",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        480, /* width */
        240, /* height */
        NULL,NULL,hInstance,NULL);


 if(hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
 }


 /*
        This is the heart of our program where all input is processed and 
        sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
        this loop will not produce unreasonably high CPU usage
 */
 while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
        TranslateMessage(&msg); /* Translate key codes to chars if present */
        DispatchMessage(&msg); /* Send it to WndProc */
 }
 return msg.wParam;
}

Вуаля! Результат работы наших часов:



кто как обзывается, тот так сам называется... маску ношу чтобы не заразить антиваксеров... Дневник тяжелобольного инвалида
#1 
uscheswoi_82 коренной житель11.02.24 16:09
uscheswoi_82
NEW 11.02.24 16:09 
в ответ uscheswoi_82 10.02.24 09:54, Последний раз изменено 11.02.24 17:12 (uscheswoi_82)

Пока болел ковидом в прошлом году осенью (если честно чуть не умер от ковида), то сделал часы на Visual Basic 4.0 для Windows 3.11, вот сам код:

Private Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, 
ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1

'Отображает текущую дату и время
Private Sub Refresh_Time()
  Me.Label1.Caption = Format(Now, "HH:mm:ss") 'Отобразить текущее время
  Me.Caption = "Часы " & Format(Now, "dd.mm.yyyy") 'Отобразить текущую дату
End Sub

Private Sub Form_Load()
  SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE 'Поверх всех окон
  Refresh_Time ' Запуск первый раз, чтобы отобразить текущую дату и текущее время
  Me.Timer1.Interval = 1000 'Таймер срабатывает 1 раз в 1 секунду
  Me.Timer1.Enabled = True 'Таймер активирован
End Sub

Private Sub Timer1_Timer()
  Refresh_Time 'чтобы отобразить текущую дату и текущее время
End Sub


Так выглядет форма сделаная в Visual Basic 4.0:



Результат работы таких часов в Windows 3.1:



кто как обзывается, тот так сам называется... маску ношу чтобы не заразить антиваксеров... Дневник тяжелобольного инвалида
#2