Scroll_Window.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 8k
Development Platform:

C++ Builder

  1. #include <windows.h>  
  2. #include "Scroll_Window.h" 
  3. #if defined (WIN32)
  4. #define IS_WIN32 TRUE
  5. #else
  6. #define IS_WIN32 FALSE
  7. #endif
  8. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  9. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  10. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  11. HINSTANCE hInst;   // current instance
  12. LPCTSTR lpszAppName  = "MyApp";
  13. LPCTSTR lpszTitle    = "My Application"; 
  14. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  15. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                       LPTSTR lpCmdLine, int nCmdShow)
  17. {
  18.    MSG      msg;
  19.    HWND     hWnd; 
  20.    WNDCLASS wc;
  21.    // Register the main application window class.
  22.    //............................................
  23.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  24.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  25.    wc.cbClsExtra    = 0;                      
  26.    wc.cbWndExtra    = 0;                      
  27.    wc.hInstance     = hInstance;              
  28.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  29.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  30.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  31.    wc.lpszMenuName  = lpszAppName;              
  32.    wc.lpszClassName = lpszAppName;              
  33.    if ( IS_WIN95 )
  34.    {
  35.       if ( !RegisterWin95( &wc ) )
  36.          return( FALSE );
  37.    }
  38.    else if ( !RegisterClass( &wc ) )
  39.       return( FALSE );
  40.    hInst = hInstance; 
  41.    // Create the main application window.
  42.    //....................................
  43.    hWnd = CreateWindow( lpszAppName, 
  44.                         lpszTitle,    
  45.                         WS_OVERLAPPEDWINDOW | WS_VSCROLL, 
  46.                         CW_USEDEFAULT, 0, 
  47.                         CW_USEDEFAULT, 0,  
  48.                         NULL,              
  49.                         NULL,              
  50.                         hInstance,         
  51.                         NULL               
  52.                       );
  53.    if ( !hWnd ) 
  54.       return( FALSE );
  55.    ShowWindow( hWnd, nCmdShow ); 
  56.    UpdateWindow( hWnd );         
  57.    while( GetMessage( &msg, NULL, 0, 0) )   
  58.    {
  59.       TranslateMessage( &msg ); 
  60.       DispatchMessage( &msg );  
  61.    }
  62.    return( msg.wParam ); 
  63. }
  64. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  65. {
  66.    WNDCLASSEX wcex;
  67.    wcex.style         = lpwc->style;
  68.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  69.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  70.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  71.    wcex.hInstance     = lpwc->hInstance;
  72.    wcex.hIcon         = lpwc->hIcon;
  73.    wcex.hCursor       = lpwc->hCursor;
  74.    wcex.hbrBackground = lpwc->hbrBackground;
  75.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  76.    wcex.lpszClassName = lpwc->lpszClassName;
  77.    // Added elements for Windows 95.
  78.    //...............................
  79.    wcex.cbSize = sizeof(WNDCLASSEX);
  80.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  81.                             IMAGE_ICON, 16, 16,
  82.                             LR_DEFAULTCOLOR );
  83.    return RegisterClassEx( &wcex );
  84. }
  85. VOID Scroll( HWND hWnd, int* pnCurPos, WORD wScroll )
  86. {
  87.    SCROLLINFO si;
  88.    // Use GetScrollInfo() to get information
  89.    // about the scroll bar.
  90.    //.......................................
  91.    si.cbSize = sizeof( SCROLLINFO );
  92.    si.fMask  = SIF_PAGE | SIF_RANGE | SIF_POS;
  93.    GetScrollInfo( hWnd, SB_VERT, &si );
  94.    switch( wScroll )
  95.    {
  96.       case SB_LINEDOWN :
  97.               if ( *pnCurPos <= (int)(si.nMax - si.nPage) )
  98.                  *pnCurPos += 1;
  99.               break;
  100.       case SB_LINEUP :
  101.               if ( *pnCurPos > 0 )
  102.                  *pnCurPos -= 1;
  103.               break;
  104.    }
  105.    if ( si.nPos != *pnCurPos )
  106.    {
  107.       RECT cltRect;
  108.       GetClientRect( hWnd, &cltRect );
  109.       ScrollWindowEx( hWnd, 0, (si.nPos-*pnCurPos)*20, NULL,
  110.                      &cltRect, NULL, NULL, SW_INVALIDATE );
  111.       si.fMask = SIF_POS;
  112.       si.nPos  = *pnCurPos;
  113.       SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  114.    }
  115. }
  116. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  117. {
  118. static int  nDspLines;
  119. static int  nNumItems = 0;
  120. static int  nCurPos   = 0;
  121. static char szBuf[10];
  122.    switch( uMsg )
  123.    {
  124.       case WM_CREATE :
  125.               ShowScrollBar( hWnd, SB_VERT, TRUE );
  126.               break;
  127.          // Every time the window is sized, re-calculate the number of lines
  128.          // the client area can display and set the scroll bar accordingly.
  129.          //................................................................
  130.       case WM_SIZE :
  131.               {
  132.                  RECT rect;
  133.                  GetClientRect( hWnd, &rect );
  134.                  nDspLines = rect.bottom / 20;
  135.                  if ( nDspLines < nNumItems ) 
  136.                  {
  137.                     SCROLLINFO si;
  138.                     si.cbSize = sizeof( SCROLLINFO );
  139.                     si.fMask  = SIF_POS | SIF_RANGE | SIF_PAGE;
  140.                     si.nMin   = 0;
  141.                     si.nMax   = nNumItems-1;
  142.                     si.nPage  = nDspLines;
  143.                     si.nPos   = nCurPos;
  144.                     EnableScrollBar( hWnd, SB_VERT, ESB_ENABLE_BOTH );
  145.                     SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  146.                  }
  147.                  else
  148.                     EnableScrollBar( hWnd, SB_VERT, ESB_DISABLE_BOTH );
  149.               }
  150.               break;
  151.                
  152.       case WM_PAINT :
  153.               {
  154.                  PAINTSTRUCT ps;
  155.                  int         i,j;
  156.                  int         nNumPaint;
  157.                  nNumPaint = min( nCurPos+nDspLines, nNumItems );
  158.                  BeginPaint( hWnd, &ps );
  159.                  for ( j=0,i=nCurPos; i<nNumPaint; i++,j++ )
  160.                  {
  161.                     itoa( i, szBuf, 10 );
  162.                     TextOut( ps.hdc, 10, j*20, "Line of Text:", 13 );
  163.                     TextOut( ps.hdc, 90, j*20, szBuf, strlen( szBuf ) );
  164.                  }
  165.                   
  166.                  EndPaint( hWnd, &ps );
  167.               }
  168.               break;
  169.       case WM_VSCROLL :
  170.               Scroll( hWnd, &nCurPos, LOWORD( wParam ) );
  171.               break;
  172.       case WM_COMMAND :
  173.               switch( LOWORD( wParam ) )
  174.               {
  175.                  case IDM_TEST :
  176.                         if ( nDspLines == nNumItems ) 
  177.                            EnableScrollBar( hWnd, SB_VERT, ESB_ENABLE_BOTH );
  178.                         nNumItems++;
  179.                         if ( nDspLines < nNumItems )
  180.                         {
  181.                            SCROLLINFO si;
  182.                            si.cbSize = sizeof( SCROLLINFO );
  183.                            si.fMask  = SIF_RANGE | SIF_PAGE;
  184.                            si.nMin   = 0;
  185.                            si.nMax   = nNumItems-1;
  186.                            si.nPage  = nDspLines;
  187.                            SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  188.                         }
  189.                         InvalidateRect( hWnd, NULL, FALSE );
  190.                         break;
  191.                  case IDM_ABOUT :
  192.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  193.                         break;
  194.                  case IDM_EXIT :
  195.                         DestroyWindow( hWnd );
  196.                         break;
  197.               }
  198.               break;
  199.       
  200.       case WM_DESTROY :
  201.               PostQuitMessage(0);
  202.               break;
  203.       default :
  204.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  205.    }
  206.    return( 0L );
  207. }
  208. LRESULT CALLBACK About( HWND hDlg,           
  209.                         UINT message,        
  210.                         WPARAM wParam,       
  211.                         LPARAM lParam)
  212. {
  213.    switch (message) 
  214.    {
  215.        case WM_INITDIALOG: 
  216.                return (TRUE);
  217.        case WM_COMMAND:                              
  218.                if (   LOWORD(wParam) == IDOK         
  219.                    || LOWORD(wParam) == IDCANCEL)    
  220.                {
  221.                        EndDialog(hDlg, TRUE);        
  222.                        return (TRUE);
  223.                }
  224.                break;
  225.    }
  226.    return (FALSE); 
  227. }