home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / filedialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  3.0 KB  |  103 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : filedialog.cpp                                                         //
  10. //  Description: File common dialog box class                                        //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <tchar.h>
  19. #include <assert.h>
  20. #include <commdlg.h>
  21.  
  22. #include "filedialog.h"
  23.  
  24. // Example: "Bitmaps" "bmp"
  25. // Example: "Bitmaps|Enhanced Metafiles" "bmp|emf"
  26. void KFileDialog::SetupOFN(OPENFILENAME & ofn, TCHAR Filter[], HWND hWnd, const TCHAR * pExtension, const TCHAR * pClass)
  27. {
  28.     memset(& ofn, 0, sizeof(ofn));
  29.  
  30.     ofn.lStructSize        = sizeof(OPENFILENAME);
  31.     ofn.hwndOwner        = hWnd;
  32.     ofn.lpstrFilter        = Filter; // _T("Bitmaps (*.bmp)\0*.bmp\0\0");
  33.     ofn.lpstrFile        = m_FileName;
  34.     ofn.nMaxFile        = MAX_PATH;
  35.     ofn.lpstrFileTitle    = m_TitleName;
  36.     ofn.nMaxFileTitle    = MAX_PATH;
  37.     ofn.lpstrDefExt        = NULL; // _T("bmp");
  38.     ofn.nFilterIndex    = 1;
  39.  
  40.     int len = 0;
  41.  
  42.     while ( (pExtension!=NULL) && (pClass!=NULL) )
  43.     {
  44.         TCHAR klass[MAX_PATH];
  45.         TCHAR ext  [MAX_PATH];
  46.  
  47.         _tcscpy(klass, pClass);
  48.         TCHAR * p = _tcschr(klass, '|');
  49.         if ( p ) 
  50.         {
  51.             * p = 0;
  52.             pClass = _tcschr(pClass, '|') + 1;
  53.         }
  54.         else 
  55.             pClass = NULL;
  56.  
  57.         _tcscpy(ext, pExtension);
  58.         p = _tcschr(ext, '|');
  59.         if ( p ) 
  60.         {
  61.             * p = 0;
  62.             pExtension = _tcschr(pExtension, '|') + 1;
  63.         }
  64.         else
  65.             pExtension = NULL;
  66.  
  67.         Filter += wsprintf(Filter, _T("%s (*.%s)%c*."), klass, ext, 0);
  68.         
  69.         if ( ofn.lpstrDefExt==NULL )
  70.             ofn.lpstrDefExt = Filter;
  71.  
  72.         Filter += wsprintf(Filter, _T("%s%c"), ext, 0);
  73.     }
  74.  
  75.     m_FileName[0] = 0;
  76. }
  77.  
  78.  
  79. BOOL KFileDialog::GetOpenFileName(HWND hWnd, const TCHAR * pExtension, const TCHAR * pClass)
  80. {
  81.     OPENFILENAME ofn;
  82.     TCHAR Filter[MAX_PATH];
  83.  
  84.     SetupOFN(ofn, Filter, hWnd, pExtension, pClass);
  85.  
  86.     ofn.Flags    = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  87.             
  88.     return ::GetOpenFileName(&ofn);
  89. }
  90.  
  91.  
  92. BOOL KFileDialog::GetSaveFileName(HWND hWnd, const TCHAR * pExtension, const TCHAR * pClass)
  93. {
  94.     OPENFILENAME ofn;
  95.     TCHAR Filter[MAX_PATH];
  96.  
  97.     SetupOFN(ofn, Filter, hWnd, pExtension, pClass);
  98.  
  99.     ofn.Flags  = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
  100.     
  101.     return ::GetSaveFileName(&ofn);
  102. }
  103.