home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / SETTIME.ZIP / SETTIME.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-04  |  17.6 KB  |  557 lines

  1. //
  2. //    SetTime by pinter@p-squared.com
  3. //
  4.  
  5. #include    <afxwin.h>
  6. #include    <afxext.h>
  7. #include    <afxpriv.h>
  8. #include    <time.h>
  9. #include    "resource.h"
  10. #include    "CWinSock.h"
  11.  
  12. #define        IDB_QUERY            (WM_USER+1)
  13. #define        IDB_SETCLOCK        (WM_USER+2)
  14. #define        IDT_TIMER1            (WM_USER+3)
  15. #define        WM_USER_STREAM        (WM_USER+4)
  16.  
  17. #define        TIMER_VALUE            (1000)                        // Number of milliseconds per frame
  18. #define        FRAME_WIDTH            (500)
  19. #define        FRAME_HEIGHT        (300)
  20.  
  21. #define        BackGroundColour    (RGB(  0,  0,0))
  22. #define        ForeGroundColour    (RGB(255,255,0))
  23.  
  24. class        CSetTimeApp : public CWinApp                    // Declare the application class
  25.             {
  26.             public:
  27.                 virtual    BOOL    InitInstance ();            // Application class Constructor
  28.             };
  29.  
  30. CSetTimeApp    SetTimeApp;                                        // Instantiate SetTimeApp
  31.  
  32. class        CSetTimeWindow : public CFrameWnd                // Declare the main window class
  33.             {
  34.             private:
  35.                 char            m_pszBuffer[100];            // Communications buffer
  36.                 char            m_pszServer[100];            // NTP Server name or address
  37.                 BOOL            TCPmode;                    // TCP (Stream) Mode flag
  38.                 BOOL            Primary;                    // Primary .vs. Backup Timebase
  39.                 BOOL            UpdateClock;                // Update System Clock flag
  40.                 CDC                memDC;                        // Memory based Device Context
  41.                 CTime            Current_time;                // Stores new time value
  42.                 CRect            rect;                        // Rectangle structure
  43.                 CFont            *font;                        // Font pointer
  44.                 CMenu            *menu,*popupmenu,*tempmenu;    // Menu structures
  45.                 CBitmap            *b1, *b2;                    // Button Bitmap structures
  46.                 CBitmap            *newMemBitmap;                // Memory DC Bitmap
  47.                 CButton            *button1, *button2;            // Buttons for Query & Timeset
  48.                 CWinSock        *m_pWinSock;                // WinSock structure
  49.                 CStatusBar        m_wndStatusBar;                // Status Bar
  50.                 CStreamSocket    *m_pStream;                    // Stream socket
  51.  
  52.             public:
  53.                                 CSetTimeWindow        ();        // Window class constructor
  54.                 virtual            ~CSetTimeWindow        ();        // Window class destructor
  55.                 afx_msg    void    QueryTimebase        ();
  56.                 afx_msg    void    OnSize                (UINT, int, int);
  57.                 afx_msg void    OnTimer                (UINT);
  58.                 afx_msg    void    OnPaint                ();
  59.                 afx_msg    void    OnExit                ();
  60.                 afx_msg    void    OnAbout                ();
  61.                 afx_msg    void    OnTCPmode            ();
  62.                 afx_msg    void    OnRButtonDown        (UINT, CPoint);
  63.                 afx_msg    void    OnTimeBase1            ();
  64.                 afx_msg    void    OnTimeBase2            ();
  65.                 afx_msg    void    OnUpdateSystemClock    ();
  66.                 afx_msg    void    OnUpdateStatusBar    (CCmdUI *pCmdUI);
  67.                 afx_msg    void    OnUpdateTCP            (CCmdUI *pCmdUI);
  68.                 afx_msg    void    OnUpdateUDP            (CCmdUI *pCmdUI);
  69.                 afx_msg    void    OnUpdateTICK        (CCmdUI *pCmdUI);
  70.                 afx_msg    void    OnUpdateTOCK        (CCmdUI *pCmdUI);
  71.                 afx_msg int        OnCreate            (LPCREATESTRUCT lpCreateStruct);
  72.                 afx_msg    LONG    OnWinSockEvent        (WPARAM wParam, LPARAM lParam);
  73.                         void    DrawFace            (CDC &dc);
  74.  
  75.             DECLARE_MESSAGE_MAP    ()
  76.  
  77.             };
  78.  
  79. BEGIN_MESSAGE_MAP            (CSetTimeWindow, CFrameWnd)
  80.     ON_WM_CREATE            ()
  81.     ON_UPDATE_COMMAND_UI    (ID_VIEW_STATUS_BAR,OnUpdateStatusBar)
  82.     ON_UPDATE_COMMAND_UI    (ID_INDICATOR_TCP,    OnUpdateTCP)
  83.     ON_UPDATE_COMMAND_UI    (ID_INDICATOR_UDP,    OnUpdateUDP)
  84.     ON_UPDATE_COMMAND_UI    (ID_INDICATOR_TICK,    OnUpdateTICK)
  85.     ON_UPDATE_COMMAND_UI    (ID_INDICATOR_TOCK,    OnUpdateTOCK)
  86.     ON_WM_SIZE                ()
  87.     ON_WM_TIMER                ()
  88.     ON_BN_CLICKED            (IDB_QUERY,            QueryTimebase)
  89.     ON_BN_CLICKED            (IDB_SETCLOCK,        OnUpdateSystemClock)
  90.     ON_COMMAND                (ID_APP_EXIT,        OnExit)
  91.     ON_COMMAND                (ID_APP_ABOUT,        OnAbout)
  92.     ON_COMMAND                (IDM_TCP_MODE,        OnTCPmode)
  93.     ON_WM_RBUTTONDOWN        ()
  94.     ON_COMMAND                (IDM_TIMEBASE1,        OnTimeBase1)
  95.     ON_COMMAND                (IDM_TIMEBASE2,        OnTimeBase2)
  96.     ON_MESSAGE                (WM_USER_STREAM,    OnWinSockEvent)
  97. END_MESSAGE_MAP                ()
  98.  
  99. static        UINT indicators[] =                                // Array of Status Bar indicators
  100.                             {
  101.                             ID_SEPARATOR,
  102.                             ID_INDICATOR_TCP,
  103.                             ID_INDICATOR_UDP,
  104.                             ID_INDICATOR_TICK,
  105.                             ID_INDICATOR_TOCK
  106.                             };
  107.  
  108. BOOL        CSetTimeApp::InitInstance ()                    // InitInstance called when
  109.             {                                                // the application first executes
  110.             m_pMainWnd    = new CSetTimeWindow ();
  111.             m_pMainWnd->ShowWindow (m_nCmdShow);
  112.             m_pMainWnd->UpdateWindow ();
  113.  
  114.             SetDialogBkColor();
  115.  
  116.             return        TRUE;
  117.             }
  118.  
  119. CSetTimeWindow::CSetTimeWindow() :    TCPmode        (TRUE),        // The window class constructor
  120.                                     Primary        (TRUE),
  121.                                     UpdateClock    (FALSE)
  122.             {
  123.             CBrush        BackGroundBrush (BackGroundColour);
  124.  
  125.             const char    *pszWindowClass = AfxRegisterWndClass  (CS_HREDRAW|
  126.                                                                 CS_VREDRAW|
  127.                                                                 CS_BYTEALIGNCLIENT|
  128.                                                                 CS_BYTEALIGNWINDOW|
  129.                                                                 CS_SAVEBITS,
  130.                                                                 NULL,
  131.                                                                 BackGroundBrush,
  132.                                                                 AfxGetApp()->LoadIcon (IDI_ICON1));
  133.     
  134.             Create        (pszWindowClass,                    // Create the window itself
  135.                         "Network Time Protocol Client",
  136.                         WS_OVERLAPPEDWINDOW,
  137.                         CRect (0, 0, FRAME_WIDTH, FRAME_HEIGHT),
  138.                         NULL,
  139.                         MAKEINTRESOURCE(IDR_MENU1));        // *need* to pre-load the menu
  140.  
  141.             CenterWindow (GetDesktopWindow());
  142.             
  143.             menu        = new CMenu();
  144.             menu->        LoadMenu (MAKEINTRESOURCE(IDR_MENU1));
  145.  
  146.             popupmenu    = new CMenu();
  147.             popupmenu->    LoadMenu (MAKEINTRESOURCE(IDR_MENU2));
  148.  
  149.             b1            = new CBitmap(); b1->LoadBitmap (MAKEINTRESOURCE(IDB_BITMAP1));
  150.             b2            = new CBitmap(); b2->LoadBitmap (MAKEINTRESOURCE(IDB_BITMAP2));
  151.  
  152.             menu->        ModifyMenu (IDM_TIMEBASE1,    MF_BYCOMMAND, IDM_TIMEBASE1, b1);
  153.             menu->        ModifyMenu (IDM_TIMEBASE2,    MF_BYCOMMAND, IDM_TIMEBASE2, b2);
  154.  
  155.             SetMenu        (menu);
  156.  
  157.             DrawMenuBar    ();
  158.  
  159.             ShowControlBar (&m_wndStatusBar, TRUE, FALSE);
  160.  
  161.             LoadAccelTable (MAKEINTRESOURCE(IDR_ACCELERATOR1));
  162.  
  163.             GetClientRect (rect);                            // Get rectangle dimensions
  164.  
  165.             button1        = new CButton();                    // Create a Static Button
  166.             button1->    Create ("Query Navy Timebase",
  167.                                 WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
  168.                                 CRect (rect.left, rect.top, rect.right, rect.top+20),
  169.                                 this,
  170.                                 IDB_QUERY);
  171.  
  172.             button2=    new CButton();                        // Create a Static Button
  173.             button2->    Create ("Correct System Clock",
  174.                                 WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
  175.                                 CRect (rect.left, rect.bottom-40, rect.right, rect.bottom-20),
  176.                                 this,
  177.                                 IDB_SETCLOCK);
  178.  
  179.             CClientDC    dc (this);                            // Get a Client area DC
  180.  
  181.             memDC.CreateCompatibleDC (&dc);                    // Create a clientarea compatible DC
  182.  
  183.             newMemBitmap            = new CBitmap;            // Instantiate a new Bitmap
  184.             newMemBitmap->            CreateCompatibleBitmap (&dc, rect.Width(), rect.Height());
  185.             CBitmap *pOldMemBitmap    = memDC.SelectObject (newMemBitmap);
  186.             memDC.PatBlt            (0,0, FRAME_WIDTH, FRAME_HEIGHT, BLACKNESS);
  187.             memDC.SelectObject        (pOldMemBitmap);        // Prevents memory leaks
  188.  
  189.             m_pWinSock            = NULL;                        // Reset Winsock pointer
  190.             m_pStream            = NULL;                        // Reset Stream pointer
  191.             (*m_pszBuffer)        = '\0';                        // Reset communication buffer
  192.  
  193.             m_pWinSock    = new CWinSock;                        // Instantiate a new Winsock object
  194.  
  195.             if            (m_pWinSock->Startup() == CWINSOCK_NOERROR)
  196.                         m_wndStatusBar.SetPaneText (0, "WinSock Initialized", TRUE);
  197.             else
  198.                         {
  199.                         m_wndStatusBar.SetPaneText (0, "WinSock Initialization FAILED", TRUE);
  200.                         delete        m_pWinSock;
  201.                         m_pWinSock    = NULL;
  202.                         return;
  203.                         }
  204.  
  205.              lstrcpy        (m_pszServer, "tick.usno.navy.mil");// Preset default timebase
  206.  
  207.             SetTimer    (IDT_TIMER1, TIMER_VALUE, NULL);    // Set timer for frame refresh
  208.  
  209.             Current_time= CTime::GetCurrentTime();            // Preload current time field
  210.  
  211.             button2->    EnableWindow(FALSE);                // Disable updates till query done
  212.             
  213.             font        = new CFont;                        // Create a new font object
  214.  
  215.             }
  216.  
  217. CSetTimeWindow::~CSetTimeWindow()
  218.             
  219.             {
  220.             if            (m_pStream)
  221.                         {
  222.                         m_pStream->    DestroySocket();
  223.                         delete        m_pStream;
  224.                         m_pStream    = NULL;
  225.                         }
  226.             
  227.             if            (m_pWinSock)
  228.                         {
  229.                         m_pWinSock->Shutdown();
  230.                         delete        m_pWinSock;
  231.                         m_pWinSock    = NULL;
  232.                         }
  233.             
  234.             delete            button1;                        // prevent memory leaks...
  235.             delete            button2;
  236.             b1->            DeleteObject(); delete b1;
  237.             b2->            DeleteObject(); delete b2;
  238.             delete            menu;
  239.             delete            popupmenu;
  240.             font->            DeleteObject(); delete font;
  241.             newMemBitmap->    DeleteObject(); delete newMemBitmap;
  242.  
  243.             }
  244.  
  245. int            CSetTimeWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
  246.             {
  247.  
  248.             if            (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;
  249.  
  250.             if            (!m_wndStatusBar.Create(this) ||
  251.                          !m_wndStatusBar.SetIndicators (indicators, sizeof(indicators)/sizeof (UINT)))
  252.                         {
  253.                         TRACE0("Failed to create status bar\n");
  254.                         return -1;
  255.                         }
  256.  
  257.             m_wndStatusBar.SetPaneInfo (0, m_wndStatusBar.GetItemID (0), SBPS_STRETCH, NULL);
  258.             return 0;
  259.             }
  260.  
  261. void        CSetTimeWindow::QueryTimebase()
  262.             {
  263.             m_wndStatusBar.SetPaneText (0, "Connecting to Internet...", TRUE);
  264.             m_wndStatusBar.SendMessage (WM_IDLEUPDATECMDUI);// Force an update to the Status Bar
  265.             m_wndStatusBar.UpdateWindow();
  266.  
  267.             button1->    EnableWindow(FALSE);
  268.  
  269.             m_pStream    = new CStreamSocket (this, WM_USER_STREAM);
  270.  
  271.             if            (m_pStream->CreateSocket() == CWINSOCK_NOERROR)
  272.                         {
  273.                         m_wndStatusBar.SetPaneText (0, "Stream Created. Waiting to Connect...", TRUE);
  274.                         }
  275.             else        {
  276.                         m_wndStatusBar.SetPaneText (0, "Stream Create FAILED. Verify Internet Link.", TRUE);
  277.                         delete        m_pStream;
  278.                         m_pStream    = NULL;
  279.                         button1->    EnableWindow(TRUE);
  280.                         }
  281.             
  282.             if            (m_pStream->Connect(m_pszServer, 37) == CWINSOCK_NOERROR)
  283.                         {
  284.                         m_wndStatusBar.SetPaneText (0, "Connected; waiting for Correct Time...", TRUE);
  285.                         }
  286.             else
  287.                         {
  288.                         m_wndStatusBar.SetPaneText (0, "Connect FAILED. Verify Internet Link.", TRUE);
  289.                         delete        m_pStream;
  290.                         m_pStream    = NULL;
  291.                         button1->    EnableWindow(TRUE);
  292.                         }
  293.             }
  294.  
  295. void        CSetTimeWindow::OnUpdateSystemClock()
  296.             {
  297.             button2->    EnableWindow(FALSE);
  298.             UpdateClock    = TRUE;
  299.             QueryTimebase();
  300.             }
  301.  
  302. void        MakeFont    (CFont *font, int x)
  303.             {
  304.             font->        DeleteObject();
  305.             font->        CreateFont    (x, 0, 0, 0, FW_THIN, 0, 0, 0,
  306.                                     ANSI_CHARSET, OUT_DEFAULT_PRECIS,
  307.                                     CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  308.                                     DEFAULT_PITCH | TMPF_TRUETYPE, NULL);
  309.             }
  310.  
  311. void        CSetTimeWindow::OnSize (UINT nType, int cx, int cy)
  312.             {
  313.             int            x = 2;
  314.             CRect        r;
  315.             CClientDC    dc (this);
  316.             
  317.             CStatusBar    *pStatusBar = (CStatusBar *) AfxGetApp()->m_pMainWnd->GetDescendantWindow (ID_VIEW_STATUS_BAR);
  318.  
  319.             GetClientRect        (&rect);
  320.  
  321.             button1->            MoveWindow (CRect (rect.left, rect.top,    rect.right, rect.top+20));
  322.             button2->            MoveWindow (CRect (rect.left, rect.bottom-40, rect.right, rect.bottom-20));
  323.             rect.InflateRect    (0, -20, 0, -40);            // Account for buttons in client area
  324.  
  325.             ShowControlBar        (pStatusBar, TRUE, FALSE);    // Show Status Bar in new location
  326.             
  327.             do
  328.                 {
  329.                 x            += 10;                        // try creating a font at size x
  330.                 r            = rect;                        // calc size of string with that font
  331.  
  332.                 MakeFont        (font, x);                // try a font on for size
  333.  
  334.                 CFont *pOldFont    = memDC.SelectObject (font);// select the font onto DC
  335.                 
  336.                 memDC.DrawText    (Current_time.Format ("%X"),
  337.                                     -1,
  338.                                     r,
  339.                                     DT_CALCRECT | DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  340.  
  341.                 memDC.SelectObject (pOldFont);            // prevent memory leaks
  342.  
  343.                 } while            (rect == (r | rect));    // halt when string overflows the rect
  344.  
  345.             MakeFont            (font, x-10);            // Adjust final fontsize for 'drawface'
  346.  
  347.             GetClientRect        (&rect);
  348.             
  349.             newMemBitmap->        DeleteObject();
  350.             newMemBitmap->        CreateCompatibleBitmap (&dc, rect.Width(), rect.Height());
  351.             }
  352.  
  353. void        CSetTimeWindow::OnTimer (UINT id)
  354.             {
  355.             CClientDC    dc (this);
  356.             DrawFace    (dc);
  357.             }
  358.  
  359. void        CSetTimeWindow::OnPaint    ()
  360.             {
  361.             CPaintDC    dc (this);
  362.             DrawFace    (dc);
  363.             }
  364.  
  365. void        CSetTimeWindow::OnExit()
  366.             {
  367.             DestroyWindow();
  368.             }
  369.  
  370. void        CSetTimeWindow::OnAbout()
  371.             {
  372.             MessageBox    ("Bearware by Pete Pinter (pinter@p-squared.com)", "About SetTime");
  373.             }
  374.  
  375. void        CSetTimeWindow::OnTCPmode()
  376.             {
  377. /********    if    (TCPmode)
  378.                 {
  379.                 menu->        CheckMenuItem (IDM_TCP_MODE,    MF_BYCOMMAND|MF_UNCHECKED);
  380.                 popupmenu->    CheckMenuItem (IDM_TCP_MODE,    MF_BYCOMMAND|MF_UNCHECKED);
  381.                 menu->        CheckMenuItem (IDM_UDP_MODE,    MF_BYCOMMAND|MF_CHECKED);
  382.                 popupmenu->    CheckMenuItem (IDM_UDP_MODE,    MF_BYCOMMAND|MF_CHECKED);
  383.                 }
  384.             else
  385.                 {
  386.                 menu->        CheckMenuItem (IDM_TCP_MODE,    MF_BYCOMMAND|MF_CHECKED);
  387.                 popupmenu->    CheckMenuItem (IDM_TCP_MODE,    MF_BYCOMMAND|MF_CHECKED);
  388.                 menu->        CheckMenuItem (IDM_UDP_MODE,    MF_BYCOMMAND|MF_UNCHECKED);
  389.                 popupmenu->    CheckMenuItem (IDM_UDP_MODE,    MF_BYCOMMAND|MF_UNCHECKED);
  390.                 }
  391.             TCPmode    =!    TCPmode;********/                    // toggle TCP mode flag
  392.             }
  393.  
  394. void        CSetTimeWindow::OnRButtonDown (UINT x, CPoint p)
  395.             {
  396.             ClientToScreen (&p);                            // convert client to screen coordinates
  397.             tempmenu    = popupmenu->GetSubMenu (0);
  398.             tempmenu->    TrackPopupMenu (TPM_CENTERALIGN|TPM_RIGHTBUTTON, p.x, p.y, this);
  399.             }
  400.  
  401. void        CSetTimeWindow::OnTimeBase1 ()
  402.             {
  403.             Primary        = TRUE;
  404.             lstrcpy        (m_pszServer, "tick.usno.navy.mil");
  405.             m_wndStatusBar.SetPaneText (0, "tick.usno.navy.mil selected", TRUE);
  406.             }
  407.  
  408. void        CSetTimeWindow::OnTimeBase2 ()
  409.             {
  410.             Primary        = FALSE;
  411.             lstrcpy        (m_pszServer, "tock.usno.navy.mil");
  412.             m_wndStatusBar.SetPaneText (0, "tock.usno.navy.mil selected", TRUE);
  413.             }
  414.  
  415. void        CSetTimeWindow::OnUpdateStatusBar (CCmdUI *pCmdUI)
  416.             {
  417.             pCmdUI->Enable (TRUE);
  418.             }
  419.  
  420. void        CSetTimeWindow::OnUpdateTCP (CCmdUI *pCmdUI)
  421.             {
  422.             if    (TCPmode)     pCmdUI->Enable (TRUE);
  423.             else             pCmdUI->Enable (FALSE);
  424.             }
  425.  
  426. void        CSetTimeWindow::OnUpdateUDP (CCmdUI *pCmdUI)
  427.             {
  428.             if    (TCPmode)     pCmdUI->Enable (FALSE);
  429.             else             pCmdUI->Enable (TRUE);
  430.             }
  431.  
  432. void        CSetTimeWindow::OnUpdateTICK (CCmdUI *pCmdUI)
  433.             {
  434.             if    (Primary)     pCmdUI->Enable (TRUE);
  435.             else             pCmdUI->Enable (FALSE);
  436.             }
  437.  
  438. void        CSetTimeWindow::OnUpdateTOCK (CCmdUI *pCmdUI)
  439.             {
  440.             if    (!Primary)     pCmdUI->Enable (TRUE);
  441.             else             pCmdUI->Enable (FALSE);
  442.             }
  443.  
  444. LONG        CSetTimeWindow::OnWinSockEvent (WPARAM wParam, LPARAM lParam)
  445.             {
  446.             int            nLength;                            // Length of Data read/written
  447.             char        pszMessage[100];                    // StatusBar text buffer
  448.             void        *pDataWritten;                        // Pointer to data sent
  449.             time_t        *pDataRead;                            // Pointer to time value read
  450.             time_t        Navy_time;                            // Time value from Navy
  451.             CString        Delta;                                // Contains delta time as a string
  452.             CTimeSpan    Span;                                // Holds delta time span of clocks
  453.             SYSTEMTIME    Timecell;                            // Holds System specific time value
  454.  
  455.             switch        (wParam)
  456.                         {
  457.                         case    CWINSOCK_DONE_WRITING:        // lParam points to sent data
  458.                                 pDataWritten = (LPVOID) lParam;
  459.                                 wsprintf    (pszMessage, "OnWinSockEvent: Data Sent (%s)", pDataWritten);
  460.                                 m_wndStatusBar.SetPaneText (0, pszMessage, TRUE);
  461.                                 (*m_pszBuffer)    = '\0';        // same as (*pDataWritten) = '\0'
  462.                                 break;
  463.  
  464.                         case    CWINSOCK_ERROR_WRITING:        // lParam points to failed sent data
  465.                                 pDataWritten = (LPVOID) lParam;
  466.                                 wsprintf    (pszMessage, "OnWinSockEvent: Error Sending Data: (%s)", pDataWritten);
  467.                                 m_wndStatusBar.SetPaneText (0, pszMessage, TRUE);
  468.                                 (*m_pszBuffer) = '\0';        // same as (*pDataWritten) = '\0'
  469.                                 break;
  470.  
  471.                         case    CWINSOCK_DONE_READING:        // lParam = number of data chunks in queue
  472.                                 
  473.                                 pDataRead    = (time_t *) m_pStream->Read (&nLength);
  474.                                 
  475.                                 if    (pDataRead == NULL)
  476.                                     {
  477.                                     m_wndStatusBar.SetPaneText (0, "Selected Timebase returned NULL data!", TRUE);
  478.                                     free (pDataRead);
  479.                                     break;
  480.                                     }
  481.  
  482.                                 Navy_time    = ntohl (*pDataRead) - 2208988800;    // Offset to Jan 1, 1970
  483.  
  484.                                 Current_time= Navy_time;                        // Store as a CTime object
  485.  
  486.                                 if    (UpdateClock)
  487.                                     {
  488.                                     UpdateClock                = FALSE;            // reset clock update flag
  489.                                     Timecell.wYear            = Current_time.GetYear();
  490.                                     Timecell.wMonth            = Current_time.GetMonth();
  491.                                     Timecell.wDay            = Current_time.GetDay();
  492.                                     Timecell.wHour            = Current_time.GetHour();
  493.                                     Timecell.wMinute           = Current_time.GetMinute();
  494.                                     Timecell.wSecond        = Current_time.GetSecond();
  495.                                     Timecell.wMilliseconds    = TIMER_VALUE/2;    // fake transit time
  496.                                     SetLocalTime            (&Timecell);        // update system clock
  497.                                     }
  498.  
  499.                                 Span    = CTime::GetCurrentTime() - Current_time;
  500.                                 Delta    = Current_time.Format("Timebase Clock: %c. ") + Span.Format("Delta: %D days %H:%M:%S");
  501.  
  502.                                 m_wndStatusBar.SetPaneText (0, Delta, TRUE);
  503.  
  504.                                 button1->    EnableWindow(TRUE);
  505.                                 button2->    EnableWindow(TRUE);
  506.  
  507.                                  free        (pDataRead);
  508.                                 break;
  509.                                                              
  510.                         case    CWINSOCK_ERROR_READING:
  511.                                 break;
  512.  
  513.                         case    CWINSOCK_YOU_ARE_CONNECTED:
  514.                                 break;
  515.  
  516.                         case    CWINSOCK_LOST_CONNECTION:    // Server closed the connection
  517.                                 if    (m_pStream)
  518.                                     {
  519.                                     m_pStream->    DestroySocket();
  520.                                     delete        m_pStream;
  521.                                     m_pStream    = NULL;
  522.                                     }
  523.                                 break;
  524.  
  525.                         default:
  526.                                 break;
  527.                         }
  528.             return 0;
  529.             }
  530.  
  531. void        CSetTimeWindow::DrawFace    (CDC &dc)
  532.             {
  533.             Current_time        = CTime::GetCurrentTime();        // Get current system time
  534.  
  535.             GetClientRect        (&rect);                        // Get current client area size
  536.  
  537.             CFont    *pOldFont    = memDC.SelectObject (font);    // Load calculated font into DC
  538.             CBitmap *pOldBitMap    = memDC.SelectObject (newMemBitmap);
  539.  
  540.             memDC.FillSolidRect    (rect, BackGroundColour);        // Fill with background colour
  541.             memDC.SetTextColor    (ForeGroundColour);                // Set foreground text colour
  542.             memDC.SetBkColor    (BackGroundColour);                // Set background text colour
  543.  
  544.             rect.InflateRect    (0, -20, 0, -40);                // Adjust for both buttons
  545.  
  546.             memDC.DrawText        (Current_time.Format ("%X"),    // Write current time to screen
  547.                                 -1,
  548.                                 rect,
  549.                                 DT_SINGLELINE |DT_CENTER | DT_VCENTER);
  550.  
  551.             dc.BitBlt            (0, 20, rect.Width(), rect.Height(),
  552.                                 &memDC, 0, 20, SRCCOPY);        // Blast memory DC to screen
  553.  
  554.             memDC.SelectObject    (pOldFont);                        // Prevent memory leaks
  555.             memDC.SelectObject    (pOldBitMap);
  556.             }
  557.