home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 08 < prev    next >
Encoding:
Text File  |  1991-08-28  |  7.3 KB  |  288 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: EditCntl.c
  4.  
  5.     PURPOSE: Creates an edit window
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.     After setting up the application's window, the size of the client
  18.     area is determined and a child window is created to use for editing.
  19.  
  20. ****************************************************************************/
  21.  
  22. #include "windows.h"
  23. #include "editcntl.h"
  24.  
  25. HANDLE hInst;
  26.  
  27. HANDLE hAccTable;                                /* handle to accelerator table */
  28. HWND hEditWnd;                        /* handle to edit window */
  29. HWND hwnd;                    /* handle to main windows  */
  30.  
  31. /****************************************************************************
  32.  
  33.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  34.  
  35.     PURPOSE: calls initialization function, processes message loop
  36.  
  37. ****************************************************************************/
  38.  
  39. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  40. HANDLE hInstance;
  41. HANDLE hPrevInstance;
  42. LPSTR lpCmdLine;
  43. int nCmdShow;
  44. {
  45.     MSG msg;
  46.  
  47.     if (!hPrevInstance)
  48.     if (!InitApplication(hInstance))
  49.         return (FALSE);
  50.  
  51.     if (!InitInstance(hInstance, nCmdShow))
  52.         return (FALSE);
  53.  
  54.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  55.  
  56.     /* Only translate message if it is not an accelerator message */
  57.  
  58.         if (!TranslateAccelerator(hwnd, hAccTable, &msg)) {
  59.             TranslateMessage(&msg);
  60.             DispatchMessage(&msg); 
  61.         }
  62.     }
  63.     return (msg.wParam);
  64. }
  65.  
  66.  
  67. /****************************************************************************
  68.  
  69.     FUNCTION: InitApplication(HANDLE)
  70.  
  71.     PURPOSE: Initializes window data and registers window class
  72.  
  73. ****************************************************************************/
  74.  
  75. BOOL InitApplication(hInstance)
  76. HANDLE hInstance;
  77. {
  78.     WNDCLASS  wc;
  79.  
  80.     wc.style = NULL;
  81.     wc.lpfnWndProc = MainWndProc;
  82.     wc.cbClsExtra = 0;
  83.     wc.cbWndExtra = 0;
  84.     wc.hInstance = hInstance;
  85.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  86.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  87.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  88.     wc.lpszMenuName =  "EditCntlMenu";
  89.     wc.lpszClassName = "EditCntlWClass";
  90.  
  91.     return (RegisterClass(&wc));
  92. }
  93.  
  94.  
  95. /****************************************************************************
  96.  
  97.     FUNCTION:  InitInstance(HANDLE, int)
  98.  
  99.     PURPOSE:  Saves instance handle and creates main window
  100.  
  101. ****************************************************************************/
  102.  
  103. BOOL InitInstance(hInstance, nCmdShow)
  104.     HANDLE          hInstance;
  105.     int             nCmdShow;
  106. {
  107.     RECT            Rect;
  108.  
  109.     hInst = hInstance;
  110.  
  111.     hAccTable = LoadAccelerators(hInst, "EditCntlAcc");
  112.  
  113.     hwnd = CreateWindow(
  114.         "EditCntlWClass",
  115.         "EditCntl Sample Application",
  116.         WS_OVERLAPPEDWINDOW,
  117.         CW_USEDEFAULT,
  118.         CW_USEDEFAULT,
  119.         CW_USEDEFAULT,
  120.         CW_USEDEFAULT,
  121.         NULL,
  122.         NULL,
  123.         hInstance,
  124.         NULL
  125.     );
  126.  
  127.     if (!hwnd)
  128.         return (FALSE);
  129.  
  130.     GetClientRect(hwnd, (LPRECT) &Rect);
  131.  
  132.     /* Create a child window */
  133.  
  134.     hEditWnd = CreateWindow("Edit",
  135.     NULL,
  136.     WS_CHILD | WS_VISIBLE |
  137.     ES_MULTILINE |
  138.     WS_VSCROLL | WS_HSCROLL |
  139.     ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  140.     0,
  141.     0,
  142.     (Rect.right-Rect.left),
  143.     (Rect.bottom-Rect.top),
  144.     hwnd,
  145.     IDC_EDIT,                          /* Child control i.d. */
  146.     hInst,
  147.     NULL);
  148.  
  149.     if (!hEditWnd) {
  150.     DestroyWindow(hwnd);
  151.     return (NULL);
  152.     }
  153.  
  154.     ShowWindow(hwnd, nCmdShow);
  155.     UpdateWindow(hwnd);
  156.     return (TRUE);
  157.  
  158. }
  159.  
  160. /****************************************************************************
  161.  
  162.     FUNCTION: MainWndProc(HWND, WORD, WORD, LONG)
  163.  
  164.     PURPOSE:  Processes messages
  165.  
  166.     MESSAGES:
  167.  
  168.     WM_COMMAND    - application menu (About dialog box)
  169.     WM_DESTROY    - destroy window
  170.  
  171. ****************************************************************************/
  172.  
  173. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  174. HWND hWnd;
  175. WORD message;
  176. WORD wParam;
  177. LONG lParam;
  178. {
  179.     FARPROC lpProcAbout;
  180.  
  181.     switch (message) {
  182.     case WM_COMMAND:
  183.             switch (wParam) {
  184.                 case IDM_ABOUT:
  185.                     lpProcAbout = MakeProcInstance(About, hInst);
  186.                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  187.                     FreeProcInstance(lpProcAbout);
  188.                     break;
  189.  
  190.                 /* file menu commands */
  191.  
  192.                 case IDM_NEW:
  193.                 case IDM_OPEN:
  194.                 case IDM_SAVE:
  195.                 case IDM_SAVEAS:
  196.                 case IDM_PRINT:
  197.                     MessageBox (
  198.                           GetFocus(),
  199.                           "Command not implemented",
  200.                           "EditCntl Sample Application",
  201.                           MB_ICONASTERISK | MB_OK);
  202.                     break;  
  203.  
  204.                 case IDM_EXIT:
  205.                     DestroyWindow(hWnd);
  206.                     break;
  207.     
  208.                 /* edit menu commands */
  209.  
  210.                 case IDM_UNDO:
  211.                 case IDM_CUT:
  212.                 case IDM_COPY:
  213.                 case IDM_PASTE:
  214.                 case IDM_CLEAR:
  215.                     MessageBox (
  216.                           GetFocus(),
  217.                           "Command not implemented",
  218.                           "EditCntl Sample Application",
  219.                           MB_ICONASTERISK | MB_OK);
  220.                     break;  
  221.  
  222.                 case IDC_EDIT:
  223.                     if (HIWORD (lParam) == EN_ERRSPACE) {
  224.                         MessageBox (
  225.                               GetFocus ()
  226.                             , "Out of memory."
  227.                             , "EditCntl Sample Application"
  228.                             , MB_ICONHAND | MB_OK
  229.                         );
  230.                     }
  231.                     break;
  232.  
  233.             } 
  234.             break;
  235.  
  236.         case WM_SETFOCUS:
  237.             SetFocus (hEditWnd);
  238.             break;
  239.  
  240.         case WM_SIZE:
  241.             MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  242.             break;
  243.  
  244.     case WM_DESTROY:
  245.         PostQuitMessage(0);
  246.         break;
  247.  
  248.     default:
  249.         return (DefWindowProc(hWnd, message, wParam, lParam));
  250.     }
  251.     return (NULL);
  252. }
  253.  
  254.  
  255. /****************************************************************************
  256.  
  257.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  258.  
  259.     PURPOSE:  Processes messages for "About" dialog box
  260.  
  261.     MESSAGES:
  262.  
  263.     WM_INITDIALOG - initialize dialog box
  264.     WM_COMMAND    - Input received
  265.  
  266. ****************************************************************************/
  267.  
  268. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  269. HWND hDlg;
  270. unsigned message;
  271. WORD wParam;
  272. LONG lParam;
  273. {
  274.     switch (message) {
  275.     case WM_INITDIALOG:
  276.         return (TRUE);
  277.  
  278.     case WM_COMMAND:
  279.         if (wParam == IDOK
  280.                 || wParam == IDCANCEL) {
  281.         EndDialog(hDlg, TRUE);
  282.         return (TRUE);
  283.         }
  284.         break;
  285.     }
  286.     return (FALSE);
  287. }
  288.