⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsk.cpp

📁 用C++Builder开发的时钟 用C++Builder开发的时钟
💻 CPP
字号:

#include <windows.h>
#pragma hdrstop
#include <condefs.h>
#include <time.h>

//---------------------------------------------------------------------------
#pragma argsused
#define ID_TIMER    1

#define YEAR  (datetime->tm_year%100)
#define MONTH (datetime->tm_mon  + 1)
#define MDAY  (datetime->tm_mday)
#define WDAY  (datetime->tm_wday)
#define HOUR  (datetime->tm_hour)
#define MIN   (datetime->tm_min)
#define SEC   (datetime->tm_sec)

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

char  sDate[2], sTime[2], sAMPM[2][5] ;
int   iDate, iTime ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
     {
     static char szAppName[] = "DesktopClock" ;
     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         = NULL;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     wndclass.hIconSm       = NULL ;

     RegisterClassEx (&wndclass) ;

     hwnd = CreateWindow (szAppName, szAppName,
                          WS_POPUP | WS_DLGFRAME | WS_SYSMENU,
                          10, 10, 20, 20,
                          NULL, NULL, hInstance, NULL) ;
 
     if (!SetTimer (hwnd, ID_TIMER, 1000, NULL))
          {
          MessageBox (hwnd, "Too many clocks or timers!", szAppName,
                      MB_ICONEXCLAMATION | MB_OK) ;
          return FALSE ;
          }

     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
     }

void SetInternational (void)
     {
     static char cName [] = "intl" ;

     iDate = GetProfileInt (cName, "iDate", 0) ;
     iTime = GetProfileInt (cName, "iTime", 0) ;

     GetProfileString (cName, "sDate",  "/", sDate,    2) ;
     GetProfileString (cName, "sTime",  ":", sTime,    2) ;
     GetProfileString (cName, "s1159", "AM", sAMPM[0], 5) ;
     GetProfileString (cName, "s2359", "PM", sAMPM[1], 5) ;
     }

void draw()
     {
     static char szWday[] = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat" ;
     char        cBuffer[40] ;
     int         iLength ;
     struct tm  *datetime ;
     time_t      lTime ;

     time (&lTime) ;
     datetime = localtime (&lTime) ;

     iLength=wsprintf (cBuffer, "  %s  %02d%s%02d%s%02d  ",
               (PSTR) szWday + 4 * WDAY,
               iDate == 1 ? MDAY  : iDate == 2 ? YEAR  : MONTH, (PSTR) sDate,
               iDate == 1 ? MONTH : iDate == 2 ? MONTH : MDAY,  (PSTR) sDate,
               iDate == 1 ? YEAR  : iDate == 2 ? MDAY  : YEAR) ;

     if (iTime == 1)
          iLength+=wsprintf (cBuffer + iLength, "  %02d%s%02d%s%02d  ",
                               HOUR, (PSTR) sTime, MIN, (PSTR) sTime, SEC) ;
     else
          iLength+=wsprintf (cBuffer + iLength, "  %d%s%02d%s%02d %s  ",
                               (HOUR % 12) ? (HOUR % 12) : 12,
                               (PSTR) sTime, MIN, (PSTR) sTime, SEC,
                               (PSTR) sAMPM [HOUR / 12]) ;
     HWND desk=GetDesktopWindow();
     HDC dskDC=GetWindowDC(desk);
     TextOut(dskDC,400,0,cBuffer,28);
     ReleaseDC(desk,dskDC);
     }

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     {

     switch (iMsg)
          {
          case WM_CREATE :
               SetInternational () ;
               return 0 ;

          case WM_TIMER :
               draw();
               return 0 ;

          case WM_DESTROY :
               KillTimer (hwnd, ID_TIMER) ;
               PostQuitMessage (0) ;
               return 0 ;
          }
     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -