home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / scroll / scroll.lst < prev    next >
Encoding:
File List  |  1988-08-11  |  18.4 KB  |  482 lines

  1.  
  2.  
  3.                                                                        PAGE   1
  4.                                                                        07-18-88
  5.                                                                        16:13:30
  6.  
  7.  Line#  Source Line                           Microsoft C Compiler Version 5.10
  8.  
  9.       1  /* SCROLL.C                             */
  10.       2  /* This program illistrates the use of ScrollDC and ScrollWindow */
  11.       3  /* as well as many other Windows functions. ScrollDC is used to  */
  12.       4  /* "Bounce" individual balls, one at a time, from the top of the */
  13.       5  /* display to the bottom of the display. ScrollWindow is used to */
  14.       6  /* "Raise" all the balls from the bottom of the display to the     */
  15.       7  /* top of the display. This program gets the resolution of the     */
  16.       8  /* screen to determine what would be a good shape for the ball     */
  17.       9  /* as well as to deternime how fast to scroll the ball. We also  */
  18.      10  /* get the size of the client area so that we can deternime how  */
  19.      11  /* many balls to draw on the screen. I decided the height of the */
  20.      12  /* ball should be 1/10 the height of the client area and the it  */
  21.      13  /* should be drawn 1/10 of the way down the client area and the  */
  22.      14  /* same distance over on the client area. The other balls are     */
  23.      15  /* drawn similarly. Enjoy .....................................  */
  24.      16  
  25.      17  #include <windows.h>
  26.      18  #include "scroll.h"
  27.      19  
  28.      20  
  29.      21  long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  30.      22  
  31.      23  static char szAppName [] = "Scroll" ;
  32.      24  
  33.      25  int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  34.      26       HANDLE      hInstance, hPrevInstance ;
  35.      27       LPSTR       lpszCmdLine ;
  36.      28       int         nCmdShow ;
  37.      29       {
  38.      30  
  39.      31       HWND        hWnd ;
  40.      32       MSG         msg ;
  41.      33       WNDCLASS     wndclass ;
  42.      34  
  43.      35       if (!hPrevInstance) 
  44.      36            {
  45.      37            wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  46.      38            wndclass.lpfnWndProc   = WndProc ;
  47.      39            wndclass.cbClsExtra    = 0 ;
  48.      40            wndclass.cbWndExtra    = 0 ;
  49.      41            wndclass.hInstance     = hInstance ;
  50.      42        wndclass.hIcon     = LoadIcon (hInstance, szAppName) ;
  51.      43            wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  52.      44            wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  53.      45        wndclass.lpszMenuName  = szAppName;
  54.      46            wndclass.lpszClassName = szAppName ;
  55.      47  
  56.      48            if (!RegisterClass (&wndclass))
  57.      49                 return FALSE ;
  58.      50            }
  59.      51  
  60.      52       hWnd = CreateWindow (szAppName,         /* window class        */
  61.      53              "Scroll",     /* window caption        */
  62.      54                      WS_OVERLAPPEDWINDOW,     /* window style          
  63.            */
  64.      55                      CW_USEDEFAULT,           /* initial x position    
  65.  
  66.  
  67.                                                                        PAGE   2
  68.                                                                        07-18-88
  69.                                                                        16:13:30
  70.  
  71.  Line#  Source Line                           Microsoft C Compiler Version 5.10
  72.  
  73.            */
  74.      56                      0,                       /* initial y position    
  75.            */
  76.      57                      CW_USEDEFAULT,           /* initial x size        
  77.            */
  78.      58                      0,                       /* initial y size        
  79.            */
  80.      59                      NULL,                    /* parent window handle  
  81.            */
  82.      60                      NULL,                    /* window menu handle    
  83.            */
  84.      61                      hInstance,               /* program instance handl
  85.          e */
  86.      62                      NULL) ;                  /* create parameters     
  87.            */
  88.      63  
  89.      64       ShowWindow (hWnd, nCmdShow) ;
  90.      65       UpdateWindow (hWnd) ;
  91.      66  
  92.      67       while (GetMessage (&msg, NULL, 0, 0))
  93.      68            {
  94.      69            TranslateMessage (&msg) ;
  95.      70            DispatchMessage (&msg) ;
  96.      71            }
  97.      72       return msg.wParam ;
  98.      73       }
  99.  
  100.  
  101. WinMain  Local Symbols
  102.  
  103. Name                      Class   Type              Size   Offset  Register
  104.  
  105. wndclass. . . . . . . . . auto                             -002e 
  106. msg . . . . . . . . . . . auto                             -0014 
  107. hWnd. . . . . . . . . . . auto                             -0002 
  108. nCmdShow. . . . . . . . . param                             0004
  109. lpszCmdLine . . . . . . . param                             0006
  110. hPrevInstance . . . . . . param                             000a
  111. hInstance . . . . . . . . param                             000c
  112.  
  113.      74  
  114.      75  void FAR PASCAL StopOne(){
  115.      76      return;
  116.      77      }
  117.      78  
  118.      79  
  119.      80  long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  120.      81       HWND        hWnd ;
  121.      82       unsigned        iMessage ;
  122.      83       WORD        wParam ;
  123.      84       LONG        lParam ;
  124.      85       {
  125.      86       static int   xClient, yClient ;
  126.      87       static int   xScreen, yScreen ;
  127.      88       static short   numBalls , bounced ;
  128.      89       static short   ScrollRate ;
  129.  
  130.  
  131.                                                                        PAGE   3
  132.                                                                        07-18-88
  133.                                                                        16:13:30
  134.  
  135.  Line#  Source Line                           Microsoft C Compiler Version 5.10
  136.  
  137.      90       HDC        hDC ;
  138.      91       PAINTSTRUCT    ps ;
  139.      92       RECT        ClientRect ;
  140.      93       RECT        DcRect ;
  141.      94       RECT        Ball ;
  142.      95       RECT        NewBall ;
  143.      96       RECT        ScrollBox ;
  144.      97       short        i, j, up, down ;
  145.      98       char szBuffer[80] ;
  146.      99  
  147.     100       switch (iMessage)
  148.     101        {
  149.     102        case WM_CREATE:
  150.     103  
  151.     104  /* Get the resolution of the Screen */
  152.     105  
  153.     106             xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  154.     107             yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  155.     108  
  156.     109  /* Get the size of the client area initially */
  157.     110  
  158.     111             GetClientRect (hWnd, &ClientRect) ;
  159.     112  
  160.     113  /* and save it in static variables */
  161.     114  
  162.     115             yClient = ClientRect.bottom - ClientRect.top ;
  163.     116             xClient = ClientRect.right - ClientRect.left  ;
  164.     117  
  165.     118  /* Set scroll speed for different resolutions */
  166.     119  
  167.     120             if (yClient <= 50) ScrollRate = 1 ;
  168.     121             if (yClient > 100)  ScrollRate = 3 ;
  169.     122             if (yClient > 175)  ScrollRate = 5 ;
  170.     123  
  171.     124             break ;
  172.     125  
  173.     126  
  174.     127        case WM_SIZE:
  175.     128  
  176.     129  /* Update the size of the client area */
  177.     130  
  178.     131             yClient = HIWORD (lParam) ;
  179.     132             xClient = LOWORD (lParam) ;
  180.     133  
  181.     134  /* Set scroll speed for different resolutions */
  182.     135  
  183.     136             if (yClient <= 50) ScrollRate = 1 ;
  184.     137             if (yClient > 100)  ScrollRate = 3 ;
  185.     138             if (yClient > 175)  ScrollRate = 5 ;
  186.     139  
  187.     140             break ;
  188.     141  
  189.     142  
  190.     143             break ;
  191.     144  
  192.     145        case WM_PAINT:
  193.  
  194.  
  195.                                                                        PAGE   4
  196.                                                                        07-18-88
  197.                                                                        16:13:30
  198.  
  199.  Line#  Source Line                           Microsoft C Compiler Version 5.10
  200.  
  201.     146  
  202.     147  /* Begin to paint the client area and redraw the balls */
  203.     148  
  204.     149             BeginPaint (hWnd, &ps);
  205.     150  
  206.     151  /* Initialize the size of the ball */
  207.     152  
  208.     153             Ball.top    = ( yClient / 10) ;
  209.     154             Ball.bottom = ( yClient / 5) ;
  210.     155             Ball.left   = ((Ball.bottom - Ball.top) * (xScreen / yScreen))
  211.           ;
  212.     156             Ball.right  = 2 * Ball.left ;
  213.     157  
  214.     158  /* Determine the number of balls that can fit on the screen */
  215.     159  
  216.     160             numBalls = ( xClient - (Ball.right - Ball.left) ) /
  217.     161                    ( 2 * (Ball.right - Ball.left)) ;
  218.     162  
  219.     163  /* And use this as a standard for drawing the balls */
  220.     164  
  221.     165             NewBall = Ball ;
  222.     166  
  223.     167             for (i = 0 ; i < numBalls; i ++ ) {
  224.     168  
  225.     169           Ellipse (ps.hdc, NewBall.left, NewBall.top,
  226.     170                  NewBall.right, NewBall.bottom);
  227.     171  
  228.     172  /* Update the position of the new ball */
  229.     173  
  230.     174           NewBall.left = NewBall.left +    2 * (Ball.right - Ball.left);
  231.     175           NewBall.right = NewBall.right + 2 * (Ball.right - Ball.left);
  232.     176  
  233.     177             }
  234.     178  
  235.     179             EndPaint (hWnd, &ps);
  236.     180  
  237.     181  /* Set bounced to false, signifing that the balls are on the
  238.     182            top of the display */
  239.     183  
  240.     184             bounced = 0 ;
  241.     185  
  242.     186             break ;
  243.     187  
  244.     188        case WM_COMMAND:
  245.     189  
  246.     190             switch (wParam)
  247.     191               {
  248.     192               case IDM_DC:
  249.     193  
  250.     194                if (!bounced) {
  251.     195  
  252.     196  /* Initialize the size of the ball */
  253.     197  
  254.     198                   Ball.top     = ( yClient / 10) ;
  255.     199                   Ball.bottom = ( yClient / 5) ;
  256.     200                   Ball.left     = ((Ball.bottom - Ball.top) * (xScreen / yScreen)) 
  257.  
  258.  
  259.                                                                        PAGE   5
  260.                                                                        07-18-88
  261.                                                                        16:13:30
  262.  
  263.  Line#  Source Line                           Microsoft C Compiler Version 5.10
  264.  
  265.          ;
  266.     201                   Ball.right  = 2 * Ball.left ;
  267.     202  
  268.     203  /* Initialize the scrolling area for one ball */
  269.     204  
  270.     205                   ScrollBox.top = 1 ;
  271.     206                   ScrollBox.left = Ball.left / 2 ;
  272.     207                   ScrollBox.bottom = yClient ;
  273.     208                   ScrollBox.right = ScrollBox.left + (2 * Ball.left) ;
  274.     209  
  275.     210                   hDC = GetDC (hWnd) ;
  276.     211                   for (i = 0 ; i < numBalls; i ++ ) {
  277.     212  
  278.     213                     up = Ball.bottom ;
  279.     214                     down = yClient - (2 * ScrollRate) ;
  280.     215  
  281.     216  /* Until the ball is done bouncing */
  282.     217  
  283.     218                     while (up < down ) {
  284.     219  
  285.     220  /* It goes down */
  286.     221  
  287.     222                   for (j = up ; j <= down ; j += ScrollRate ) {
  288.     223                     ScrollDC (hDC,0, ScrollRate,
  289.     224                             &ScrollBox,&ScrollBox,
  290.     225                             NULL,NULL);
  291.     226                   }
  292.     227  
  293.     228  /* and won't go up quit so high */
  294.     229                   up = up + ((down - up) / 3) + 1 ;
  295.     230                   /*  MessageBeep (0); */
  296.     231  
  297.     232  
  298.     233  /* and now it bounces up */
  299.     234                   for (j = up ; j <= down ; j += ScrollRate ) {
  300.     235                     ScrollDC (hDC,0, - ScrollRate,
  301.     236                             &ScrollBox,&ScrollBox,
  302.     237                             NULL,NULL);
  303.     238                   }
  304.     239                     }
  305.     240  
  306.     241  /* and bounces down for the last time */
  307.     242  
  308.     243                     for (j = up ; j <= down ; j += ScrollRate ) {
  309.     244                   ScrollDC (hDC,0, 1,&ScrollBox,&ScrollBox,
  310.     245                           NULL,NULL);
  311.     246                     }
  312.     247  
  313.     248  /* Move to the next scrolling area */
  314.     249  
  315.     250                     ScrollBox.left = ScrollBox.left + ( 2 * Ball.left) ;
  316.     251                     ScrollBox.right = ScrollBox.right + (2 * Ball.left);
  317.     252                   }
  318.     253  
  319.     254                   ReleaseDC (hWnd, hDC) ;
  320.     255                   bounced = 1 ;
  321.  
  322.  
  323.                                                                        PAGE   6
  324.                                                                        07-18-88
  325.                                                                        16:13:30
  326.  
  327.  Line#  Source Line                           Microsoft C Compiler Version 5.10
  328.  
  329.     256                }
  330.     257  
  331.     258                break ;
  332.     259  
  333.     260               case IDM_WIN:
  334.     261  
  335.     262  /* If the balls are at the bottom of the display then */
  336.     263  
  337.     264                if (bounced) {
  338.     265  
  339.     266  /* Initialize how far they need to rise */
  340.     267  
  341.     268                  down = yClient - (2 * ScrollRate);
  342.     269                  up = (yClient / 5) ;
  343.     270  
  344.     271  /* And raise them to the top */
  345.     272  
  346.     273                  for (j = up ; j <= down ; j += ScrollRate ) {
  347.     274                    ScrollWindow (hWnd, 0,- ScrollRate,
  348.     275                             NULL, NULL) ;
  349.     276                  }
  350.     277  
  351.     278                  MessageBeep (0);
  352.     279  
  353.     280  /* Set bounced to false, signifing the balls are at the top of the dis
  354.          play */
  355.     281  
  356.     282                  bounced = 0 ;
  357.     283  
  358.     284                }
  359.     285  
  360.     286                break ;
  361.     287  
  362.     288               case IDM_EXIT:
  363.     289  
  364.     290                SendMessage (hWnd, WM_CLOSE, 0, 0L);
  365.     291                break;
  366.     292  
  367.     293               case IDM_HELP:
  368.     294  
  369.     295                MessageBox (hWnd, "Try Scrolling the DC ...",
  370.     296                     szAppName, MB_ICONASTERISK | MB_OK) ;
  371.     297                break;
  372.     298               case IDM_ABOUT:
  373.     299  
  374.     300                MessageBox (hWnd, "SCROLL DEMO.",
  375.     301                     szAppName, MB_ICONASTERISK | MB_OK) ;
  376.     302               default:
  377.     303                break;
  378.     304               }
  379.     305             break;
  380.     306  
  381.     307            case WM_DESTROY:
  382.     308                 PostQuitMessage (0) ;
  383.     309                 break ;
  384.     310  
  385.  
  386.  
  387.                                                                        PAGE   7
  388.                                                                        07-18-88
  389.                                                                        16:13:30
  390.  
  391.  Line#  Source Line                           Microsoft C Compiler Version 5.10
  392.  
  393.     311            default:
  394.     312                 return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  395.          
  396.     313            }
  397.     314       return 0L ;
  398.     315       }
  399.  
  400.  
  401. WndProc  Local Symbols
  402.  
  403. Name                      Class   Type              Size   Offset  Register
  404.  
  405. hDC . . . . . . . . . . . auto                             -00a4 
  406. ClientRect. . . . . . . . auto                             -00a2 
  407. Ball. . . . . . . . . . . auto                             -009a 
  408. j . . . . . . . . . . . . auto                             -0092 
  409. i . . . . . . . . . . . . auto                             -0090 
  410. ScrollBox . . . . . . . . auto                             -008e 
  411. down. . . . . . . . . . . auto                             -0086 
  412. szBuffer. . . . . . . . . auto                             -0084 
  413. DcRect. . . . . . . . . . auto                             -0034 
  414. NewBall . . . . . . . . . auto                             -002c 
  415. up. . . . . . . . . . . . auto                             -0024 
  416. ps. . . . . . . . . . . . auto                             -0022 
  417. lParam. . . . . . . . . . param                             0006
  418. wParam. . . . . . . . . . param                             000a
  419. iMessage. . . . . . . . . param                             000c
  420. hWnd. . . . . . . . . . . param                             000e
  421. bounced . . . . . . . . . static  int                  2    0000
  422. xClient . . . . . . . . . static  int                  2    0002
  423. xScreen . . . . . . . . . static  int                  2    0004
  424. yClient . . . . . . . . . static  int                  2    0006
  425. yScreen . . . . . . . . . static  int                  2    0008
  426. ScrollRate. . . . . . . . static  int                  2    000a
  427. numBalls. . . . . . . . . static  int                  2    000c
  428.  
  429.  
  430. Global Symbols
  431.  
  432. Name                      Class   Type              Size   Offset  
  433.  
  434. BeginPaint. . . . . . . . extern  far function       ***     ***
  435. CreateWindow. . . . . . . extern  far function       ***     ***
  436. DefWindowProc . . . . . . extern  far function       ***     ***
  437. DispatchMessage . . . . . extern  far function       ***     ***
  438. Ellipse . . . . . . . . . extern  far function       ***     ***
  439. EndPaint. . . . . . . . . extern  far function       ***     ***
  440. GetClientRect . . . . . . extern  far function       ***     ***
  441. GetDC . . . . . . . . . . extern  far function       ***     ***
  442. GetMessage. . . . . . . . extern  far function       ***     ***
  443. GetStockObject. . . . . . extern  far function       ***     ***
  444. GetSystemMetrics. . . . . extern  far function       ***     ***
  445. LoadCursor. . . . . . . . extern  far function       ***     ***
  446. LoadIcon. . . . . . . . . extern  far function       ***     ***
  447. MessageBeep . . . . . . . extern  far function       ***     ***
  448. MessageBox. . . . . . . . extern  far function       ***     ***
  449.  
  450.  
  451.                                                                        PAGE   8
  452.                                                                        07-18-88
  453.                                                                        16:13:30
  454.  
  455.                                               Microsoft C Compiler Version 5.10
  456.  
  457.  
  458. Global Symbols
  459.  
  460. Name                      Class   Type              Size   Offset  
  461.  
  462. PostQuitMessage . . . . . extern  far function       ***     ***
  463. RegisterClass . . . . . . extern  far function       ***     ***
  464. ReleaseDC . . . . . . . . extern  far function       ***     ***
  465. ScrollDC. . . . . . . . . extern  far function       ***     ***
  466. ScrollWindow. . . . . . . extern  far function       ***     ***
  467. SendMessage . . . . . . . extern  far function       ***     ***
  468. ShowWindow. . . . . . . . extern  far function       ***     ***
  469. StopOne . . . . . . . . . global  far function       ***    00e7
  470. TranslateMessage. . . . . extern  far function       ***     ***
  471. UpdateWindow. . . . . . . extern  far function       ***     ***
  472. WinMain . . . . . . . . . global  near function      ***    0000
  473. WndProc . . . . . . . . . global  far function       ***    00fa
  474. pLocalHeap. . . . . . . . common  near pointer         2     ***
  475. szAppName . . . . . . . . static  struct/array         7    0008
  476.  
  477. Code size = 04e2 (1250)
  478. Data size = 0035 (53)
  479. Bss size  = 000e (14)
  480.  
  481. No errors detected
  482.