home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap18 / cosmo1.0 / commdlg.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  3KB  |  92 lines

  1. /*
  2.  * COMMDLG.C
  3.  *
  4.  * Routines to interface to the COMMDLG library for File Open and
  5.  * File Save/Save As functions.
  6.  *
  7.  * Copyright(c) Microsoft Corp. 1992-1994 All Rights Reserved
  8.  * Win32 version, January 1994
  9.  */
  10.  
  11. #include <windows.h>
  12. #include <commdlg.h>
  13. #include "cosmo.h"
  14.  
  15.  
  16.  
  17. /*
  18.  * FSaveOpenDialog
  19.  *
  20.  * Purpose:
  21.  *  Invokes the COMMDLG.DLL GetOpenFileName dialog and retrieves
  22.  *  a filename for saving or opening.
  23.  *
  24.  * Parameters:
  25.  *  hWnd            HWND of the owning application.
  26.  *  hInst           HINSTANCE of the application instance.
  27.  *  pszExt          LPSTR of the default extension
  28.  *  pszFilter       LPSTR of the filter desciption.
  29.  *  pszFile         LPSTR buffer to receive the entered filename.
  30.  *                  Must be at least CCHPATHMAX long.
  31.  *  fOpen           BOOL indicating if we want file open or save.
  32.  *
  33.  * Return Value:
  34.  *  BOOL            TRUE if the function retrieved a filename,
  35.  *                  FALSE if the user pressed CANCEL.
  36.  */
  37.  
  38. BOOL WINAPI FSaveOpenDialog(HWND hWnd, HINSTANCE hInst, LPSTR pszExt
  39.     , LPSTR pszFilter, LPSTR pszFile, LPSTR pszCaption, BOOL fOpen)
  40.     {
  41.     OPENFILENAME    ofn;
  42.     char            szTitle[CCHFILENAMEMAX];
  43.     char            szFilter[80];
  44.     UINT            cch1;
  45.     UINT            cch2;
  46.  
  47.     ofn.lStructSize      =sizeof(OPENFILENAME);
  48.     ofn.hwndOwner        =hWnd;
  49.     ofn.hInstance        =hInst;
  50.  
  51.     ofn.lpstrFilter      =szFilter;
  52.     ofn.lpstrCustomFilter=NULL;
  53.     ofn.nMaxCustFilter   =0L;
  54.     ofn.nFilterIndex     =1L;                //We only have 1 extension.
  55.  
  56.     ofn.lpstrFile        =pszFile;
  57.     ofn.nMaxFile         =CCHPATHMAX;
  58.     ofn.lpstrFileTitle   =(LPSTR)szTitle;
  59.     ofn.nMaxFileTitle    =CCHFILENAMEMAX;
  60.  
  61.     ofn.lpstrInitialDir  =NULL;
  62.     ofn.lpstrTitle       =pszCaption;
  63.  
  64.     ofn.Flags            =OFN_HIDEREADONLY;
  65.     ofn.nFileOffset      =0;
  66.     ofn.nFileExtension   =0;
  67.     ofn.lpstrDefExt      =pszExt;
  68.     ofn.lCustData        =0;
  69.     ofn.lpfnHook         =NULL;
  70.     ofn.lpTemplateName   =NULL;
  71.  
  72.  
  73.     //Modify the flags as appropriate.
  74.     if (fOpen)
  75.         ofn.Flags        |= OFN_FILEMUSTEXIST;
  76.     else
  77.         ofn.Flags        |= OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
  78.  
  79.  
  80.     //Build a filter like "pszFilter\0*.pszExt\0\0"
  81.     lstrcpy(szFilter, pszFilter);
  82.     cch1=1+lstrlen(szFilter);
  83.  
  84.     cch2=wsprintf(pszFile, "*.%s", pszExt);  //Initial edit control contents.
  85.     lstrcpy(szFilter+cch1, pszFile);         //Append to filter.
  86.  
  87.     //Add the second null-terminator.
  88.     *(szFilter+cch1+cch2+1)=0;
  89.  
  90.     return GetOpenFileName(&ofn);
  91.     }
  92.