home *** CD-ROM | disk | FTP | other *** search
/ Building OCXs / Building_OCXs_Que_1995.iso / code / ch10 / timerctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-21  |  9.0 KB  |  362 lines

  1. // timerctl.cpp : Implementation of the CTimerCtrl OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "timer.h"
  5. #include "timerctl.h"
  6. #include "timerppg.h"
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. const short ID_TIMER = 1;
  15.  
  16.  
  17. IMPLEMENT_DYNCREATE(CTimerCtrl, COleControl)
  18.  
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // Message map
  22.  
  23. BEGIN_MESSAGE_MAP(CTimerCtrl, COleControl)
  24.     //{{AFX_MSG_MAP(CTimerCtrl)
  25.     ON_WM_CREATE()
  26.     ON_WM_DESTROY()
  27.     ON_WM_ERASEBKGND()
  28.     ON_WM_TIMER()
  29.     //}}AFX_MSG_MAP
  30.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  31. END_MESSAGE_MAP()
  32.  
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // Dispatch map
  36.  
  37. BEGIN_DISPATCH_MAP(CTimerCtrl, COleControl)
  38.     //{{AFX_DISPATCH_MAP(CTimerCtrl)
  39.     DISP_PROPERTY_NOTIFY(CTimerCtrl, "Interval", m_interval, OnIntervalChanged, VT_I2)
  40.     DISP_STOCKPROP_ENABLED()
  41.     //}}AFX_DISPATCH_MAP
  42.     DISP_FUNCTION_ID(CTimerCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  43. END_DISPATCH_MAP()
  44.  
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // Event map
  48.  
  49. BEGIN_EVENT_MAP(CTimerCtrl, COleControl)
  50.     //{{AFX_EVENT_MAP(CTimerCtrl)
  51.     EVENT_CUSTOM("Timer", FireTimer, VTS_NONE)
  52.     //}}AFX_EVENT_MAP
  53. END_EVENT_MAP()
  54.  
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // Property pages
  58.  
  59. // TODO: Add more property pages as needed.  Remember to increase the count!
  60. BEGIN_PROPPAGEIDS(CTimerCtrl, 1)
  61.     PROPPAGEID(CTimerPropPage::guid)
  62. END_PROPPAGEIDS(CTimerCtrl)
  63.  
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // Initialize class factory and guid
  67.  
  68. IMPLEMENT_OLECREATE_EX(CTimerCtrl, "TIMER.TimerCtrl.1",
  69.     0x56e0b3b0, 0x1784, 0x11ce, 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5)
  70.  
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // Type library ID and version
  74.  
  75. IMPLEMENT_OLETYPELIB(CTimerCtrl, _tlid, _wVerMajor, _wVerMinor)
  76.  
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // Interface IDs
  80.  
  81. const IID BASED_CODE IID_DTimer =
  82.         { 0x56e0b3b1, 0x1784, 0x11ce, { 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5 } };
  83. const IID BASED_CODE IID_DTimerEvents =
  84.         { 0x56e0b3b2, 0x1784, 0x11ce, { 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5 } };
  85.  
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // Control type information
  89.  
  90. static const DWORD BASED_CODE _dwTimerOleMisc =
  91.     OLEMISC_INVISIBLEATRUNTIME |
  92.     OLEMISC_ACTIVATEWHENVISIBLE |
  93.     OLEMISC_SETCLIENTSITEFIRST |
  94.     OLEMISC_INSIDEOUT |
  95.     OLEMISC_CANTLINKINSIDE |
  96.     OLEMISC_RECOMPOSEONRESIZE;
  97.  
  98. IMPLEMENT_OLECTLTYPE(CTimerCtrl, IDS_TIMER, _dwTimerOleMisc)
  99.  
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CTimerCtrl::CTimerCtrlFactory::UpdateRegistry -
  103. // Adds or removes system registry entries for CTimerCtrl
  104.  
  105. BOOL CTimerCtrl::CTimerCtrlFactory::UpdateRegistry(BOOL bRegister)
  106. {
  107.     if (bRegister)
  108.         return AfxOleRegisterControlClass(
  109.             AfxGetInstanceHandle(),
  110.             m_clsid,
  111.             m_lpszProgID,
  112.             IDS_TIMER,
  113.             IDB_TIMER,
  114.             FALSE,                      //  Not insertable
  115.             _dwTimerOleMisc,
  116.             _tlid,
  117.             _wVerMajor,
  118.             _wVerMinor);
  119.     else
  120.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  121. }
  122.  
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // Licensing strings
  126.  
  127. static const TCHAR BASED_CODE _szLicFileName[] = _T("TIMER.LIC");
  128.  
  129. static const TCHAR BASED_CODE _szLicString[] =
  130.     _T("Copyright (c) 1994 Edward B. Toupin");
  131.  
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134. // CTimerCtrl::CTimerCtrlFactory::VerifyUserLicense -
  135. // Checks for existence of a user license
  136.  
  137. BOOL CTimerCtrl::CTimerCtrlFactory::VerifyUserLicense()
  138. {
  139.     return AfxVerifyLicFile(AfxGetInstanceHandle(), _szLicFileName,
  140.         _szLicString);
  141. }
  142.  
  143.  
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CTimerCtrl::CTimerCtrlFactory::GetLicenseKey -
  146. // Returns a runtime licensing key
  147.  
  148. BOOL CTimerCtrl::CTimerCtrlFactory::GetLicenseKey(DWORD dwReserved,
  149.     BSTR FAR* pbstrKey)
  150. {
  151.     if (pbstrKey == NULL)
  152.         return FALSE;
  153.  
  154.     *pbstrKey = SysAllocString(_szLicString);
  155.     return (*pbstrKey != NULL);
  156. }
  157.  
  158.  
  159. /////////////////////////////////////////////////////////////////////////////
  160. // CTimerCtrl::CTimerCtrl - Constructor
  161.  
  162. CTimerCtrl::CTimerCtrl()
  163. {
  164.     InitializeIIDs(&IID_DTimer, &IID_DTimerEvents);
  165.  
  166.     // TODO: Initialize your control's instance data here.
  167.     SetInitialSize(24, 22);
  168. }
  169.  
  170.  
  171. /////////////////////////////////////////////////////////////////////////////
  172. // CTimerCtrl::~CTimerCtrl - Destructor
  173.  
  174. CTimerCtrl::~CTimerCtrl()
  175. {
  176.     // TODO: Cleanup your control's instance data here.
  177. }
  178.  
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CTimerCtrl::OnDraw - Drawing function
  182.  
  183. void CTimerCtrl::OnDraw(
  184.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  185. {
  186.     // TODO: Replace the following code with your own drawing code.
  187.     CBitmap bitmap;
  188.     BITMAP  bmp;
  189.     CPictureHolder picHolder;
  190.     CRect rcSrcBounds;
  191.  
  192.     // Load clock bitmap
  193.     bitmap.LoadBitmap(IDB_CLOCK);
  194.     bitmap.GetObject(sizeof(BITMAP), &bmp);
  195.     rcSrcBounds.right = bmp.bmWidth;
  196.     rcSrcBounds.bottom = bmp.bmHeight;
  197.  
  198.     // Create picture and render
  199.     picHolder.CreateFromBitmap((HBITMAP)bitmap.m_hObject, NULL, FALSE);
  200.     picHolder.Render(pdc, rcBounds, rcSrcBounds);
  201. }
  202.  
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // CTimerCtrl::DoPropExchange - Persistence support
  206.  
  207. void CTimerCtrl::DoPropExchange(CPropExchange* pPX)
  208. {
  209.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  210.     COleControl::DoPropExchange(pPX);
  211.  
  212.     // TODO: Call PX_ functions for each persistent custom property.
  213.     PX_Short(pPX, _T("Interval"), m_interval, DEFAULT_INTERVAL);
  214. }
  215.  
  216.  
  217. /////////////////////////////////////////////////////////////////////////////
  218. // CTimerCtrl::OnResetState - Reset control to default state
  219.  
  220. void CTimerCtrl::OnResetState()
  221. {
  222.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  223.  
  224.     // TODO: Reset any other control state here.
  225. }
  226.  
  227.  
  228. /////////////////////////////////////////////////////////////////////////////
  229. // CTimerCtrl::AboutBox - Display an "About" box to the user
  230.  
  231. void CTimerCtrl::AboutBox()
  232. {
  233.     CDialog dlgAbout(IDD_ABOUTBOX_TIMER);
  234.     dlgAbout.DoModal();
  235. }
  236.  
  237.  
  238. /////////////////////////////////////////////////////////////////////////////
  239. // CTimeCtrl::OnSetClientSite - Force creation of window
  240.  
  241. void CTimerCtrl::OnSetClientSite()
  242. {
  243.     RecreateControlWindow();
  244. }
  245.  
  246. /////////////////////////////////////////////////////////////////////////////
  247. // CTimerCtrl message handlers
  248.  
  249. int CTimerCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  250. {
  251.     if (COleControl::OnCreate(lpCreateStruct) == -1)
  252.         return -1;
  253.     
  254.     // TODO: Add your specialized creation code here
  255.  
  256.     // Start timer if in user mode and if enabled
  257.     if (AmbientUserMode() && GetEnabled())
  258.         StartTimer();
  259.     
  260.     return 0;
  261. }
  262.  
  263. void CTimerCtrl::OnDestroy() 
  264. {
  265.     StopTimer();
  266.  
  267.     COleControl::OnDestroy();
  268.     
  269.     // TODO: Add your message handler code here
  270.     
  271. }
  272.  
  273. BOOL CTimerCtrl::OnEraseBkgnd(CDC* pDC) 
  274. {
  275.     // TODO: Add your message handler code here and/or call default
  276.     
  277.     return COleControl::OnEraseBkgnd(pDC);
  278. }
  279.  
  280. void CTimerCtrl::OnTimer(UINT nIDEvent) 
  281. {
  282.     // TODO: Add your message handler code here and/or call default
  283.     FireTimer();    
  284.     COleControl::OnTimer(nIDEvent);
  285. }
  286.  
  287. void CTimerCtrl::OnIntervalChanged() 
  288. {
  289.     // TODO: Add notification handler code
  290.     if (GetEnabled() && AmbientUserMode())
  291.     {
  292.         StopTimer();
  293.         StartTimer();
  294.     }
  295.  
  296.     MessageBox("OnIntervalChange");
  297.  
  298.     SetModifiedFlag();
  299. }
  300.  
  301. /////////////////////////////////////////////////////////////////////////////
  302. // CTimeCtrl::OnEnabledChanged - Start/stop the timer when the enable state
  303. // has changed.
  304.  
  305. void CTimerCtrl::OnEnabledChanged()
  306. {
  307.     if (AmbientUserMode())
  308.     {
  309.         if (GetEnabled())
  310.         {
  311.             if (GetHwnd() != NULL)
  312.                 StartTimer();
  313.         }
  314.         else
  315.             StopTimer();
  316.     }
  317.     
  318.     MessageBox("OnEnabledChange");
  319. }
  320.  
  321.  
  322. /////////////////////////////////////////////////////////////////////////////
  323. // CTimeCtrl::OnAmbientPropertyChange - Start the timer if user mode ambient
  324. // property has changed and if timer is enabled.  Stop the timer if user
  325. // mode ambient property has changed and if timer is enabled.
  326.  
  327. void CTimerCtrl::OnAmbientPropertyChange(DISPID dispid)
  328. {
  329.     if (dispid == DISPID_AMBIENT_USERMODE)
  330.     {
  331.         // Start or stop the timer
  332.         if (GetEnabled())
  333.         {
  334.             if (AmbientUserMode())
  335.                 StartTimer();
  336.             else
  337.                 StopTimer();
  338.         }
  339.  
  340.         InvalidateControl();
  341.     }
  342. }
  343.  
  344.  
  345. /////////////////////////////////////////////////////////////////////////////
  346. // CTimeCtrl::StartTimer - Start the timer.
  347.  
  348. void CTimerCtrl::StartTimer()
  349. {
  350.     SetTimer(ID_TIMER, m_interval, NULL);
  351. }
  352.  
  353.  
  354. /////////////////////////////////////////////////////////////////////////////
  355. // CTimeCtrl::StopTimer - Stop the timer.
  356.  
  357. void CTimerCtrl::StopTimer()
  358. {
  359.     KillTimer(ID_TIMER);
  360. }
  361.  
  362.