home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 4_2005-2006.ISO / data / Zips / UPX_FrontE1944871112005.psc / cFileDialog.cls < prev    next >
Text File  |  2005-10-02  |  11KB  |  340 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cFileDialog"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. 'Name:      cFileDialog
  15. 'Version:   1.0
  16. 'Date:      02/10/2001
  17. '
  18. 'Developer: Bill Bither
  19. '           http://www.atalasoft.com/
  20. '           support@atalasoft.com
  21. '
  22. 'Description:
  23. '
  24. 'This class module was created to replace Microsoft's heavy Common Dialog Control
  25. 'Most of the time ShowOpen and ShowSave are the only dialogs used. Instead of making
  26. 'a more versatile control with print dialogs, color dialogs, and hooking, I decided
  27. 'to make a very lightweight one since the CommonControl Print and Color Dialogs are
  28. 'essentially useless
  29. '
  30. 'I could have added hooking functionality, but that would involve including a standard module
  31. 'as well.  The only major benefit of hooking is the ability to center the dialog.  However,
  32. 'with this code, as long as you specify the form's handle, it will position the dialog to the
  33. 'upper left of the form
  34. '
  35. 'I use this class exclusively in my apps
  36. '
  37. 'This is a direct replacement to the Microsoft CommonDialog control and will involve very little
  38. 'Change in coding to implement.  If any bugs are found, please contact me
  39. '
  40. 'Disclaimer:
  41. '
  42. 'This code may be used for commercial purposes. I take no responsibility for bugs or problems from
  43. 'using this code.
  44. '
  45. 'Code Example:
  46. '
  47. '1) Add this class module 'cFileDialog' to your project
  48. '
  49. '2) In a module, form, or procedure declare the cFileDialog object:
  50. '   [Private] [Public] [Dim] fD as cFileDialog
  51. '
  52. '3) In a load event create the object:
  53. '   Set fD = New cFileDialog
  54. '
  55. '4) To use the open dialog:
  56. '   fD.Hwnd = me.Hwnd
  57. '   fD.ShowOpen
  58. '   sFilename = fD.FileName
  59. '
  60. '5) When unloading, be sure to set the cFileDialog to Nothing to prevent memory leaks:
  61. '   Set fD = Nothing
  62.  
  63. Option Explicit
  64.  
  65. 'This is the easiest way to get VB to recognize the
  66. 'constants project wide when in a class module
  67. Public Enum DialogFlags
  68.     OFN_READONLY = &H1
  69.     OFN_OVERWRITEPROMPT = &H2
  70.     OFN_HIDEREADONLY = &H4
  71.     OFN_NOCHANGEDIR = &H8
  72.     OFN_SHOWHELP = &H10
  73.     OFN_ENABLEHOOK = &H20
  74.     OFN_ENABLETEMPLATE = &H40
  75.     OFN_ENABLETEMPLATEHANDLE = &H80
  76.     OFN_NOVALIDATE = &H100
  77.     OFN_ALLOWMULTISELECT = &H200
  78.     OFN_EXTENSIONDIFFERENT = &H400
  79.     OFN_PATHMUSTEXIST = &H800
  80.     OFN_FILEMUSTEXIST = &H1000
  81.     OFN_CREATEPROMPT = &H2000
  82.     OFN_SHAREAWARE = &H4000
  83.     OFN_NOREADONLYRETURN = &H8000
  84.     OFN_NOTESTFILECREATE = &H10000
  85.     OFN_NONETWORKBUTTON = &H20000
  86.     OFN_NOLONGNAMES = &H40000
  87.     OFN_EXPLORER = &H80000
  88.     OFN_NODEREFERENCELINKS = &H100000
  89.     OFN_LONGNAMES = &H200000
  90.     cdlCancel = 32755
  91. End Enum
  92.  
  93. Private Type OPENFILENAME
  94.   nStructSize       As Long
  95.   hWndOwner         As Long
  96.   hInstance         As Long
  97.   sFilter           As String
  98.   sCustomFilter     As String
  99.   nMaxCustFilter    As Long
  100.   nFilterIndex      As Long
  101.   sFile             As String
  102.   nMaxFile          As Long
  103.   sFileTitle        As String
  104.   nMaxTitle         As Long
  105.   sInitialDir       As String
  106.   sDialogTitle      As String
  107.   flags             As Long
  108.   nFileOffset       As Integer
  109.   nFileExtension    As Integer
  110.   sDefFileExt       As String
  111.   nCustData         As Long
  112.   fnHook            As Long
  113.   sTemplateName     As String
  114. End Type
  115.  
  116. Private OFN As OPENFILENAME
  117.  
  118. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  119. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  120.  
  121. 'Module level variables
  122. Private m_bCancelError As Boolean
  123. Private m_sFileName As String
  124. Private m_sFileTitle As String
  125. Private m_sFilter As String
  126. Private m_sDefaultExt As String
  127. Private m_sInitDir As String
  128. Private m_lFlags As Long
  129.  
  130. Public Property Get CancelError() As Boolean
  131.     CancelError = m_bCancelError
  132. End Property
  133. Public Property Let CancelError(ByVal bCancelError As Boolean)
  134.     m_bCancelError = bCancelError
  135. End Property
  136. Public Property Get Filename() As String
  137.     'return object's FileName property
  138.     Filename = m_sFileName
  139. End Property
  140. Public Property Let Filename(ByVal sFilename As String)
  141.     'assign object's FileName property
  142.     m_sFileName = sFilename
  143.     OFN.sFile = sFilename & Space$(1024 - Len(sFilename)) & vbNullChar & vbNullChar
  144. End Property
  145. Public Property Get FileTitle() As String
  146.     'return object's FileTitle property
  147.     FileTitle = m_sFileTitle
  148. End Property
  149. Public Property Let FileTitle(ByVal vNewValue As String)
  150.     'assign object's FileTitle property
  151.     m_sFileTitle = vNewValue
  152. End Property
  153. Public Property Get Filter() As String
  154.     'return object's Filter property
  155.     Filter = m_sFilter
  156. End Property
  157. Public Property Let Filter(ByVal sFilter As String)
  158.     Dim S As String
  159.     'assign object's Filter property
  160.     m_sFilter = sFilter
  161.     ' To make Windows-style filter, replace | and : with nulls
  162.    Dim ch As String, i As Integer
  163.    For i = 1 To Len(sFilter)
  164.       ch = Mid$(sFilter, i, 1)
  165.       If ch = "|" Or ch = ":" Then
  166.           S = S & vbNullChar
  167.       Else
  168.           S = S & ch
  169.       End If
  170.    Next
  171.    ' Put double null at end
  172.    OFN.sFilter = S & vbNullChar & vbNullChar
  173. End Property
  174. Public Property Get FilterIndex() As Long
  175.     'return object's FilterIndex property
  176.     FilterIndex = OFN.nFilterIndex
  177. End Property
  178. Public Property Let FilterIndex(ByVal lFilterIndex As Long)
  179.     'assign object's FilterIndex property
  180.     OFN.nFilterIndex = lFilterIndex
  181. End Property
  182. Public Property Get DefaultExt() As String
  183.     'return object's DefaultExt property
  184.     DefaultExt = m_sDefaultExt
  185. End Property
  186. Public Property Let DefaultExt(ByVal sDefaultExt As String)
  187.     'assign object's DefaultExt property
  188.     m_sDefaultExt = sDefaultExt
  189.     OFN.sDefFileExt = sDefaultExt & vbNullChar & vbNullChar
  190. End Property
  191. Public Property Get DialogTitle() As String
  192.     'return object's FileTitle property
  193.     DialogTitle = OFN.sDialogTitle
  194. End Property
  195. Public Property Let DialogTitle(ByVal vNewValue As String)
  196.     'assign object's FileTitle property
  197.     OFN.sDialogTitle = vNewValue
  198. End Property
  199. Public Property Get flags() As Long
  200.     'return object's Flags property
  201.     flags = m_lFlags
  202. End Property
  203. Public Property Let flags(ByVal vNewValue As DialogFlags)
  204.     'assign object's Flags property
  205.     m_lFlags = vNewValue
  206. End Property
  207. Public Property Get hwnd() As Long
  208.     'Return object's hWnd property
  209.     hwnd = OFN.hWndOwner
  210. End Property
  211. Public Property Let hwnd(ByVal vNewValue As Long)
  212.     'Assign object's hWnd property
  213.     OFN.hWndOwner = vNewValue
  214. End Property
  215. Public Property Get InitDir() As String
  216.     'Return object's InitDir property
  217.     InitDir = m_sInitDir
  218. End Property
  219. Public Property Let InitDir(ByVal vNewValue As String)
  220.     'Assign object's InitDir property
  221.     m_sInitDir = vNewValue
  222.     OFN.sInitialDir = vNewValue & vbNullChar & vbNullChar
  223. End Property
  224.  
  225. Public Sub ShowOpen()
  226.     Dim sBuff As String
  227.     Dim lReturn As Long
  228.     Dim lFileSize As Long
  229.     
  230.     With OFN
  231.         .flags = m_lFlags
  232.  
  233.         'If multiselect then OFN_EXPLORER must be active else it'll crash
  234.         ' Pad file and file title buffers to maximum path
  235.         If (.flags And OFN_ALLOWMULTISELECT) = OFN_ALLOWMULTISELECT Then
  236.             .flags = .flags Or OFN_EXPLORER
  237.             lFileSize = 8192
  238.         Else
  239.             lFileSize = 1024
  240.         End If
  241.         
  242.         .sFile = m_sFileName & String$(lFileSize - Len(m_sFileName), 0)
  243.         .nMaxFile = lFileSize
  244.         .sFileTitle = m_sFileTitle & String$(lFileSize - Len(FileTitle), 0)
  245.         .nMaxTitle = lFileSize
  246.             
  247.         lReturn = GetOpenFileName(OFN)
  248.         If lReturn Then
  249.             If (.flags And OFN_ALLOWMULTISELECT) Then
  250.                 sBuff = .sFile
  251.             Else
  252.                 sBuff = TrimNull(.sFile)
  253.             End If
  254.             m_sFileName = sBuff
  255.         Else
  256.             If m_bCancelError Then
  257.                 Err.Raise cdlCancel, App.EXEName & ".cFileDialog", "User selected cancel."
  258.             End If
  259.         End If
  260.     End With
  261. End Sub
  262.  
  263. Public Sub ShowSave()
  264.     Dim sBuff As String
  265.     Dim lReturn As Long
  266.     
  267.     With OFN
  268.         .flags = m_lFlags
  269.         .sFile = m_sFileName & String$(1024 - Len(m_sFileName), 0)
  270.         .nMaxFile = 1024
  271.         .sFileTitle = m_sFileTitle & String$(1024 - Len(FileTitle), 0)
  272.         .nMaxTitle = 1024
  273.         
  274.         lReturn = GetSaveFileName(OFN)
  275.         If lReturn Then
  276.             sBuff = TrimNull(.sFile)
  277.             m_sFileName = sBuff
  278.         Else
  279.             If m_bCancelError Then
  280.                 Err.Raise cdlCancel, App.EXEName & ".cFilenDialog", "User selected cancel."
  281.             End If
  282.         End If
  283.     End With
  284. End Sub
  285.  
  286. Public Sub ParseMultiFileName(ByRef sDir As String, ByRef sFiles() As String, ByRef lFileCount As Long)
  287. Dim lPos As Long
  288. Dim lNextPos As Long
  289. Dim sAllFiles As String
  290. Dim i As Long
  291.     
  292.     lPos = InStr(m_sFileName, vbNullChar & vbNullChar)
  293.     sAllFiles = Left$(m_sFileName, lPos - 1)
  294.     lNextPos = InStr(sAllFiles, vbNullChar)
  295.     If lNextPos <> 0 Then
  296.         ' multi names
  297.         sDir = Mid$(sAllFiles, 1, lNextPos - 1)
  298.         Do While lNextPos <> 0
  299.             lPos = lNextPos + 1
  300.             lNextPos = InStr(lPos, sAllFiles, vbNullChar)
  301.             lFileCount = lFileCount + 1
  302.             ReDim Preserve sFiles(0 To lFileCount - 1) As String
  303.             If lNextPos > 0 Then
  304.                 sFiles(lFileCount - 1) = Mid$(sAllFiles, lPos, lNextPos - lPos)
  305.             Else
  306.                 sFiles(lFileCount - 1) = Mid$(sAllFiles, lPos)
  307.             End If
  308.         Loop
  309.     Else
  310.         ' single file
  311.         lFileCount = 1
  312.         ReDim sFiles(0)
  313.         lPos = InStrRev(m_sFileName, "\")
  314.         If lPos > 0 Then
  315.             sDir = Left$(m_sFileName, lPos)
  316.             sFiles(0) = TrimNull(Right$(m_sFileName, Len(m_sFileName) - lPos))
  317.         Else
  318.             sDir = ""
  319.             sFiles(0) = m_sFileName
  320.         End If
  321.     End If
  322.  
  323. End Sub
  324.  
  325. Private Sub Class_Initialize()
  326.     With OFN
  327.         .nFilterIndex = 1
  328.         .nStructSize = Len(OFN)
  329.     End With
  330. End Sub
  331.  
  332. Private Function TrimNull(ByVal item As String) As String
  333.  
  334.     Dim pos As Integer
  335.     pos = InStr(item, Chr$(0))
  336.     If pos Then item = Left$(item, pos - 1)
  337.     TrimNull = item
  338.  
  339. End Function
  340.