home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / wpj_mag / wpjv1n8.zip / GDI.ZIP / PBALL.ZIP / PBALL.C < prev    next >
C/C++ Source or Header  |  1993-08-11  |  9KB  |  395 lines

  1. #include <windows.h>        /* required for all Windows applications */
  2. #include "pball.h"        /* specific to this program */
  3.  
  4.  
  5. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  6. {
  7.     MSG msg;    /* message */
  8.  
  9.     if (!hPrevInstance)        /* Other instances of app running? */
  10.         if (!InitApplication(hInstance))    /* Initialize shared things */
  11.             return (FALSE);        /* Exits if unable to initialize */
  12.  
  13.     /* Perform initializations that apply to a specific instance */
  14.     if (!InitInstance(hInstance, nCmdShow))
  15.         return (FALSE);
  16.  
  17.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  18.  
  19. //    while (GetMessage(&msg,        /* message structure */
  20. //        NULL,    /* handle of window receiving the message */
  21. //        NULL,    /* lowest message to examine    */
  22. //        NULL))    /* highest message to examine    */
  23. //    {
  24. //        TranslateMessage(&msg);        /* Translates virtual key codes          */
  25. //        DispatchMessage(&msg);        /* Dispatches message to window          */
  26. //    }
  27.  
  28.    while (TRUE)
  29.      {
  30.      if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  31.           {
  32.           if (msg.message == WM_QUIT)
  33.                break ;
  34.  
  35.           TranslateMessage (&msg) ;
  36.           DispatchMessage (&msg) ;
  37.           }
  38.      else
  39.           {
  40.                 DrawBall() ;
  41.           }
  42.      }
  43.  
  44.  
  45.  
  46.     return (msg.wParam);        /* Returns the value from PostQuitMessage */
  47. }
  48.  
  49.  
  50.  
  51. BOOL InitApplication(HANDLE hInstance)
  52. {
  53.     WNDCLASS  wc;
  54.  
  55.     /* Fill in window class structure with parameters that describe the
  56.         main window. */
  57.  
  58.     wc.style = NULL;                /* Class style(s) */
  59.     wc.lpfnWndProc = MainWndProc;    /* Function to retrieve messages for  */
  60.                                     /* windows of this class */
  61.     wc.cbClsExtra = 0;                /* No per-class extra data */
  62.     wc.cbWndExtra = 0;                /* No per-window extra data */
  63.     wc.hInstance = hInstance;        /* Application that owns the class */
  64.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  65.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  66.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  67.     wc.lpszMenuName =  "BallMenu";    /* Name of menu resource in .RC file. */
  68.     wc.lpszClassName = "BallWClass"; /* Name used in call to CreateWindow. */
  69.  
  70.     /* Register the window class and return success/failure code. */
  71.     return (RegisterClass(&wc));
  72. }
  73.  
  74.  
  75. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  76. {
  77.  
  78.  
  79.     /* Save the instance handle in static variable, which will be used in
  80.        many subsequent calls from this application to Windows.    */
  81.  
  82.     hInst = hInstance;
  83.  
  84.     /* Create a main window for this application instance.    */
  85.  
  86.     hWnd = CreateWindow(
  87.         "BallWClass",    /* See RegisterClass() call */
  88.         "Peek Message Ball",    /* Text for window title bar */
  89.         WS_OVERLAPPEDWINDOW,    /* Window style */
  90.         CW_USEDEFAULT,    /* Default horizontal position */
  91.         CW_USEDEFAULT,    /* Default vertical position */
  92.         CW_USEDEFAULT,    /* Default width */
  93.         CW_USEDEFAULT,    /* Default height */
  94.         NULL,    /* Overlapped windows have no parent */
  95.         NULL,    /* Use the window class menu */
  96.         hInstance,    /* This instance owns this window */
  97.         NULL    /* Pointer not needed */
  98.     );
  99.  
  100.     /* If window could not be created, return "failure" */
  101.  
  102.     if (!hWnd)
  103.         return (FALSE);
  104.  
  105.  
  106.  
  107.  
  108.     /* Make the window visible; update its client area; and return "success" */
  109.  
  110.     ShowWindow(hWnd, nCmdShow);        /* Show the window */
  111.     UpdateWindow(hWnd);     /* Sends WM_PAINT message */
  112.     return (TRUE);    /* Returns the value from PostQuitMessage */
  113. }
  114.  
  115.  
  116.  
  117.  
  118. LONG FAR PASCAL MainWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  119. {
  120.     FARPROC lpProcAbout;          /* pointer to the "About" function */
  121.     TEXTMETRIC tm;
  122.     HMENU hMenu;
  123.  
  124.     switch (message)
  125.     {
  126.         case WM_CREATE:
  127.            hbBall=LoadBitmap(hInst, "ball");
  128.            GetObject(hbBall,16, (LPSTR) &bmBall);
  129.            nWidth=bmBall.bmWidth;
  130.             nHeight=bmBall.bmHeight;
  131.  
  132.             break;
  133.  
  134.         case WM_COMMAND:    /* message: command from application menu */
  135.             switch (wParam)
  136.             {
  137.                 case IDM_ABOUT:
  138.                     lpProcAbout = MakeProcInstance(About, hInst);
  139.  
  140.                     DialogBox(hInst,         /* current instance         */
  141.                         "AboutBox",             /* resource to use         */
  142.                         hWnd,             /* parent handle         */
  143.                         lpProcAbout);         /* About() instance address */
  144.  
  145.                     FreeProcInstance(lpProcAbout);
  146.                     break;
  147.                 case IDM_SPEED1:
  148.                     wPrevItem=wPrevSpeed;
  149.                     wPrevSpeed=wParam;
  150.                     AnimeStep=1;
  151.                     break;
  152.                 case IDM_SPEED2:
  153.                     wPrevItem=wPrevSpeed;
  154.                     wPrevSpeed=wParam;
  155.                     AnimeStep=5;
  156.                     break;
  157.                 case IDM_SPEED3:
  158.                     wPrevItem=wPrevSpeed;
  159.                     wPrevSpeed=wParam;
  160.                     AnimeStep=10;
  161.                     break;
  162.                 case IDM_SPEED4:
  163.                     wPrevItem=wPrevSpeed;
  164.                     wPrevSpeed=wParam;
  165.                     AnimeStep=50;
  166.                     break;
  167.             }
  168.             if (wParam!=IDM_ABOUT){
  169.             CheckMenuItem(GetMenu(hWnd), wPrevItem, MF_UNCHECKED);
  170.             CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  171.             }
  172.             break;
  173.  
  174.         case WM_TIMER:
  175.         //    MoveBall();
  176.                break;
  177.  
  178.         case WM_PAINT:
  179.             {
  180.             hDC = BeginPaint (hWnd, &ps);
  181.             hMemoryDC = CreateCompatibleDC(hDC);
  182.             SelectObject(hMemoryDC, hbBall);
  183.             BitBlt(hDC, x, y,
  184.                 bmBall.bmWidth, bmBall.bmHeight, hMemoryDC, 0, 0, SRCCOPY);
  185.             DeleteDC(hMemoryDC);
  186.               EndPaint(hWnd, &ps);
  187.             }
  188.             break;
  189.  
  190.  
  191.         case WM_DESTROY:          /* message: window being destroyed */
  192.             DeleteObject(hbBall);
  193.             PostQuitMessage(0);
  194.             break;
  195.  
  196.         default:              /* Passes it on if unproccessed     */
  197.             return (DefWindowProc(hWnd, message, wParam, lParam));
  198.     }
  199.     return (NULL);
  200. }
  201.  
  202.  
  203.  
  204. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  205. {
  206.     switch (message)
  207.     {
  208.         case WM_INITDIALOG:        /* message: initialize dialog box */
  209.             return (TRUE);
  210.  
  211.         case WM_COMMAND:    /* message: received a command */
  212.             if (wParam == IDOK || wParam == IDCANCEL)
  213.             {
  214.                 EndDialog(hDlg, TRUE);    /* Exits the dialog box */
  215.                 return (TRUE);
  216.             }
  217.             break;
  218.     }
  219.     return (FALSE);        /* Didn't process a message */
  220. }
  221.  
  222.  
  223. void DrawBall(void)
  224. {
  225.     MoveBall();
  226.  
  227.     DrawBitmap(hbBall, x, y);
  228.         EraseOldBall();
  229.     return;
  230. }
  231.  
  232.  
  233.  
  234. void MoveBall(void)
  235. {
  236.     GetClientRect(hWnd, &r);
  237.  
  238.     rect1.left  = x;
  239.     rect1.top   = y;
  240.     rect1.right = x+nWidth;
  241.     rect1.bottom= y+nHeight;
  242.  
  243.     if (x<=r.left) {
  244.            x=r.left;
  245.            xdirection=RIGHT;
  246.     }
  247.  
  248.     if (x>=(r.right-nWidth)) {
  249.         x=(r.right-nWidth);
  250.         xdirection=LEFT;
  251.     }
  252.  
  253.     if (y<=r.top){
  254.         y=r.top;
  255.         ydirection=DOWN;
  256.     }
  257.  
  258.     if (y>=(r.bottom-nHeight)) {
  259.         y=r.bottom-nHeight;
  260.         ydirection=UP;
  261.     }
  262.  
  263.  
  264.     if (xdirection==RIGHT){
  265.         x+=AnimeStep;
  266.     }
  267.     else {
  268.         x-=AnimeStep;
  269.     }
  270.  
  271.     if (ydirection==DOWN) {
  272.         y+=AnimeStep;
  273.     }
  274.     else {
  275.         y-=AnimeStep;
  276.     }
  277.  
  278.     rect2.left=x;
  279.     rect2.top=y;
  280.     rect2.right=x+nWidth;
  281.     rect2.bottom=y+nHeight;
  282.  
  283.  
  284.     return;
  285. }
  286.  
  287.  
  288. void DrawBitmap (HBITMAP hBitmap, int xStart, int yStart)
  289. {
  290.     HDC hdc;
  291.     BITMAP bm ;
  292.     HDC hdcMem ;
  293.  
  294.     hdc = GetDC(hWnd);
  295.     hdcMem = CreateCompatibleDC (hdc) ;
  296.     SelectObject (hdcMem, hBitmap) ;
  297.  
  298.     GetObject (hBitmap, sizeof (BITMAP), (LPSTR)&bm) ;
  299.  
  300.     BitBlt (hdc, xStart, yStart, bm.bmWidth, bm.bmHeight,
  301.     hdcMem, 0, 0, SRCCOPY) ;
  302.  
  303.     DeleteDC (hdcMem) ;
  304.     ReleaseDC (hWnd, hdc);
  305. }
  306.  
  307.  
  308.  
  309. void EraseOldBall(void)
  310. {
  311.     HDC hdc;
  312.     HBRUSH hbrush;
  313.     hdc = GetDC(hWnd);
  314.     hbrush=GetStockObject(WHITE_BRUSH);
  315.  
  316.     if (IntersectRect(&rect3,&rect1,&rect2))
  317.     {
  318.         if (xdirection==RIGHT)
  319.         {
  320.             if (ydirection==DOWN)
  321.             {
  322.                 rect4.top=rect1.top;
  323.                 rect4.left=rect1.left;
  324.                 rect4.bottom=rect3.bottom;
  325.                 rect4.right=rect3.left;
  326.  
  327.                 rect5.top=rect1.top;
  328.                 rect5.left=rect3.left;
  329.                 rect5.bottom=rect3.top;
  330.                 rect5.right=rect3.right;
  331.             }
  332.             else
  333.             {
  334.                 rect4.top=rect1.top;
  335.                 rect4.left=rect1.left;
  336.                 rect4.bottom=rect1.bottom;
  337.                 rect4.right=rect3.left;
  338.  
  339.                 rect5.top=rect3.bottom;
  340.                 rect5.left=rect3.left;
  341.                 rect5.bottom=rect1.bottom;
  342.                 rect5.right=rect1.right;
  343.             }
  344.         }
  345.         else
  346.         {
  347.             if (ydirection==DOWN)
  348.             {
  349.                 rect4.top=rect1.top;
  350.                 rect4.left=rect1.left;
  351.                 rect4.bottom=rect3.top;
  352.                 rect4.right=rect1.right;
  353.  
  354.                 rect5.top=rect3.top;
  355.                 rect5.left=rect3.right;
  356.                 rect5.bottom=rect1.bottom;
  357.                 rect5.right=rect1.right;
  358.             }
  359.             else
  360.             {
  361.                 rect4.top=rect3.top;
  362.                 rect4.left=rect3.right;
  363.                 rect4.bottom=rect1.bottom;
  364.                 rect4.right=rect1.right;
  365.  
  366.                 rect5.top=rect3.bottom;
  367.                 rect5.left=rect3.left;
  368.                 rect5.bottom=rect1.bottom;
  369.                 rect5.right=rect3.right;
  370.             }
  371.         }
  372.  
  373.         FillRect(hdc, &rect4, hbrush);
  374.         FillRect(hdc, &rect5, hbrush);
  375.     }
  376.     else
  377.     {
  378.         rect4.top=rect1.top;
  379.         rect4.left=rect1.left;
  380.         rect4.bottom=rect1.bottom;
  381.         rect4.right=rect1.right;
  382.  
  383.         rect5.top=0;
  384.         rect5.left=0;
  385.         rect5.bottom=0;
  386.         rect5.right=0;
  387.  
  388.         FillRect(hdc, &rect4, hbrush);
  389.     }
  390.  
  391.    DeleteObject(hbrush);
  392.     ReleaseDC(hWnd, hdc);
  393. }
  394.  
  395.