home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1995 March / SOFM_Mar1995.bin / pc / sri / windows / edi / threads.c < prev    next >
C/C++ Source or Header  |  1995-01-27  |  7KB  |  255 lines

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "thrdapi.h"
  5.  
  6. /********************************************************************
  7.  *                     Threads test application                     *
  8.  *                                                                  *
  9.  ********************************************************************
  10.  *       Copyright 1992 Robert Salesas, All Rights Reserved         *
  11.  ********************************************************************
  12.  *       Version: 1.00             Author:  Robert Salesas          *
  13.  *       Date:    30-Jan-1992      Changes: Original                *
  14.  *                                                                  *
  15.  *       Version: 1.00             Author:  Sylvain Tremblay        *
  16.  *       Date:    24-Feb-1992      Changes: Pascal to C convertion  *
  17.  *                                                                  *
  18.  ********************************************************************/
  19.  
  20. #define APPNAME (LPSTR)"BC++/TC++ Threads"
  21. #define APPFILE "THREADS.EXE"
  22. #define CLASSNAME "Threads"
  23.  
  24. HANDLE hInst;
  25.  
  26. FARPROC BallProc,
  27.         LineProc;
  28.  
  29.  
  30. /***** Thread functions *****/
  31.  
  32. VOID FAR PASCAL _export LineThread(PThreadRec Thread,  HWND Wnd, WORD wParam, LONG lParam)
  33. {
  34.   COLORREF Colors[] = { 0x00FF0000,
  35.                0x0000FF00,
  36.                0x000000FF,
  37.                0x00FFFF00,
  38.                0x0000FFFF,
  39.                0x00FF00FF,
  40.                0x00C000C0 };
  41.   HDC DC;
  42.   RECT Rect;
  43.   HPEN Pen,
  44.        OPen;
  45.   int X, Y, ColIndex;
  46.   COLORREF Col;
  47.  
  48.   GetClientRect(Wnd, &Rect);
  49.   X = random(Rect.right);
  50.   Y = random(Rect.bottom);
  51.  
  52.   Col = Colors[0];
  53.   Pen = CreatePen(PS_SOLID, 1, Col);
  54.  
  55.   do {
  56.     DC = GetDC(Wnd);
  57.     if (DC == 0) {
  58.       DeleteObject(Pen);
  59.       ExitThread();
  60.     }
  61.  
  62.     OPen = SelectObject(DC, Pen);
  63.  
  64.     GetClientRect(Wnd, &Rect);
  65.     MoveTo(DC, X, Y);
  66.     X = max(0, min(Rect.right, X + random(91) - 45));
  67.     Y = max(0, min(Rect.bottom, Y + random(91) - 45));
  68.     LineTo(DC, X, Y);
  69.  
  70.     SelectObject(DC, OPen);
  71.     ReleaseDC(Wnd, DC);
  72.   } while (YieldThread() != TM_QUIT);
  73.  
  74.   DeleteObject(Pen);
  75.   ExitThread();
  76. }
  77.  
  78.  
  79. VOID FAR PASCAL _export BallThread(PThreadRec Thread, HWND Wnd, WORD wParam, LONG lParam)
  80. {
  81.   HDC DC;
  82.   RECT Rect;
  83.   int  XDir,
  84.        YDir,
  85.        X, OX,
  86.        Y, OY;
  87.   HICON Ball,
  88.      Erase;
  89.  
  90.   X = 0;
  91.   Y = 0;
  92.   XDir = 10 + (random(11) - 5);
  93.   YDir = 10 + (random(11) - 5);
  94.  
  95.   Ball = LoadIcon(hInst, MAKEINTRESOURCE(random(4) + 100));
  96.   Erase = LoadIcon(hInst, "ERASEBALL");
  97.  
  98.   do {
  99.     DC = GetDC(Wnd);
  100.     if (DC == 0)
  101.      ExitThread();
  102.  
  103.     GetClientRect(Wnd, &Rect);
  104.     OX = X;
  105.     OY = Y;
  106.     X = X + XDir;
  107.     Y = Y + YDir;
  108.  
  109.     if (X < 0) {
  110.       X = 0;
  111.       XDir = -(XDir - (random(11) - 5));
  112.       YDir = YDir + (random(11) - 5);
  113.     }
  114.     if (X + 32 > Rect.right) {
  115.       X = Rect.right - 32;
  116.       XDir = -(XDir - (random(11) - 5));
  117.       YDir = YDir + (random(11) - 5);
  118.     }
  119.  
  120.     if (Y < 0) {
  121.       Y = 0;
  122.       XDir = XDir - (random(11) - 5);
  123.       YDir = -(YDir + (random(11) - 5));
  124.     }
  125.     if (Y + 32 > Rect.bottom) {
  126.       Y = Rect.bottom - 32;
  127.       XDir = XDir + (random(11) - 5);
  128.       YDir = -(YDir + (random(11) - 5));
  129.     }
  130.  
  131.  
  132.     if ((XDir <= 0) && (XDir > -6))
  133.      XDir = -6;
  134.     if ((XDir > 0) && (XDir < 6))
  135.      XDir = 6;
  136.     if ((YDir <= 0) && (YDir > -6))
  137.      YDir = -6;
  138.     if ((YDir > 0) && (YDir < 6))
  139.      YDir = 5;
  140.     XDir = max(-20, min(20, XDir));
  141.     YDir = max(-20, min(20, YDir));
  142.  
  143.     DrawIcon(DC, OX, OY, Erase);
  144.     DrawIcon(DC, X, Y, Ball);
  145.     ReleaseDC(Wnd, DC);
  146.  
  147.   } while (YieldThread() != TM_QUIT);
  148.  
  149.   ExitThread();
  150. }
  151.  
  152.  
  153. /***** Window function *****/
  154.  
  155. LONG _export FAR PASCAL MainWndProc(HWND Window, WORD Msg, WORD wParam, DWORD lParam)
  156. {
  157.   char Title[256];
  158.   LONG NumThreads;
  159.  
  160.   switch (Msg) {
  161.     case WM_CREATE:
  162.       LineProc = MakeProcInstance((FARPROC)LineThread, hInst);
  163.       BallProc = MakeProcInstance((FARPROC)BallThread, hInst);
  164.       break;
  165.     case WM_COMMAND:
  166.       switch(wParam) {
  167.     case 100:
  168.       StartThread(BallProc, 2000, Window, 0, 0);
  169.       NumThreads = GetNumThreads();
  170.       wsprintf(Title, "%s - %d Threads", APPNAME, NumThreads);
  171.       SetWindowText(Window, Title);
  172.       break;
  173.     case 110:
  174.       SetThreadPriority(StartThread((FARPROC)LineProc, 2000, Window, 0, 0), TS_DEFPRIORITY / 2);
  175.       NumThreads = GetNumThreads();
  176.       wsprintf(Title, "%s - %d Threads", APPNAME, NumThreads);
  177.       SetWindowText(Window, Title);
  178.       break;
  179.  
  180.     case 500:
  181.       InvalidateRect(Window, NULL,TRUE);
  182.       break;
  183.     case 510:
  184.       EndTaskThreads(GetCurrentTask());
  185.       InvalidateRect(Window, NULL,TRUE);
  186.       NumThreads = GetNumThreads();
  187.       wsprintf(Title, "%s - %d Threads", APPNAME, NumThreads);
  188.       SetWindowText(Window, Title);
  189.       break;
  190.      }
  191.     break;
  192.  
  193.     case WM_DESTROY:
  194.       EndTaskThreads(GetCurrentTask());
  195.       FreeProcInstance(BallProc);
  196.       FreeProcInstance(LineProc);
  197.       PostQuitMessage(0);
  198.       break;
  199.  
  200.     default:
  201.       return DefWindowProc(Window, Msg, wParam, lParam);
  202.   }
  203. }
  204.  
  205.  
  206. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  207. {
  208.   HWND hWnd;
  209.   MSG msg;
  210.   WNDCLASS wndclass;
  211.   char Title[256];
  212.  
  213.   if(!hPrevInstance) {
  214.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  215.     wndclass.lpfnWndProc   = MainWndProc;
  216.     wndclass.cbClsExtra    = 0;
  217.     wndclass.cbWndExtra    = 0;
  218.     wndclass.hInstance     = 0;
  219.     wndclass.hIcon         = LoadIcon(0, IDI_APPLICATION);
  220.     wndclass.hCursor       = LoadCursor(0, IDC_ARROW);
  221.     wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  222.     wndclass.lpszMenuName  = (LPSTR)"APPMENU";
  223.     wndclass.lpszClassName = CLASSNAME;
  224.  
  225.     if(!RegisterClass(&wndclass)) {
  226.       MessageBox(NULL, "Unable to register window class.", NULL, MB_OK|MB_ICONSTOP);
  227.       return 0;
  228.     }
  229.   }
  230.  
  231.   hInst = hInstance;
  232.  
  233.   wsprintf(Title, "%s - 0 Threads", APPNAME);
  234.  
  235.   hWnd = CreateWindow(CLASSNAME,
  236.                       Title,
  237.                       WS_OVERLAPPEDWINDOW,
  238.                       CW_USEDEFAULT,
  239.                       0,
  240.                       CW_USEDEFAULT,
  241.                       0,
  242.                       0,
  243.                       0,
  244.                       hInstance,
  245.                       NULL);
  246.  
  247.   ShowWindow(hWnd, nCmdShow);
  248.  
  249.   while(GetMessage(&msg, NULL, 0, 0)) {
  250.     TranslateMessage(&msg);
  251.     DispatchMessage(&msg);
  252.   }
  253.   return msg.wParam;
  254. }
  255.