home *** CD-ROM | disk | FTP | other *** search
/ Gurewich OLE Controls for Visual Basic 4 / Gurewich OLE Controls for Visual Basic 4.iso / ocxprog / programs / ch17 / myclock / mycloctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-20  |  9.2 KB  |  366 lines

  1. // mycloctl.cpp : Implementation of the CMyclockCtrl OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "myclock.h"
  5. #include "mycloctl.h"
  6. #include "mycloppg.h"
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14.  
  15. IMPLEMENT_DYNCREATE(CMyclockCtrl, COleControl)
  16.  
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Message map
  20.  
  21. BEGIN_MESSAGE_MAP(CMyclockCtrl, COleControl)
  22.     //{{AFX_MSG_MAP(CMyclockCtrl)
  23.     ON_WM_CREATE()
  24.     ON_WM_TIMER()
  25.     //}}AFX_MSG_MAP
  26.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  27. END_MESSAGE_MAP()
  28.  
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Dispatch map
  32.  
  33. BEGIN_DISPATCH_MAP(CMyclockCtrl, COleControl)
  34.     //{{AFX_DISPATCH_MAP(CMyclockCtrl)
  35.     DISP_PROPERTY_NOTIFY(CMyclockCtrl, "UpdateInterval", m_updateInterval, OnUpdateIntervalChanged, VT_I4)
  36.     DISP_FUNCTION(CMyclockCtrl, "Beep", Beep, VT_EMPTY, VTS_NONE)
  37.     DISP_STOCKFUNC_REFRESH()
  38.     DISP_STOCKPROP_BACKCOLOR()
  39.     DISP_STOCKPROP_FORECOLOR()
  40.     DISP_STOCKPROP_FONT()
  41.     //}}AFX_DISPATCH_MAP
  42.     DISP_FUNCTION_ID(CMyclockCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  43. END_DISPATCH_MAP()
  44.  
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // Event map
  48.  
  49. BEGIN_EVENT_MAP(CMyclockCtrl, COleControl)
  50.     //{{AFX_EVENT_MAP(CMyclockCtrl)
  51.     EVENT_CUSTOM("NewMinute", FireNewMinute, VTS_NONE)
  52.     EVENT_STOCK_CLICK()
  53.     EVENT_STOCK_DBLCLICK()
  54.     //}}AFX_EVENT_MAP
  55. END_EVENT_MAP()
  56.  
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. // Property pages
  60.  
  61. // TODO: Add more property pages as needed.  Remember to increase the count!
  62. BEGIN_PROPPAGEIDS(CMyclockCtrl, 3)
  63.      PROPPAGEID(CMyclockPropPage::guid)
  64.      PROPPAGEID(CLSID_CColorPropPage)
  65.      PROPPAGEID(CLSID_CFontPropPage)
  66. END_PROPPAGEIDS(CMyclockCtrl)
  67.  
  68.  
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // Initialize class factory and guid
  72.  
  73. IMPLEMENT_OLECREATE_EX(CMyclockCtrl, "MYCLOCK.MyclockCtrl.1",
  74.     0x85a87840, 0xd415, 0x101b, 0x96, 0xe3, 0x0, 0x20, 0xaf, 0x38, 0xf4, 0xbb)
  75.  
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // Type library ID and version
  79.  
  80. IMPLEMENT_OLETYPELIB(CMyclockCtrl, _tlid, _wVerMajor, _wVerMinor)
  81.  
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Interface IDs
  85.  
  86. const IID BASED_CODE IID_DMyclock =
  87.         { 0x85a87841, 0xd415, 0x101b, { 0x96, 0xe3, 0x0, 0x20, 0xaf, 0x38, 0xf4, 0xbb } };
  88. const IID BASED_CODE IID_DMyclockEvents =
  89.         { 0x85a87842, 0xd415, 0x101b, { 0x96, 0xe3, 0x0, 0x20, 0xaf, 0x38, 0xf4, 0xbb } };
  90.  
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // Control type information
  94.  
  95. static const DWORD BASED_CODE _dwMyclockOleMisc =
  96.     OLEMISC_ACTIVATEWHENVISIBLE |
  97.     OLEMISC_SETCLIENTSITEFIRST |
  98.     OLEMISC_INSIDEOUT |
  99.     OLEMISC_CANTLINKINSIDE |
  100.     OLEMISC_RECOMPOSEONRESIZE;
  101.  
  102. IMPLEMENT_OLECTLTYPE(CMyclockCtrl, IDS_MYCLOCK, _dwMyclockOleMisc)
  103.  
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CMyclockCtrl::CMyclockCtrlFactory::UpdateRegistry -
  107. // Adds or removes system registry entries for CMyclockCtrl
  108.  
  109. BOOL CMyclockCtrl::CMyclockCtrlFactory::UpdateRegistry(BOOL bRegister)
  110. {
  111.     if (bRegister)
  112.         return AfxOleRegisterControlClass(
  113.             AfxGetInstanceHandle(),
  114.             m_clsid,
  115.             m_lpszProgID,
  116.             IDS_MYCLOCK,
  117.             IDB_MYCLOCK,
  118.             FALSE,                      //  Not insertable
  119.             _dwMyclockOleMisc,
  120.             _tlid,
  121.             _wVerMajor,
  122.             _wVerMinor);
  123.     else
  124.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  125. }
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CMyclockCtrl::CMyclockCtrl - Constructor
  130.  
  131. CMyclockCtrl::CMyclockCtrl()
  132. {
  133.     InitializeIIDs(&IID_DMyclock, &IID_DMyclockEvents);
  134.  
  135.     // TODO: Initialize your control's instance data here.
  136.     
  137.     //////////////////////
  138.     // MY CODE STARTS HERE
  139.     //////////////////////
  140.     
  141.     // Set the initial size of the control to Width=200, Height=15.
  142.     SetInitialSize(200, 15);
  143.     
  144.     ////////////////////
  145.     // MY CODE ENDS HERE
  146.     ////////////////////
  147.     
  148. }
  149.  
  150.  
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CMyclockCtrl::~CMyclockCtrl - Destructor
  153.  
  154. CMyclockCtrl::~CMyclockCtrl()
  155. {
  156.     // TODO: Cleanup your control's instance data here.
  157. }
  158.  
  159.  
  160. /////////////////////////////////////////////////////////////////////////////
  161. // CMyclockCtrl::OnDraw - Drawing function
  162.  
  163. void CMyclockCtrl::OnDraw(
  164.              CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  165. {
  166.     // TODO: Replace the following code with your own drawing code.
  167.     
  168.    //////////////////////
  169.    // MY CODE STARTS HERE
  170.    //////////////////////
  171.  
  172.     // Select the font that is specified by the stock Font property.
  173.     CFont* pOldFont = SelectStockFont(pdc);
  174.     
  175.     // Set the foreground color (i.e. the text color)
  176.     // according to the ForeColor property.
  177.     pdc->SetTextColor(TranslateColor(GetForeColor()));
  178.     
  179.     // Set the background mode to transparent mode.
  180.     pdc->SetBkMode(TRANSPARENT);
  181.  
  182.     // Create a brush based on the BackColor property. 
  183.     CBrush bkBrush(TranslateColor(GetBackColor()));
  184.  
  185.     // Paint the background using the BackColor property
  186.     pdc->FillRect(rcBounds, &bkBrush);
  187.    
  188.    char CurrentTime[30];
  189.    struct tm *newtime;
  190.    long lTime;
  191.  
  192.    // Get the current time
  193.    time(&lTime);
  194.    newtime=localtime(&lTime);
  195.  
  196.    // Convert the time into a string.
  197.    strcpy(CurrentTime, asctime(newtime));
  198.  
  199.    // Pad the string with 1 blank.
  200.    CurrentTime[24]=' ';
  201.    
  202.    // Terminate the string.
  203.    CurrentTime[25] = 0;
  204.  
  205.    // Display the current time
  206.    pdc->ExtTextOut(rcBounds.left, 
  207.                    rcBounds.top, 
  208.                    ETO_CLIPPED, 
  209.                    rcBounds, 
  210.                    CurrentTime,
  211.                    strlen(CurrentTime),
  212.                    NULL);
  213.                    
  214.     // Select the original font.
  215.     pdc->SelectObject(pOldFont);
  216.  
  217.  
  218.     // If new minute has just begun, fire a NewMinute event.
  219.    if (newtime->tm_sec==0)
  220.       FireNewMinute();
  221.  
  222.  
  223.    ////////////////////
  224.    // MY CODE ENDS HERE
  225.    ////////////////////
  226.  
  227. }
  228.  
  229.  
  230. /////////////////////////////////////////////////////////////////////////////
  231. // CMyclockCtrl::DoPropExchange - Persistence support
  232.  
  233. void CMyclockCtrl::DoPropExchange(CPropExchange* pPX)
  234. {
  235.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  236.     COleControl::DoPropExchange(pPX);
  237.  
  238.     // TODO: Call PX_ functions for each persistent custom property.
  239.     
  240.     //////////////////////
  241.     // MY CODE STARTS HERE
  242.     //////////////////////
  243.     
  244.     // Initialize the UpdateInterval property to 1000.
  245.     PX_Long( pPX, _T("UpdateIntrval"), m_updateInterval, 1000L );
  246.     
  247.     ////////////////////
  248.     // MY CODE ENDS HERE
  249.     ////////////////////
  250.  
  251. }
  252.  
  253.  
  254. /////////////////////////////////////////////////////////////////////////////
  255. // CMyclockCtrl::OnResetState - Reset control to default state
  256.  
  257. void CMyclockCtrl::OnResetState()
  258. {
  259.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  260.  
  261.     // TODO: Reset any other control state here.
  262. }
  263.  
  264.  
  265. /////////////////////////////////////////////////////////////////////////////
  266. // CMyclockCtrl::AboutBox - Display an "About" box to the user
  267.  
  268. void CMyclockCtrl::AboutBox()
  269. {
  270.     CDialog dlgAbout(IDD_ABOUTBOX_MYCLOCK);
  271.     dlgAbout.DoModal();
  272. }
  273.  
  274.  
  275. /////////////////////////////////////////////////////////////////////////////
  276. // CMyclockCtrl message handlers
  277.  
  278. int CMyclockCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  279. {
  280.     if (COleControl::OnCreate(lpCreateStruct) == -1)
  281.         return -1;
  282.     
  283.     // TODO: Add your specialized creation code here
  284.     
  285.     
  286.     //////////////////////
  287.     // MY CODE STARTS HERE
  288.     //////////////////////
  289.  
  290.     // Install a timer.
  291.     SetTimer(1, (UINT)m_updateInterval, NULL);
  292.     
  293.     ////////////////////
  294.     // MY CODE ENDS HERE
  295.     ////////////////////
  296.     
  297.     return 0;
  298. }
  299.  
  300. void CMyclockCtrl::OnTimer(UINT nIDEvent) 
  301. {
  302.     // TODO: Add your message handler code here and/or call default
  303.     
  304.     //////////////////////
  305.     // MY CODE STARTS HERE
  306.     //////////////////////
  307.  
  308.     // Trigger a call to the OnDraw() function.
  309.     InvalidateControl();
  310.  
  311.     ////////////////////
  312.     // MY CODE ENDS HERE
  313.     ////////////////////
  314.     
  315.     COleControl::OnTimer(nIDEvent);
  316. }
  317.  
  318. void CMyclockCtrl::OnUpdateIntervalChanged() 
  319. {
  320.     // TODO: Add notification handler code
  321.     
  322.     
  323.     //////////////////////
  324.     // MY CODE STARTS HERE
  325.     //////////////////////
  326.  
  327.     // Make sure the user did not set the property to a 
  328.     // negative value.
  329.     if (m_updateInterval < 0)
  330.        {
  331.        MessageBox("This property cannot be negative!");
  332.        m_updateInterval = 1000;
  333.        } 
  334.     
  335.     // Re-install the timer with interval set
  336.     // to the current value of the UpdateInterval
  337.     // property.
  338.     SetTimer(1, (UINT)m_updateInterval, NULL);
  339.  
  340.     
  341.     SetModifiedFlag();
  342.     
  343.     ////////////////////
  344.     // MY CODE ENDS HERE
  345.     ////////////////////    
  346.  
  347.     
  348. }
  349.  
  350.  
  351. void CMyclockCtrl::Beep() 
  352. {
  353.     // TODO: Add your dispatch handler code here
  354.  
  355.     //////////////////////
  356.     // MY CODE STARTS HERE
  357.     //////////////////////
  358.     
  359.     MessageBeep((WORD)-1);
  360.     
  361.     ////////////////////
  362.     // MY CODE ENDS HERE
  363.     ////////////////////
  364.  
  365. }
  366.