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

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