home *** CD-ROM | disk | FTP | other *** search
/ Computer Buyer 1998 October / dpcb1098.iso / Business / Maxim / MAX5 / data.z / DTCtrl.cpp < prev    next >
C/C++ Source or Header  |  1998-05-15  |  5KB  |  174 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. // NAME.......: DTCtrl.CPP                                               
  3. // PURPOSE....: new time windows common control, 
  4. //              need Comctl32.lib and Commctrl.h
  5. //
  6. // WRITTEN....: 96/09/27 by Darko Juvan
  7. //
  8. // This code and information is provided "as is" without warranty of any
  9. // kind, either expressed or implied, including but not limited to the
  10. // implied warranties of merchantability and/or fitness for a particular
  11. // purpose..
  12. //
  13. // Copyright (c) 1998  Multiactive Software Inc.  All Rights Reserved.
  14. //
  15. //////////////////////////////////////////////////////////////////////////////
  16.  
  17. #include "stdafx.h"
  18. #include "resource.h"
  19. #include "commctrl.h"
  20. #include "DTCtrl.h"
  21.  
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CDTCtrl
  30.  
  31. //////////////////////////////////////////////////////////////////////////////
  32. //                        
  33. // FUNCTION...: CDTCtrl()
  34. //                                                  
  35. // DESCRIPTION: constructor
  36. //
  37. //////////////////////////////////////////////////////////////////////////////
  38. CDTCtrl::CDTCtrl()
  39. {
  40. }
  41.  
  42. //////////////////////////////////////////////////////////////////////////////
  43. //                        
  44. // FUNCTION...: ~CDTCtrl()
  45. //                                                  
  46. // DESCRIPTION: destructor
  47. //
  48. //////////////////////////////////////////////////////////////////////////////
  49. CDTCtrl::~CDTCtrl()
  50. {
  51. }
  52.  
  53.  
  54. BEGIN_MESSAGE_MAP(CDTCtrl, CWnd)
  55.     //{{AFX_MSG_MAP(CDTCtrl)
  56.         // NOTE - the ClassWizard will add and remove mapping macros here.
  57.     //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CDTCtrl message handlers
  63.  
  64. //////////////////////////////////////////////////////////////////////////////
  65. //                        
  66. // FUNCTION...: Create()
  67. //                                                  
  68. // DESCRIPTION: create time common ctrl
  69. //
  70. //////////////////////////////////////////////////////////////////////////////
  71. BOOL CDTCtrl::Create(BOOL bDate, CWnd* pParentWnd, UINT nDlgID, UINT nCtrlID) 
  72. {
  73.     CRect rect;
  74.     pParentWnd->GetDlgItem(nDlgID)->GetWindowRect(rect);
  75.     pParentWnd->ScreenToClient(rect);
  76.     BOOL bRes = CWnd::Create(DATETIMEPICK_CLASS, 
  77.                         NULL, 
  78.                         WS_VISIBLE |
  79.                         WS_CHILD | 
  80.                         WS_TABSTOP |
  81.                         DTS_LONGDATEFORMAT |
  82.                         (bDate ? 0 : DTS_TIMEFORMAT), 
  83.                         rect, 
  84.                         pParentWnd, 
  85.                         nCtrlID );
  86.  
  87.     return bRes;
  88. }
  89.  
  90. //////////////////////////////////////////////////////////////////////////////
  91. //                        
  92. // FUNCTION...: GetTime()
  93. //                                                  
  94. // DESCRIPTION: return time in string format
  95. //
  96. //////////////////////////////////////////////////////////////////////////////
  97. CString CDTCtrl::GetTime(CTime* pTime)
  98. {
  99.     SYSTEMTIME sys_time;
  100.     SendMessage(DTM_GETSYSTEMTIME, 0, (LPARAM)(&sys_time));
  101.     CTime time(sys_time);
  102.     *pTime = time;
  103.  
  104.     CString t = time.Format("%I:%M %p");
  105.     if(t[0] == '0')
  106.         t.SetAt(0, ' ');
  107.  
  108.     return t;
  109. }
  110.  
  111. //////////////////////////////////////////////////////////////////////////////
  112. //                        
  113. // FUNCTION...:  GetDate()
  114. //                                                  
  115. // DESCRIPTION:  return date in string format
  116. //
  117. //////////////////////////////////////////////////////////////////////////////
  118. CString CDTCtrl::GetDate()
  119. {
  120.     SYSTEMTIME sys_time;
  121.  
  122.     SendMessage(DTM_GETSYSTEMTIME, 0, (LPARAM)(&sys_time));
  123.     CTime time(sys_time);
  124.  
  125.     return time.Format("%A, %B %d, %Y" );
  126. }
  127.  
  128. //////////////////////////////////////////////////////////////////////////////
  129. //                        
  130. // FUNCTION...: SetDate()
  131. //                                                  
  132. // DESCRIPTION: set date in ctrl
  133. //
  134. //////////////////////////////////////////////////////////////////////////////
  135. void CDTCtrl::SetDate(UINT year, UINT month, UINT day)
  136. {
  137.     SYSTEMTIME sys_time;
  138.     sys_time.wYear = year; 
  139.     sys_time.wMonth = month; 
  140.     sys_time.wDayOfWeek = 0; 
  141.     sys_time.wDay = day; 
  142.     sys_time.wHour = 0; 
  143.     sys_time.wMinute = 0; 
  144.     sys_time.wSecond = 0; 
  145.     sys_time.wMilliseconds = 0; 
  146.  
  147.     SendMessage(DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)(&sys_time));
  148.  
  149. }
  150.  
  151. //////////////////////////////////////////////////////////////////////////////
  152. //                        
  153. // FUNCTION...: SetTime()
  154. //                                                  
  155. // DESCRIPTION: set time in ctrl
  156. //
  157. //////////////////////////////////////////////////////////////////////////////
  158. void CDTCtrl::SetTime(UINT hour, UINT min, UINT sec)
  159. {
  160.     SYSTEMTIME sys_time;
  161.     sys_time.wYear = 1996; 
  162.     sys_time.wMonth = 11; 
  163.     sys_time.wDayOfWeek = 0; 
  164.     sys_time.wDay = 9; 
  165.     sys_time.wHour = hour; 
  166.     sys_time.wMinute = min; 
  167.     sys_time.wSecond = sec; 
  168.     sys_time.wMilliseconds = 10; 
  169.  
  170.     SendMessage(DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)(&sys_time));
  171.  
  172. }
  173.  
  174.