home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / COMMONDG.PAK / FILEDLG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.0 KB  |  186 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: filedlg.c
  9. //
  10. //  PURPOSE: Shows basic use of "Open" and "Save As" common dialogs.
  11. //
  12. //  FUNCTIONS:
  13. //    CmdOpen - Call the open common dialog and show its results.
  14. //    CmdSaveAs - Call the SaveAs common dialog and show the results.
  15. //
  16. //  COMMENTS:
  17. //
  18. //
  19. //
  20. //  SPECIAL INSTRUCTIONS: N/A
  21. //
  22.  
  23. #include <windows.h>            // required for all Windows applications
  24. #ifdef WIN16
  25. #include "win16ext.h"           // required only for win16 applications
  26. #include "commdlg.h"           
  27. #endif
  28. #include "globals.h"            // prototypes specific to this application
  29. #include "resource.h"
  30.  
  31. //
  32. //  FUNCTION: CmdOpen(HWND, WORD, WORD, HWND)
  33. //
  34. //  PURPOSE: Call the open common dialog and show its results.
  35. //
  36. //  PARAMETERS:
  37. //    hwnd     - The window handle.
  38. //    wCommand - IDM_OPEN (Unused)
  39. //    wNotify   - Notification number (unused)
  40. //    hwndCtrl - NULL (Unused)
  41. //
  42. //  RETURN VALUE:
  43. //    Always returns 0 - command handled.
  44. //
  45. //  COMMENTS:
  46. //
  47. //
  48.  
  49. #pragma argsused
  50. LRESULT CmdOpen(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  51. {
  52.     OPENFILENAME ofn = {0}; // common dialog box structure
  53.     char szDirName[256];    // directory string
  54.     char szFile[256];       // filename string
  55.     char szFileTitle[256];  // file-title string
  56.     char szFilter[256];     // filter string
  57.     char chReplace;         // strparator for szFilter
  58.     int i, cbString;        // integer count variables
  59.  
  60.     // Retrieve the current directory name and store it in szDirName.
  61.  
  62. #ifdef WIN16
  63.     GetWindowsDirectory(szDirName, sizeof(szDirName));
  64. #else
  65.     GetCurrentDirectory(sizeof(szDirName), szDirName);
  66. #endif
  67.  
  68.     // Place the terminating null character in the szFile.
  69.  
  70.     szFile[0] = '\0';
  71.  
  72.     // Load the filter string from the resource file.
  73.  
  74.     cbString = LoadString(hInst, IDS_FILTERSTRING, szFilter, sizeof(szFilter));
  75.  
  76.     // Add a terminating null character to the filter string.
  77.  
  78.     chReplace = szFilter[cbString - 1];
  79.     for (i = 0; szFilter[i] != '\0'; i++)
  80.     {
  81.         if (szFilter[i] == chReplace)
  82.             szFilter[i] = '\0';
  83.     }
  84.  
  85.     // Set the members of the OPENFILENAME structure.
  86.  
  87.     ofn.lStructSize = sizeof(OPENFILENAME);
  88.     ofn.hwndOwner = hwnd;
  89.     ofn.lpstrFilter = szFilter;
  90.     ofn.nFilterIndex = 1;
  91.     ofn.lpstrFile = szFile;
  92.     ofn.nMaxFile = sizeof(szFile);
  93.     ofn.lpstrFileTitle = szFileTitle;
  94.     ofn.nMaxFileTitle = sizeof(szFileTitle);
  95.     ofn.lpstrInitialDir = szDirName;
  96.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  97.  
  98.     // Display the Open dialog box.
  99.  
  100.     if (GetOpenFileName(&ofn))
  101.     {
  102.         Open(ofn.lpstrFile, hwnd);
  103.     }
  104.     return 0;
  105. }
  106.  
  107.  
  108. //
  109. //  FUNCTION: CmdSaveAs(HWND, WORD, WORD, HWND)
  110. //
  111. //  PURPOSE: Call the SaveAs common dialog and show the results.
  112. //
  113. //  PARAMETERS:
  114. //    hwnd     - The window handle.
  115. //    wCommand - IDM_SAVEAS (Unused)
  116. //    wNotify   - Notification number (unused)
  117. //    hwndCtrl - NULL (Unused)
  118. //
  119. //  RETURN VALUE:
  120. //    Always returns 0 - command handled.
  121. //
  122. //  COMMENTS:
  123. //
  124. //
  125.  
  126. #pragma argsused
  127. LRESULT CmdSaveAs(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  128. {
  129.      OPENFILENAME ofn = {0}; // common dialog box structure
  130.      char szDirName[256];    // directory string
  131.      char szFile[256];       // filename string
  132.      char szFileTitle[256];  // file-title string
  133.     char szFilter[256];     // filter string
  134.     char chReplace;         // string separator for szFilter
  135.     int i, cbString;        // integer count variables
  136.  
  137.     // Retrieve the current directory name and store it in szDirName.
  138.  
  139. #ifdef WIN16
  140.     GetWindowsDirectory(szDirName, sizeof(szDirName));
  141. #else
  142.     GetCurrentDirectory(sizeof(szDirName), szDirName);
  143. #endif
  144.  
  145.      // Place the terminating null character in szFile.
  146.  
  147.     szFile[0] = '\0';
  148.  
  149.     // Load the filter string from the .RC file.
  150.  
  151.     cbString = LoadString(hInst, IDS_FILTERSTRING, szFilter, sizeof(szFilter));
  152.  
  153.     // Add a terminating null character to the filter string.
  154.  
  155.     chReplace = szFilter[cbString - 1];
  156.  
  157.     for (i = 0; szFilter[i] != '\0'; i++)
  158.      {
  159.         if (szFilter[i] == chReplace)
  160.             szFilter[i] = '\0';
  161.     }
  162.  
  163.     // Set the members of the OPENFILENAME structure.
  164.  
  165.     ofn.lStructSize = sizeof(OPENFILENAME);
  166.     ofn.hwndOwner = hwnd;
  167.     ofn.lpstrFilter = szFilter;
  168.     ofn.nFilterIndex = 1;
  169.     ofn.lpstrFile = szFile;
  170.     ofn.nMaxFile = sizeof(szFile);
  171.  
  172.     ofn.lpstrFileTitle = szFileTitle;
  173.     ofn.nMaxFileTitle = sizeof(szFileTitle);
  174.     ofn.lpstrInitialDir = szDirName;
  175.     ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT;
  176.  
  177.     // Display the Save As dialog box.
  178.  
  179.     if (GetSaveFileName(&ofn))
  180.     {
  181.         SaveAs(ofn.lpstrFile, hwnd);
  182.     }
  183.  
  184.      return 0;
  185. }
  186.