home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sb_bc.ddi / BC / CH6 / WDLG-1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-21  |  3.5 KB  |  138 lines

  1. /***    Windows Application                    ***/
  2. /***           --- DIALOG BOX example      ***/
  3. /***                                           ***/
  4. /***         WDLG-1 includes                   ***/
  5. /***                (1) WDLG-1.C               ***/
  6. /***                (2) WDLG-1.DEF             ***/
  7. /***                (3) WDLG-1.RES             ***/
  8.  
  9. #pragma hdrfile "windows.sym"
  10. #include <windows.h>
  11. #pragma hdrstop
  12.  
  13. #define IDM_DEMO  101           // for menu item
  14.  
  15. #define ID_OK      1001         // for control
  16. #define ID_CANCEL  1002         // for control
  17.  
  18. // Declaration
  19.  
  20. void InitFirstInstance(HANDLE) ;
  21. void InitEachInstance(HANDLE, int) ;
  22. long FAR PASCAL AppWndProc (HWND, WORD, WORD, LONG) ;
  23.  
  24. BOOL FAR PASCAL DemoDlgProc(HWND, WORD, WORD, LONG) ;
  25.  
  26. static char  szAppName[12] = "WinDlg";
  27. static char  szMenuName[12] = "WinDlgMenu";
  28.  
  29. HWND  hwnd ;
  30.  
  31. // Definition
  32.  
  33. int PASCAL WinMain(HANDLE hInstance,
  34.            HANDLE hPrevInstance,
  35.            LPSTR  lpszCmdLine,
  36.            int    nCmdShow)
  37. { MSG msg ;
  38.  
  39.   if ( !hPrevInstance ) InitFirstInstance(hInstance) ;
  40.  
  41.   InitEachInstance(hInstance,nCmdShow) ;
  42.  
  43.   while ( GetMessage(&msg, NULL, 0, 0) )
  44.     { TranslateMessage(&msg) ;
  45.       DispatchMessage(&msg) ;
  46.     }
  47.   return msg.wParam ;
  48. }
  49.  
  50. void InitFirstInstance(HANDLE hInstance)
  51. { WNDCLASS wndclass ;
  52.  
  53.   wndclass.style      = CS_HREDRAW | CS_VREDRAW ;
  54.   wndclass.lpfnWndProc      = AppWndProc ;
  55.   wndclass.cbClsExtra       = 0 ;
  56.   wndclass.cbWndExtra      = 0 ;
  57.   wndclass.hInstance      = hInstance ;
  58.   wndclass.hIcon             = LoadIcon(NULL,IDI_APPLICATION) ;
  59.   wndclass.hCursor      = LoadCursor(NULL,IDC_ARROW) ;
  60.   wndclass.hbrBackground  = GetStockObject(WHITE_BRUSH) ;
  61.   wndclass.lpszMenuName      = szMenuName ;
  62.   wndclass.lpszClassName  = szAppName ;
  63.  
  64.   RegisterClass(&wndclass) ;
  65. }
  66.  
  67.  
  68. void InitEachInstance(HANDLE hInstance, int nCmdShow)
  69. {
  70.   hwnd = CreateWindow(szAppName,               // window class name
  71.               "Windows Application", // window caption
  72.               WS_OVERLAPPEDWINDOW ,
  73.               CW_USEDEFAULT,      // initial x position
  74.               0,                  // initial y position
  75.               CW_USEDEFAULT,      // initial x length
  76.               0,                  // initial y length
  77.               NULL,               // parent window handle
  78.               NULL,               // window menu handle
  79.               hInstance,          // program instance handle
  80.               NULL) ;             // parameters
  81.  
  82.   ShowWindow(hwnd,nCmdShow) ;
  83.   UpdateWindow(hwnd) ;
  84. }
  85.  
  86.  
  87. long FAR PASCAL AppWndProc (HWND hwnd, WORD message,
  88.                 WORD wParam, LONG lParam)
  89. { static HANDLE hInstance ;
  90.   static FARPROC lpfnDemoDlgProc ;
  91.  
  92.   switch (message)
  93.     { case WM_CREATE :
  94.        hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  95.        lpfnDemoDlgProc = MakeProcInstance(DemoDlgProc, hInstance) ;
  96.        break ;
  97.  
  98.       case WM_COMMAND :
  99.        switch(wParam)
  100.          { case IDM_DEMO :
  101.             DialogBox(hInstance, "DemoDlg",
  102.                   hwnd, lpfnDemoDlgProc) ;
  103.             break ;
  104.          }
  105.        break ;
  106.  
  107.       case WM_DESTROY :
  108.        FreeProcInstance(lpfnDemoDlgProc) ;
  109.        PostQuitMessage(0) ;
  110.        break ;
  111.  
  112.       default :
  113.        return DefWindowProc(hwnd, message, wParam, lParam) ;
  114.     }
  115.  
  116.   return 0L ;
  117. }
  118.  
  119.  
  120. BOOL FAR PASCAL DemoDlgProc(HWND hDlg, WORD message,
  121.          WORD wParam, LONG lParam)
  122. {
  123.   switch (message)
  124.     { case WM_INITDIALOG :
  125.        return TRUE ;
  126.  
  127.       case WM_COMMAND :
  128.        switch (wParam)
  129.          { case ID_OK :
  130.            case ID_CANCEL :
  131.             EndDialog(hDlg,0) ;
  132.             return TRUE ;
  133.          }
  134.        break ;
  135.     }
  136.   return FALSE ;
  137. }
  138.