home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / EDMI2.ZIP / PMINTRO.ZIP / step02.c next >
C/C++ Source or Header  |  1993-04-07  |  7KB  |  258 lines

  1. //-------------------------------------------------------------------
  2. //
  3. // File:        STEP02.C
  4. //
  5. // Description: Example of Using Threads in PM
  6. //
  7. //-------------------------------------------------------------------
  8. //
  9. // Source Code from "Introduction to PM Programming Part II" in the
  10. // Electronic Developer's Monthly Magazine (EDM/2) April 1993 Vol 2
  11. //
  12. // Written by Gavin Baker in March 1993
  13. //
  14. // net:  demogrb@lust.latrobe.edu.au
  15. // cis:  100026,270
  16. // bix:  gbaker
  17. //
  18. // For non-profit use only. Example code only - use at your own risk!
  19. //-------------------------------------------------------------------
  20.  
  21. // Includes
  22.  
  23. #define INCL_WIN
  24. #define INCL_GPI
  25. #define INCL_WINDIALOGS
  26.  
  27. #include <os2.h>
  28. #include <process.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <dos.h>
  34. #include "step02.h"
  35.  
  36. // Defines
  37.  
  38. #define WM_BEGIN_PAINT  WM_USER+1
  39. #define WM_END_PAINT    WM_USER+2
  40. #define WM_ACK          WM_USER+3
  41.  
  42. // Globals
  43.  
  44. HWND    hwndMain,
  45.         hwndWorker;
  46.  
  47. // Prototypes
  48.  
  49. MRESULT EXPENTRY MainWndProc (HWND, ULONG, MPARAM, MPARAM);
  50. VOID             WorkerThread ();
  51. MRESULT EXPENTRY WorkWndProc (HWND, ULONG, MPARAM, MPARAM);
  52. MRESULT EXPENTRY DlgProc(HWND, ULONG, MPARAM, MPARAM);
  53. VOID             WorkPaint (HWND, HPS);
  54.  
  55. // Main Program!
  56.  
  57. int main (void)
  58. {
  59.     HAB     hab;
  60.     HMQ     hmq;
  61.     HWND    hwndFrame;
  62.     QMSG    qmsg;
  63.     ULONG   flFrameFlags =  FCF_TITLEBAR      |     FCF_SYSMENU  |
  64.                             FCF_SIZEBORDER    |     FCF_MINMAX   |
  65.                             FCF_SHELLPOSITION |     FCF_TASKLIST |
  66.                             FCF_MENU          ;
  67.  
  68.     randomize();
  69.  
  70.     hab = WinInitialize (0);
  71.     hmq = WinCreateMsgQueue (hab, 0);
  72.  
  73.     WinRegisterClass (hab, "STEP2", MainWndProc, CS_SIZEREDRAW, 0);
  74.  
  75.     hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  76.                                     &flFrameFlags, "STEP2", NULL,
  77.                                     0, NULLHANDLE, ID_MAIN, &hwndMain);
  78.  
  79.     while (WinGetMsg (hab, &qmsg, 0, 0, 0))
  80.             WinDispatchMsg (hab, &qmsg);
  81.  
  82.     WinDestroyWindow (hwndFrame);
  83.     WinDestroyMsgQueue (hmq);
  84.     WinTerminate (hab);
  85.     return 0;
  86. }
  87.  
  88. MRESULT EXPENTRY MainWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  89. {
  90.     FILEDLG     fild;
  91.  
  92.     switch (msg)
  93.     {
  94.         case WM_CREATE:
  95.             if (_beginthread (WorkerThread, 8192, NULL) == -1)
  96.             {
  97.                 WinMessageBox (HWND_DESKTOP, hwnd,
  98.                     "Creation of second thread failed!", "Step 2",
  99.                     0, MB_OK | MB_CUACRITICAL);
  100.                 return 0;
  101.             }
  102.  
  103.             return 0;
  104.  
  105.         case WM_ACK:
  106.             WinPostMsg(hwndWorker, WM_BEGIN_PAINT, 0, 0);
  107.             return 0;
  108.  
  109.         case WM_SIZE:
  110.             WinSendMsg(hwndWorker, WM_END_PAINT, 0, 0);
  111.  
  112.             WinPostMsg(hwndWorker, WM_BEGIN_PAINT, 0, 0);
  113.             return 0;
  114.  
  115.         case WM_PRESPARAMCHANGED:
  116.             WinInvalidateRect(hwndMain,NULL,TRUE);
  117.             return 0;
  118.  
  119.         case WM_PAINT:
  120.             WinSendMsg(hwndWorker, WM_END_PAINT, 0, 0);
  121.  
  122.             WinPostMsg(hwndWorker, WM_BEGIN_PAINT, 0, 0);
  123.             return 0;
  124.  
  125.         case WM_ERASEBACKGROUND:
  126.             return (MRESULT) TRUE;
  127.  
  128.         case WM_COMMAND:
  129.             switch (SHORT1FROMMP(mp1))
  130.             {
  131.                 case ID_ABOUT:
  132.                     WinDlgBox(HWND_DESKTOP,hwnd,(PFNWP)DlgProc,NULLHANDLE,
  133.                         DLG_ABOUT,NULL);
  134.                     return 0;
  135.  
  136.                 case ID_FILEOPEN:
  137.                     memset(&fild, 0, sizeof(FILEDLG));
  138.                     fild.cbSize=sizeof(FILEDLG);
  139.                     fild.fl=FDS_OPEN_DIALOG | FDS_CENTER ;
  140.                     fild.pszIDrive="C:";
  141.                     WinFileDlg(HWND_DESKTOP,hwnd,&fild);
  142.                     return 0;
  143.  
  144.                 case ID_FILEEXIT:
  145.                     WinPostMsg(hwnd, WM_CLOSE, 0, 0);
  146.                     return 0;
  147.  
  148.                 default:
  149.                     return WinDefWindowProc(hwnd,msg,mp1,mp2);
  150.             }
  151.  
  152.         case WM_DESTROY:
  153.             WinSendMsg(hwndWorker, WM_DESTROY, mp1, mp2);
  154.             return 0;
  155.         }
  156.     return WinDefWindowProc (hwnd, msg, mp1, mp2);
  157. }
  158.  
  159. VOID WorkerThread ()
  160. {
  161.     HAB  hab;
  162.     HMQ  hmq;
  163.     HWND hwndObject,
  164.          hwndParent;
  165.     QMSG qmsg;
  166.  
  167.     hab = WinInitialize (0);
  168.  
  169.     hmq = WinCreateMsgQueue(hab, 0);
  170.  
  171.     WinRegisterClass(hab, "STEP2_B", WorkWndProc, 0, 0);
  172.  
  173.     hwndWorker = WinCreateWindow( HWND_OBJECT, "STEP2_B", "",
  174.              0, 0, 0, 0, 0, HWND_OBJECT, HWND_BOTTOM, 0, NULL, NULL );
  175.  
  176.     WinSendMsg( hwndMain, WM_ACK, 0, 0 );
  177.  
  178.     while( WinGetMsg ( hab, &qmsg, 0, 0, 0 ))
  179.         WinDispatchMsg ( hab, &qmsg );
  180.  
  181.     WinPostMsg( hwndMain, WM_QUIT, 0, 0 );
  182.  
  183.     WinDestroyWindow( hwndWorker );
  184.     WinDestroyMsgQueue( hmq );
  185.     WinTerminate (hab);
  186.     _endthread ();
  187. }
  188.  
  189. MRESULT EXPENTRY DlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  190. {
  191.     switch (msg)
  192.     {
  193.         case WM_COMMAND:
  194.             WinDismissDlg(hwnd,TRUE);
  195.             return 0;
  196.         default:
  197.             return WinDefDlgProc(hwnd,msg,mp1,mp2);
  198.     }
  199. }
  200.  
  201. MRESULT EXPENTRY WorkWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  202. {
  203.     static BOOL Paint=FALSE;
  204.     static HPS  hps;
  205.     SIZEL       sizel;
  206.  
  207.     switch (msg)
  208.     {
  209.         case WM_CREATE:
  210.             if (!WinStartTimer(WinQueryAnchorBlock(hwnd), hwnd, 1, 1000))
  211.                 WinMessageBox(HWND_DESKTOP,hwnd,"Could not start timer!",
  212.                     "Error",0,MB_CUACRITICAL | MB_OK);
  213.             hps = WinGetPS(hwndMain);
  214.             GpiSetBackMix(hps, BM_OVERPAINT);
  215.             return 0;
  216.  
  217.         case WM_BEGIN_PAINT:
  218.             Paint = TRUE;
  219.             return 0;
  220.  
  221.         case WM_END_PAINT:
  222.             Paint = FALSE;
  223.             return 0;
  224.  
  225.         case WM_TIMER:
  226.             if (Paint)
  227.                 WorkPaint(hwndMain, hps);
  228.             return 0;
  229.  
  230.         case WM_DESTROY:
  231.             WinStopTimer(WinQueryAnchorBlock(hwnd), hwnd, 0);
  232.             WinReleasePS(hps);
  233.             return 0;
  234.     }
  235.     return WinDefWindowProc (hwnd, msg, mp1, mp2);
  236. }
  237.  
  238. VOID WorkPaint (HWND hwnd, HPS hps)
  239. {
  240.     ULONG   x, y, cx, cy;
  241.     RECTL   rect;
  242.     POINTL  ptl;
  243.     char    s[42];
  244.     struct time  t;
  245.  
  246.     WinQueryWindowRect(hwnd, &rect);
  247.  
  248.     cx = rect.xRight - rect.xLeft;
  249.     cy = rect.yTop - rect.yBottom;
  250.  
  251.     gettime(&t);
  252.     sprintf(s,"Current Time: %2.2d:%2.2d:%2.2d",t.ti_hour,t.ti_min,t.ti_sec);
  253.  
  254.     ptl.x=(cx/3); ptl.y=(cy/2);
  255.     GpiSetColor(hps, rand() % 16);
  256.     GpiCharStringAt(hps, &ptl, strlen(s), s);
  257. }
  258.