home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_06 / 3n06043a < prev    next >
Text File  |  1991-09-25  |  6KB  |  234 lines

  1.                    /* Listing 3 */
  2.  
  3. /*****************************************************
  4.            File Name: C_W_DEMO.C
  5.        Expanded Name: Center Window Demo
  6.          Description: Center Window Demo Program
  7.         Program List: C_W_DEMO.C CNTR_WND.C
  8. Global Function List: CenterWindow
  9. Static Function List: CenterWindowDialog
  10.     Local Macro List: 
  11.          Global Data: 
  12.          Static Data: _hInstance _hWnd
  13.          Portability: MS Windows, Any memory model,
  14.                       Any windows compatable C Compiler
  15. ******************************************************/
  16. /* MS Windows */
  17. #include <windows.h>
  18.  
  19. /* Types and Prototypes */
  20. #include <cntr_wnd.h>
  21.  
  22. /* Own */
  23. #include <c_w_demo.h>
  24.  
  25. /* Prototypes of functions called only by windows. */
  26. LONG FAR PASCAL CenterWindowDemoProc(
  27.       HWND hWnd, WORD iMessage, WORD wParam,
  28.       LONG lParam );
  29.  
  30. BOOL FAR PASCAL CenterWindowDialogProc(
  31.       HWND hDlg, WORD iMessage, WORD wParam,
  32.       LONG lParam );
  33.  
  34. /* static data */
  35. static HWND _hWnd;
  36. static HANDLE _hInstance;
  37.  
  38. /*****************************************************
  39.        Name: WinMain
  40. Description: Program entry point.
  41. *****************************************************/
  42. int PASCAL WinMain( HANDLE hInstance,
  43.       HANDLE hPrevInstance, LPSTR lpszCmdParam,
  44.       int nCmdShow )
  45.    {
  46.  
  47.    MSG Message;
  48.    WNDCLASS WndClass;
  49.    char *CenterWindowDemoName = "CenterWindowDemo";
  50.  
  51.    _hInstance = hInstance;
  52.  
  53.    if ( !hPrevInstance )
  54.       {
  55.       WndClass.style = CS_HREDRAW | CS_VREDRAW;
  56.       WndClass.lpfnWndProc = CenterWindowDemoProc;
  57.       WndClass.cbClsExtra = 0;
  58.       WndClass.cbWndExtra = 0;
  59.       WndClass.hInstance = _hInstance;
  60.       WndClass.hIcon = LoadIcon( _hInstance,
  61.             CenterWindowDemoName );
  62.       WndClass.hCursor =
  63.             LoadCursor( NULL, IDC_ARROW );
  64.       WndClass.hbrBackground = COLOR_WINDOW + 1;
  65.       WndClass.lpszMenuName =
  66.             CenterWindowDemoName;
  67.       WndClass.lpszClassName =
  68.             CenterWindowDemoName;
  69.       if ( RegisterClass( &WndClass ) == FALSE )
  70.          {
  71.          MessageBeep( 0 );
  72.          return ( FALSE );
  73.          }
  74.       }
  75.  
  76.    /* Create the window with default pos. and size */
  77.    _hWnd = CreateWindow( CenterWindowDemoName,
  78.          "Center Window Demo",
  79.          WS_OVERLAPPEDWINDOW | WS_VISIBLE |
  80.          WS_VSCROLL | WS_HSCROLL,
  81.          CW_USEDEFAULT, CW_USEDEFAULT,
  82.          CW_USEDEFAULT, CW_USEDEFAULT,
  83.          NULL, NULL, _hInstance, NULL );
  84.  
  85.    if ( _hWnd == NULL )
  86.       {
  87.       /* If window could not be created, return. */
  88.       MessageBeep( 0 );
  89.       return ( FALSE );
  90.       }
  91.  
  92.    ShowWindow( _hWnd, nCmdShow );
  93.    UpdateWindow( _hWnd );
  94.  
  95.    while ( GetMessage( &Message, NULL, NULL, NULL ) )
  96.       {
  97.       TranslateMessage( &Message );
  98.       DispatchMessage( &Message );
  99.       }
  100.  
  101.    return ( (int)Message.wParam );
  102.  
  103.    }   /* function WinMain */
  104.  
  105.  
  106. /*****************************************************
  107.        Name: CenterWindowDemoProc
  108. Description: Window Procedure for center window demo
  109.              program.  This is called by Windows only.
  110. *****************************************************/
  111. long FAR PASCAL CenterWindowDemoProc( HWND hWnd,
  112.       WORD iMessage, WORD wParam, LONG lParam )
  113.    {
  114.    switch ( iMessage )
  115.       {
  116.       case WM_CREATE:
  117.          {
  118.          CenterWindow( NULL, hWnd, FALSE );
  119.          break;
  120.          }
  121.       case WM_COMMAND:
  122.          {
  123.          switch ( wParam )
  124.             {
  125.             case IDM_CENTER_MAIN:
  126.                {
  127.                CenterWindow( NULL, hWnd,
  128.                      TRUE );
  129.                break;
  130.                }
  131.             case IDM_DIALOG:
  132.                {
  133.                FARPROC lpfCenterWindowDialog;
  134.  
  135.                lpfCenterWindowDialog =
  136.                      MakeProcInstance(
  137.                      CenterWindowDialogProc,
  138.                      _hInstance );
  139.                DialogBox( _hInstance,
  140.                      "CenterWindowDialog",
  141.                      hWnd,
  142.                      lpfCenterWindowDialog );
  143.                FreeProcInstance(
  144.                      lpfCenterWindowDialog );
  145.                break;
  146.                }
  147.             case IDM_EXIT:
  148.                {
  149.                SendMessage( hWnd,
  150.                      WM_CLOSE, 0, 0L );
  151.                break;
  152.                }
  153.             default:
  154.                {
  155.                break;
  156.                }
  157.             }
  158.          break;
  159.          }
  160.       case WM_DESTROY:
  161.          {
  162.          PostQuitMessage( 0 );
  163.          break;
  164.          }
  165.       default:
  166.          {
  167.          return ( DefWindowProc( hWnd, iMessage,
  168.                wParam, lParam ) );
  169.          }
  170.       }   /* switch iMessage */
  171.  
  172.    return ( FALSE );
  173.  
  174.    }   /* function CenterWindowDemoProc */
  175.  
  176.  
  177. /*****************************************************
  178.        Name: CenterWindow
  179. Description: Dialog Procedure for center window demo.
  180.              Processes commands to center the dialog
  181.              box within the parent window or within
  182.              the desktop.  This function is called
  183.              by Windows only.
  184. *****************************************************/
  185. BOOL FAR PASCAL CenterWindowDialogProc( HWND hDlg,
  186.       WORD Message, WORD wParam, LONG lParam )
  187.    {
  188.  
  189.    switch ( Message )
  190.       {
  191.       case WM_INITDIALOG:
  192.          {
  193.          CenterWindow( _hWnd, hDlg, FALSE );
  194.          return ( TRUE );
  195.          }
  196.       case WM_COMMAND:
  197.          {
  198.          switch ( wParam )
  199.             {
  200.             default:
  201.                {
  202.                break;
  203.                }
  204.             case IDCANCEL:
  205.             case IDOK:
  206.                {
  207.                EndDialog( hDlg, FALSE );
  208.                return ( TRUE );
  209.                }
  210.             case IDD_PARENT:
  211.                {
  212.                CenterWindow( _hWnd, hDlg,
  213.                      TRUE );
  214.                break;
  215.                }
  216.             case IDD_DESKTOP:
  217.                {
  218.                CenterWindow( NULL, hDlg,
  219.                      TRUE );
  220.                break;
  221.                }
  222.             }   /* switch wParam */
  223.          break;
  224.          }
  225.       default:
  226.          {
  227.          break;
  228.          }
  229.       }   /* switch message */
  230.  
  231.    return ( FALSE );
  232.  
  233.    }   /* function CenterWindowDialogProc */
  234.