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