home *** CD-ROM | disk | FTP | other *** search
/ Master Visual C++ 1.5 / MASTERVC15.ISO / vcprog / original / ch35 / myclock / myclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-09  |  8.6 KB  |  330 lines

  1. //-------------------------------------------------------
  2. // MYCLOCK.C
  3. //-------------------------------------------------------
  4. // Contains code for Myclock VBX control.
  5. //
  6. // Use the following files as templates for building your
  7. // own VBX control:
  8. //
  9. // - MYCLOCK.C (this file)
  10. // - MYCLOCK.H
  11. // - MYCLOCK.DEF
  12. // - MYCLOCK.RC 
  13. //-------------------------------------------------------
  14.  
  15. #include <windows.h>
  16. #include "vbapi.h"
  17. #include "MYCLOCK.H"
  18.  
  19. //////////////////////
  20. // MY CODE STARTS HERE
  21. //////////////////////
  22.  
  23. #include <time.h>
  24.  
  25. ////////////////////
  26. // MY CODE ENDS HERE
  27. ////////////////////
  28.  
  29.  
  30. //------------------------------------------------------
  31. // Global Variables
  32. //------------------------------------------------------
  33. HANDLE hmodDLL;
  34.  
  35.  
  36. //------------------------------------------------------
  37. // Local Prototypes
  38. //------------------------------------------------------
  39. VOID NEAR DrawTheControl(HCTL hctl, HWND hwnd, HDC hdc);
  40.  
  41.  
  42. //------------------------------------------------------
  43. // Myclock Control Procedure
  44. //------------------------------------------------------
  45. LONG FAR PASCAL _export MyclockCtlProc
  46. (
  47.     HCTL   hctl,
  48.     HWND   hwnd,
  49.     USHORT msg,
  50.     USHORT wp,
  51.     LONG   lp
  52. )
  53. {
  54.  
  55.     // Process messages of the VBX control.
  56.     switch (msg)
  57.         {
  58.         case WM_NCCREATE:
  59.          
  60.              // TODO: Add initialization code here
  61.  
  62.              //////////////////////
  63.              // MY CODE STARTS HERE
  64.              //////////////////////
  65.          
  66.              // Install a timer.
  67.              SetTimer(hwnd,1,1000,NULL);
  68.                 
  69.              // Set the BackColor property to 65280 (Green).
  70.              VBSetControlProperty(hctl, 
  71.                                   IPROP_MYCLOCK_BACKCOLOR,
  72.                                   65280L);
  73.                           
  74.              // Set the UpdateInterval property to 1000
  75.              pMYCLOCK(hctl)->UpdateInterval = 1000;         
  76.              
  77.              ////////////////////
  78.              // MY CODE ENDS HERE
  79.              ////////////////////
  80.          
  81.              break;
  82.  
  83.         case WM_PAINT:
  84.                            
  85.              // Note: Write the control drawing code
  86.              //       inside the function DrawTheControl().
  87.          
  88.              if (wp)
  89.                 DrawTheControl(hctl, hwnd, (HDC)wp);
  90.              else
  91.                 {
  92.                 PAINTSTRUCT ps;
  93.                 BeginPaint(hwnd, &ps);
  94.                 DrawTheControl(hctl, hwnd, ps.hdc);
  95.                 EndPaint(hwnd, &ps);
  96.                 }
  97.          
  98.              break;
  99.  
  100.  
  101.         case VBM_SETPROPERTY:
  102.          
  103.              // NOTE: wp = Property that was just changed.
  104.              //       lp = New value of the property.
  105.  
  106.              switch (wp)
  107.                 {
  108.                 // TODO: Add a case for each custom property
  109.              
  110.                 //////////////////////
  111.                 // MY CODE STARTS HERE
  112.                 //////////////////////
  113.     
  114.                 case IPROP_MYCLOCK_UPDATEINTERVAL:
  115.                   
  116.                      // Set the UpdateInterval property with
  117.                      // the new value.
  118.                      pMYCLOCK(hctl)->UpdateInterval = (SHORT)lp;
  119.                   
  120.                      // Set the timer with the new value.
  121.                      SetTimer(hwnd,
  122.                               1,
  123.                               pMYCLOCK(hctl)->UpdateInterval,
  124.                               NULL);
  125.                   
  126.                      return 0;
  127.              
  128.                 ////////////////////
  129.                 // MY CODE ENDS HERE
  130.                 ////////////////////         
  131.  
  132.  
  133.                 }
  134.          
  135.              break;
  136.  
  137.         case WM_TIMER:
  138.              {
  139.              // TODO: Add timer code here
  140.  
  141.              ///////////////////////
  142.              // MY CODE STARTS HERE
  143.              //////////////////////
  144.  
  145.              // Draw the control.
  146.              HDC hdc = GetDC(hwnd);
  147.              DrawTheControl(hctl, hwnd, hdc);  
  148.              ReleaseDC(hwnd,hdc);
  149.              
  150.              ///////////////////////
  151.              // MY CODE STARTS HERE
  152.              //////////////////////
  153.               
  154.              break;
  155.              }
  156.  
  157.  
  158.         // TODO: Add cases for other events here
  159.  
  160.  
  161.         }
  162.  
  163.  
  164.     return VBDefControlProc(hctl, hwnd, msg, wp, lp);
  165. }
  166.  
  167.  
  168.  
  169. //------------------------------------------------------
  170. // Initialize library. This routine is called when the
  171. // first client loads the DLL.
  172. //------------------------------------------------------
  173. int FAR PASCAL LibMain
  174. (
  175.     HANDLE hModule,
  176.     WORD   wDataSeg,
  177.     WORD   cbHeapSize,
  178.     LPSTR  lpszCmdLine
  179. )
  180. {
  181.     // Avoid warnings on unused formal parameters
  182.     wDataSeg    = wDataSeg;
  183.     cbHeapSize  = cbHeapSize;
  184.     lpszCmdLine = lpszCmdLine;
  185.  
  186.     hmodDLL = hModule;
  187.  
  188.     return 1;
  189. }
  190.  
  191.  
  192. //------------------------------------------------------
  193. // Register custom control. This routine is called by VB 
  194. // when the custom control DLL is loaded for use.
  195. //------------------------------------------------------
  196. BOOL FAR PASCAL _export VBINITCC
  197. (
  198.     USHORT usVersion,
  199.     BOOL   fRuntime
  200. )
  201. {
  202.     // Avoid warnings on unused formal parameters
  203.     fRuntime  = fRuntime;
  204.     usVersion = usVersion;
  205.  
  206.     // Register control(s)
  207.     return VBRegisterModel(hmodDLL, &modelMyclock);
  208. }
  209.  
  210.  
  211. //------------------------------------------------------
  212. // WEP
  213. //------------------------------------------------------
  214. // C7 and QCWIN provide default WEP:
  215. //------------------------------------------------------
  216. #if (_MSC_VER < 610)
  217.  
  218. int FAR PASCAL WEP(int fSystemExit);
  219.  
  220. //------------------------------------------------------
  221. // For Windows 3.0 it is recommended that the WEP
  222. // function reside in a FIXED code segment and be
  223. // exported as RESIDENTNAME.  This is accomplished
  224. // using the alloc_text pragma below and the related
  225. // EXPORTS and SEGMENTS directives in the .DEF file.
  226. //
  227. // Read the comments section documenting the WEP
  228. // function in the Windows 3.1 SDK "Programmers
  229. // Reference, Volume 2: Functions" before placing
  230. // any additional code in the WEP routine for a 
  231. // Windows 3.0 DLL.
  232. //------------------------------------------------------
  233. #pragma alloc_text(WEP_TEXT,WEP)
  234.  
  235. //------------------------------------------------------
  236. // Performs cleanup tasks when the DLL is unloaded.
  237. // WEP() is called automatically by Windows when the DLL
  238. // is unloaded (no remaining tasks still have the DLL 
  239. // loaded). It is strongly recommended that a DLL have a 
  240. // WEP() function, even if it does nothing but returns 
  241. // success (1), as in this example.
  242. //------------------------------------------------------
  243. int FAR PASCAL WEP
  244. (
  245.     int fSystemExit
  246. )
  247. {
  248.     // Avoid warnings on unused formal parameters
  249.     fSystemExit = fSystemExit;
  250.  
  251.     return 1;
  252. }
  253. #endif // C6
  254.  
  255. //------------------------------------------------------
  256.  
  257. //------------------------------------------------------
  258. // Draw inside the control.
  259. //------------------------------------------------------
  260. VOID NEAR DrawTheControl
  261. (
  262.     HCTL hctl,
  263.     HWND hwnd,
  264.     HDC  hdc
  265. )
  266. {
  267.    // Variables for the brush.
  268.    HBRUSH hbr;
  269.    HBRUSH hbrOld = NULL;
  270.  
  271.    // TODO: Define your own local variables here (if any)
  272.  
  273.    //////////////////////
  274.    // MY CODE STARTS HERE
  275.    //////////////////////
  276.    
  277.    char CurrentTime[30];
  278.    struct tm *newtime;
  279.    long lTime;
  280.    
  281.    ////////////////////
  282.    // MY CODE ENDS HERE
  283.    ////////////////////
  284.    
  285.    // Select new brush, and save old brush.
  286.    hbr = (HBRUSH)SendMessage(GetParent(hwnd),
  287.           WM_CTLCOLOR, hdc, MAKELONG(hwnd,0));
  288.    if (hbr)
  289.       hbrOld = SelectObject(hdc, hbr);
  290.  
  291.    
  292.    // TODO: Add your drawing code here
  293.  
  294.    ///////////////////////
  295.    // MY CODE STARTS HERE
  296.    //////////////////////
  297.  
  298.    // Get the current time
  299.    time(&lTime);
  300.    newtime=localtime(&lTime);
  301.    
  302.    // Convert the time into a string.
  303.    strcpy(CurrentTime, asctime(newtime));
  304.    
  305.    // Pad the string with 5 blanks
  306.    CurrentTime[24]=' ';
  307.    CurrentTime[25]=' ';
  308.    CurrentTime[26]=' ';
  309.    CurrentTime[27]=' ';
  310.    CurrentTime[28]=' ';
  311.    
  312.    // Display the current time
  313.    TextOut(hdc, 0, 0, CurrentTime, 29);
  314.  
  315.    // If a new minute has just begun, fire the NEWMINUTE event.
  316.    if (newtime->tm_sec==0)
  317.       VBFireEvent(hctl, IEVENT_MYCLOCK_NEWMINUTE, NULL);
  318.  
  319.    ////////////////////
  320.    // MY CODE ENDS HERE
  321.    ////////////////////
  322.  
  323.  
  324.    // Restore the old brush
  325.    if (hbrOld)
  326.       SelectObject(hdc, hbrOld);
  327.  
  328. }
  329.  
  330.