home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / FILEOPEN / FILEOPEN.C_ / FILEOPEN.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  9.8 KB  |  358 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: FileOpen.c
  4.  
  5.     PURPOSE: Demonstrates usage of common Open and Save As dialogs
  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. ****************************************************************************/
  16.  
  17. #include "windows.h"
  18. #include "fileopen.h"
  19. #include "commdlg.h"
  20.  
  21. HANDLE hInst;
  22.  
  23. HANDLE hAccTable;                /* handle to accelerator table */
  24. HWND hEditWnd;                    /* handle to edit window */
  25. HWND hwnd;                                      /* handle to main window */
  26.  
  27. /* new variables for common dialogs */
  28.  
  29. OPENFILENAME ofn;
  30. char szFilterSpec [128] =                       /* file type filters */
  31.              "Text Files (*.TXT)\0*.TXT\0All Files (*.*)\0*.*\0";
  32.  
  33. char szFileName[MAXFILENAME];
  34. char szFileTitle[MAXFILENAME];
  35.  
  36. char szBaseWindowTitle[] = "FileOpen Sample Application";
  37. char szWindowTitle[80];
  38.  
  39. /****************************************************************************
  40.  
  41.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  42.  
  43.     PURPOSE: calls initialization function, processes message loop
  44.  
  45. ****************************************************************************/
  46.  
  47. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  48. HANDLE hInstance;
  49. HANDLE hPrevInstance;
  50. LPSTR lpCmdLine;
  51. int nCmdShow;
  52. {
  53.     MSG msg;
  54.  
  55.     if (!hPrevInstance)
  56.         if (!InitApplication(hInstance))
  57.             return (FALSE);
  58.  
  59.     if (!InitInstance(hInstance, nCmdShow))
  60.         return (FALSE);
  61.  
  62.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  63.  
  64.     /* Only translate message if it is not an accelerator message */
  65.  
  66.         if (!TranslateAccelerator(hwnd, hAccTable, &msg)) {
  67.             TranslateMessage(&msg);
  68.             DispatchMessage(&msg);
  69.         }
  70.     }
  71.     return (msg.wParam);
  72. }
  73.  
  74.  
  75. /****************************************************************************
  76.  
  77.     FUNCTION: InitApplication(HANDLE)
  78.  
  79.     PURPOSE: Initializes window data and registers window class
  80.  
  81. ****************************************************************************/
  82.  
  83. BOOL InitApplication(hInstance)
  84. HANDLE hInstance;
  85. {
  86.     WNDCLASS  wc;
  87.  
  88.     wc.style = NULL;
  89.     wc.lpfnWndProc = MainWndProc;
  90.     wc.cbClsExtra = 0;
  91.     wc.cbWndExtra = 0;
  92.     wc.hInstance = hInstance;
  93.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  94.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  95.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  96.     wc.lpszMenuName =  "FileOpenMenu";
  97.     wc.lpszClassName = "FileOpenWClass";
  98.  
  99.     return (RegisterClass(&wc));
  100. }
  101.  
  102.  
  103. /****************************************************************************
  104.  
  105.     FUNCTION:  InitInstance(HANDLE, int)
  106.  
  107.     PURPOSE:  Saves instance handle and creates main window
  108.  
  109. ****************************************************************************/
  110.  
  111. BOOL InitInstance(hInstance, nCmdShow)
  112.     HANDLE          hInstance;
  113.     int             nCmdShow;
  114. {
  115.     RECT            Rect;
  116.  
  117.     hInst = hInstance;
  118.  
  119.     hAccTable = LoadAccelerators(hInst, "FileOpenAcc");
  120.  
  121.     hwnd = CreateWindow(
  122.         "FileOpenWClass",
  123.         szBaseWindowTitle,
  124.         WS_OVERLAPPEDWINDOW,
  125.         CW_USEDEFAULT,
  126.         CW_USEDEFAULT,
  127.         CW_USEDEFAULT,
  128.         CW_USEDEFAULT,
  129.         NULL,
  130.         NULL,
  131.         hInstance,
  132.         NULL
  133.     );
  134.  
  135.     if (!hwnd)
  136.         return (FALSE);
  137.  
  138.     GetClientRect(hwnd, (LPRECT) &Rect);
  139.  
  140.     /* Create a child window */
  141.  
  142.     hEditWnd = CreateWindow("Edit",
  143.         NULL,
  144.         WS_CHILD | WS_VISIBLE |
  145.         ES_MULTILINE |
  146.         WS_VSCROLL | WS_HSCROLL |
  147.         ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  148.         0,
  149.         0,
  150.         (Rect.right-Rect.left),
  151.         (Rect.bottom-Rect.top),
  152.         hwnd,
  153.         IDC_EDIT,                          /* Child control i.d. */
  154.         hInst,
  155.         NULL);
  156.  
  157.     if (!hEditWnd) {
  158.         DestroyWindow(hwnd);
  159.         return (NULL);
  160.     }
  161.  
  162.     /* fill in non-variant fields of OPENFILENAME struct. */
  163.     ofn.lStructSize       = sizeof(OPENFILENAME);
  164.     ofn.hwndOwner      = hwnd;
  165.     ofn.lpstrFilter      = szFilterSpec;
  166.     ofn.lpstrCustomFilter = NULL;
  167.     ofn.nMaxCustFilter      = 0;
  168.     ofn.nFilterIndex      = 1;
  169.     ofn.lpstrFile         = szFileName;
  170.     ofn.nMaxFile      = MAXFILENAME;
  171.     ofn.lpstrInitialDir   = NULL;
  172.     ofn.lpstrFileTitle    = szFileTitle;
  173.     ofn.nMaxFileTitle     = MAXFILENAME;
  174.     ofn.lpstrTitle        = NULL;
  175.     ofn.lpstrDefExt       = "TXT";
  176.     ofn.Flags             = 0;
  177.  
  178.     ShowWindow(hwnd, nCmdShow);
  179.     UpdateWindow(hwnd);
  180.     return (TRUE);
  181.  
  182. }
  183.  
  184. /****************************************************************************
  185.  
  186.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  187.  
  188.     PURPOSE:  Processes messages
  189.  
  190.     MESSAGES:
  191.  
  192.         WM_COMMAND    - application menu (About dialog box)
  193.         WM_DESTROY    - destroy window
  194.  
  195.     COMMENTS:
  196.  
  197.         WM_COMMAND processing:
  198.  
  199.             IDM_OPEN - query to save current file if there is one and it
  200.                        has been changed, open a new file.
  201.  
  202.             IDM_ABOUT - display "About" box.
  203.  
  204. ****************************************************************************/
  205.  
  206. long FAR PASCAL __export MainWndProc(hWnd, message, wParam, lParam)
  207. HWND hWnd;
  208. UINT message;
  209. WPARAM wParam;
  210. LPARAM lParam;
  211. {
  212.     FARPROC lpProcAbout;
  213.  
  214.     switch (message) {
  215.         case WM_COMMAND:
  216.             switch (wParam) {
  217.                 case IDM_ABOUT:
  218.                     lpProcAbout = MakeProcInstance(About, hInst);
  219.                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  220.                     FreeProcInstance(lpProcAbout);
  221.                     break;
  222.  
  223.                 case IDM_OPEN:
  224.  
  225.                     /* Use standard open dialog */
  226.  
  227.             if (!GetOpenFileName ((LPOPENFILENAME)&ofn))
  228.             return FALSE;
  229.  
  230.                     lstrcpy(szWindowTitle, szBaseWindowTitle);
  231.                     lstrcat(szWindowTitle, " - ");
  232.                     lstrcat(szWindowTitle, szFileTitle);
  233.                     SetWindowText(hWnd, szWindowTitle);
  234.  
  235.                     /* Let user know that "open" functionality is not yet
  236.                        implemented */
  237.             MessageBox (
  238.                           hWnd,
  239.               "Command not implemented",
  240.               "FileOpen Sample Application",
  241.               MB_ICONASTERISK | MB_OK);
  242.                     break;
  243.  
  244.                 case IDM_SAVEAS:
  245.  
  246.                     /* Use standard save dialog */
  247.  
  248.                     if (!GetSaveFileName ((LPOPENFILENAME)&ofn))
  249.             return FALSE;
  250.  
  251.                     lstrcpy(szWindowTitle, szBaseWindowTitle);
  252.                     lstrcat(szWindowTitle, " - ");
  253.                     lstrcat(szWindowTitle, szFileTitle);
  254.                     SetWindowText(hWnd, szWindowTitle);
  255.  
  256.                     /* Let user know that "save" functionality is not yet
  257.                        implemented */
  258.             MessageBox (
  259.                           hWnd,
  260.               "Command not implemented",
  261.               "FileOpen Sample Application",
  262.               MB_ICONASTERISK | MB_OK);
  263.                     break;
  264.  
  265.                 case IDM_NEW:
  266.                 case IDM_SAVE:
  267.                 case IDM_PRINT:
  268.                     MessageBox (
  269.                           GetFocus(),
  270.                           "Command not implemented",
  271.                           "FileOpen Sample Application",
  272.                           MB_ICONASTERISK | MB_OK);
  273.                     break;
  274.  
  275.                 case IDM_EXIT:
  276.                     DestroyWindow(hWnd);
  277.                     break;
  278.  
  279.                 /* edit menu commands */
  280.  
  281.                 case IDM_UNDO:
  282.                 case IDM_CUT:
  283.                 case IDM_COPY:
  284.                 case IDM_PASTE:
  285.                 case IDM_CLEAR:
  286.                     MessageBox (
  287.                           GetFocus(),
  288.                           "Command not implemented",
  289.                           "FileOpen Sample Application",
  290.                           MB_ICONASTERISK | MB_OK);
  291.                     break;
  292.  
  293.                 case IDC_EDIT:
  294.                     if (HIWORD (lParam) == EN_ERRSPACE) {
  295.                         MessageBox (
  296.                               GetFocus ()
  297.                             , "Out of memory."
  298.                             , "FileOpen Sample Application"
  299.                             , MB_ICONHAND | MB_OK
  300.                         );
  301.                     }
  302.                     break;
  303.  
  304.             }
  305.             break;
  306.  
  307.         case WM_SETFOCUS:
  308.             SetFocus (hEditWnd);
  309.             break;
  310.  
  311.         case WM_SIZE:
  312.             MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  313.             break;
  314.  
  315.         case WM_DESTROY:
  316.             PostQuitMessage(0);
  317.             break;
  318.  
  319.         default:
  320.             return (DefWindowProc(hWnd, message, wParam, lParam));
  321.     }
  322.     return (NULL);
  323. }
  324.  
  325. /****************************************************************************
  326.  
  327.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  328.  
  329.     PURPOSE:  Processes messages for "About" dialog box
  330.  
  331.     MESSAGES:
  332.  
  333.         WM_INITDIALOG - initialize dialog box
  334.         WM_COMMAND    - Input received
  335.  
  336. ****************************************************************************/
  337.  
  338. BOOL FAR PASCAL __export About(hDlg, message, wParam, lParam)
  339. HWND hDlg;
  340. unsigned message;
  341. WORD wParam;
  342. LONG lParam;
  343. {
  344.     switch (message) {
  345.         case WM_INITDIALOG:
  346.             return (TRUE);
  347.  
  348.         case WM_COMMAND:
  349.         if (wParam == IDOK
  350.                 || wParam == IDCANCEL) {
  351.                 EndDialog(hDlg, TRUE);
  352.                 return (TRUE);
  353.             }
  354.             break;
  355.     }
  356.     return (FALSE);
  357. }
  358.