home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rundemo.zip / RUN.C < prev    next >
C/C++ Source or Header  |  1989-05-26  |  4KB  |  172 lines

  1. /* run.c
  2.  *
  3.  * Obnoxious Window by Alan Bishop (agb@ecsvax.uncecs.edu)
  4.  *
  5.  * The /h (hard) option turns off the tasklist flag
  6.  *
  7.  */
  8.  
  9. #define INCL_WIN
  10. #include <os2.h>
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM);
  17. void move_window(void);
  18. void calculate_push(int x1, int y1, int x2, int y2, double *cx, double *cy, int mult);
  19.  
  20. #define ID_TIMER    1
  21.  
  22. HAB hab;
  23. HWND hwndFrame;
  24. static SWP frame;
  25.  
  26. int
  27. main(int argc, char *argv[])
  28. {
  29.     static CHAR  szClientClass[] = "Welcome";
  30.     static ULONG flFrameFlags =     FCF_SYSMENU | FCF_BORDER | FCF_TASKLIST;
  31.     HMQ hmq;
  32.     HWND hwndClient;
  33.     QMSG qmsg;
  34.  
  35. /* Turn on hard option */
  36.     if (argc == 2 && !strcmp(argv[1], "/h")) flFrameFlags &= ~FCF_TASKLIST;
  37.     
  38.     hab = WinInitialize(0);
  39.     hmq = WinCreateMsgQueue(hab, 0);
  40.  
  41.     WinRegisterClass(hab, szClientClass, ClientWndProc, 0L, 0);
  42.  
  43.     hwndFrame = WinCreateStdWindow(HWND_DESKTOP, 0L,
  44.         &flFrameFlags, szClientClass, NULL, 0L, NULL, 0, &hwndClient);
  45.  
  46.     WinSetWindowPos(hwndFrame, NULL, 100, 100, 50, 50,
  47.         SWP_MOVE | SWP_SIZE | SWP_SHOW);
  48.         
  49.     WinSendMsg(hwndFrame, WM_SETICON, WinQuerySysPointer(HWND_DESKTOP,
  50.         SPTR_APPICON, FALSE), NULL);
  51.  
  52.     WinStartTimer(hab, hwndFrame, ID_TIMER, 200);
  53.  
  54. /* Figure out how big the desktop is */        
  55.     WinQueryWindowPos(HWND_DESKTOP, &frame);
  56.  
  57.     while (WinGetMsg(hab, &qmsg, NULL, 0, 0)) {
  58.  
  59.         WinDispatchMsg(hab, &qmsg);
  60.  
  61. /* Move the window every time we get a message */
  62.         move_window();
  63.     }
  64.  
  65.     WinDestroyWindow(hwndFrame);
  66.     WinDestroyMsgQueue(hmq);
  67.     WinTerminate(hab);
  68.     return(0);
  69. }
  70.  
  71. MRESULT EXPENTRY
  72. ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  73. {
  74.     HPS hps;
  75.  
  76.     switch (msg) {
  77.  
  78. /* Just keep the screen clear */
  79.         case WM_PAINT :
  80.             hps = WinBeginPaint(hwnd, NULL, NULL);
  81.  
  82.             GpiErase(hps);
  83.             
  84.             WinEndPaint(hps);
  85.             return(0);
  86.  
  87.         default :
  88.             return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  89.  
  90.     }
  91. }
  92.  
  93. /* Recalculate where the window should be */
  94. void
  95. move_window()
  96. {
  97.     SWP ipos;
  98.     POINTL mpos;
  99.     static double rdx = 0.0, rdy = 0.0;
  100.     
  101.     WinQueryPointerPos(HWND_DESKTOP, &mpos);
  102.     WinQueryWindowPos(hwndFrame, &ipos);
  103.  
  104. /* Check bounds and bounce off of edges */
  105.     if (ipos.x < 2) {
  106.         ipos.x = 1;
  107.         rdx = 1.0;
  108.     }
  109.     if (ipos.y < 2) {
  110.         ipos.y = 1;
  111.         rdy = 1.0;
  112.     }
  113.     if (ipos.x > frame.cx - 52) {
  114.         ipos.x = frame.cx - 51;
  115.         rdx = -1.0;
  116.     }
  117.     if (ipos.y > frame.cy - 52) {
  118.         ipos.y = frame.cy - 51;
  119.         rdy = -1.0;
  120.     }
  121.  
  122. /* Calculate the push from: the four corners, the four points of the
  123.  * edge of the screen where horizontal and vertical lines meet,
  124.  * and the mouse
  125.  */    
  126.     calculate_push(0           , 0           , ipos.x, ipos.y, &rdx, &rdy, 1);
  127.     calculate_push(0           , frame.cy    , ipos.x, ipos.y, &rdx, &rdy, 1);
  128.     calculate_push(frame.cx    , 0           , ipos.x, ipos.y, &rdx, &rdy, 1);
  129.     calculate_push(frame.cx    , frame.cy    , ipos.x, ipos.y, &rdx, &rdy, 1);
  130.     calculate_push(ipos.x      , 0           , ipos.x, ipos.y, &rdx, &rdy, 2);
  131.     calculate_push(ipos.x      , frame.cy    , ipos.x, ipos.y, &rdx, &rdy, 2);
  132.     calculate_push(0           , ipos.y      , ipos.x, ipos.y, &rdx, &rdy, 2);
  133.     calculate_push(frame.cx    , ipos.y      , ipos.x, ipos.y, &rdx, &rdy, 2);
  134.     calculate_push((int) mpos.x, (int) mpos.y, ipos.x, ipos.y, &rdx, &rdy, 8);
  135.  
  136.     ipos.x += (int) rdx;
  137.     ipos.y += (int) rdy;
  138.  
  139.     WinSetWindowPos(hwndFrame, NULL, ipos.x, ipos.y, 0, 0,
  140.         SWP_MOVE);
  141. }
  142.  
  143. void
  144. calculate_push(int x1, int y1, int x2, int y2, double *cx, double *cy, int mult)
  145. /* Figure out how much we should change the acceleration */
  146. {
  147. #define sign(x) (x > 0 ? 1 : x < 0 ? -1 : 0)
  148.     double dist;
  149.     double chx, chy;
  150.     double mag;
  151.     double dx, dy;
  152.  
  153.     dx = (double) (x1 - x2);
  154.     dy = (double) (y1 - y2);
  155.     
  156.     dist = dx*dx + dy*dy;
  157.  
  158.     if (dist == 0.0) dist = 0.0001;
  159.     if (dx == 0.0) dx = 0.0001;
  160.  
  161.     mag = 8000/dist*(double)mult;
  162.     chx = mag*sign(x2 - x1);
  163.     chy = mag*sign(y2 - y1);
  164.  
  165.     if (chx > 10.0) chx = 10.0;
  166.     if (chy > 10.0) chy = 10.0;
  167.     if (chx < -10.0) chx = -10.0;
  168.     if (chy < -10.0) chy = -10.0;
  169.     *cx += chx;
  170.     *cy += chy;
  171. }
  172.