home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / TextToPict2084669252007.psc / clsCommonDialog.cls < prev    next >
Text File  |  2005-12-01  |  34KB  |  1,163 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 = "clsCommonDialog"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'API function called by ChooseColor method
  17. Private Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
  18.  
  19. 'API function called by ChooseFont method
  20. Private Declare Function ChooseFont Lib "comdlg32.dll" Alias "ChooseFontA" (pChoosefont As ChooseFont) As Long
  21.  
  22. 'API function inside ShowHelp method
  23. Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" (ByVal hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData As Long) As Long
  24.  
  25. 'API function called by ShowOpen method
  26. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  27.  
  28. 'API function called by ShowSave method
  29. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  30.  
  31. 'API function called by ShowPrint method
  32. Private Declare Function PrintDlg Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PrintDlg) As Long
  33.  
  34.  
  35. 'API function to retrieve extended error information
  36. Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
  37.  
  38. 'API memory functions
  39. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  40. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  41. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
  42. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  43.  
  44. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  45.          hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  46.  
  47.  
  48. 'constants for API memory functions
  49. Private Const GMEM_MOVEABLE = &H2
  50. Private Const GMEM_ZEROINIT = &H40
  51. Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
  52.  
  53.  
  54. 'data buffer for the ChooseColor function
  55. Private Type CHOOSECOLOR
  56.         lStructSize As Long
  57.         hwndOwner As Long
  58.         hInstance As Long
  59.         rgbResult As Long
  60.         lpCustColors As Long
  61.         flags As Long
  62.         lCustData As Long
  63.         lpfnHook As Long
  64.         lpTemplateName As String
  65. End Type
  66.  
  67. 'constants for LOGFONT
  68. Private Const LF_FACESIZE = 32
  69. Private Const LF_FULLFACESIZE = 64
  70. Private Const FW_BOLD = 700
  71.  
  72. 'data buffer for the ChooseFont function
  73. Private Type LOGFONT
  74.         lfHeight As Long
  75.         lfWidth As Long
  76.         lfEscapement As Long
  77.         lfOrientation As Long
  78.         lfWeight As Long
  79.         lfItalic As Byte
  80.         lfUnderline As Byte
  81.         lfStrikeOut As Byte
  82.         lfCharSet As Byte
  83.         lfOutPrecision As Byte
  84.         lfClipPrecision As Byte
  85.         lfQuality As Byte
  86.         lfPitchAndFamily As Byte
  87.         lfFaceName(LF_FACESIZE) As Byte
  88. End Type
  89.  
  90. 'data buffer for the ChooseFont function
  91. Private Type ChooseFont
  92.         lStructSize As Long
  93.         hwndOwner As Long
  94.         hdc As Long
  95.         lpLogFont As Long
  96.         iPointSize As Long
  97.         flags As Long
  98.         rgbColors As Long
  99.         lCustData As Long
  100.         lpfnHook As Long
  101.         lpTemplateName As String
  102.         hInstance As Long
  103.         lpszStyle As String
  104.         nFontType As Integer
  105.         MISSING_ALIGNMENT As Integer
  106.         nSizeMin As Long
  107.         nSizeMax As Long
  108. End Type
  109.  
  110.  
  111. 'data buffer for the GetOpenFileName and GetSaveFileName functions
  112. Private Type OPENFILENAME
  113.         lStructSize As Long
  114.         hwndOwner As Long
  115.         hInstance As Long
  116.         lpstrFilter As String
  117.         lpstrCustomFilter As String
  118.         nMaxCustFilter As Long
  119.         iFilterIndex As Long
  120.         lpstrFile As String
  121.         nMaxFile As Long
  122.         lpstrFileTitle As String
  123.         nMaxFileTitle As Long
  124.         lpstrInitialDir As String
  125.         lpstrTitle As String
  126.         flags As Long
  127.         nFileOffset As Integer
  128.         nFileExtension As Integer
  129.         lpstrDefExt As String
  130.         lCustData As Long
  131.         lpfnHook As Long
  132.         lpTemplateName As String
  133. End Type
  134.  
  135.  
  136. 'data buffer for the PrintDlg function
  137. Private Type PrintDlg
  138.         lStructSize As Long
  139.         hwndOwner As Long
  140.         hDevMode As Long
  141.         hDevNames As Long
  142.         hdc As Long
  143.         flags As Long
  144.         nFromPage As Integer
  145.         nToPage As Integer
  146.         nMinPage As Integer
  147.         nMaxPage As Integer
  148.         nCopies As Integer
  149.         hInstance As Long
  150.         lCustData As Long
  151.         lpfnPrintHook As Long
  152.         lpfnSetupHook As Long
  153.         lpPrintTemplateName As String
  154.         lpSetupTemplateName As String
  155.         hPrintTemplate As Long
  156.         hSetupTemplate As Long
  157. End Type
  158. '********from experts exchange
  159. 'Private Const CF_SCREENFONTS = &H1
  160. 'Private Const CF_EFFECTS = &H100&
  161. 'Private Const CF_INITTOLOGFONTSTRUCT = &H40&
  162. 'internal property buffers
  163.  
  164. Private iAction As Integer         'internal buffer for Action property
  165. Private bCancelError As Boolean    'internal buffer for CancelError property
  166. Private lColor As Long             'internal buffer for Color property
  167. Private lCopies As Long            'internal buffer for lCopies property
  168. Private sDefaultExt As String      'internal buffer for sDefaultExt property
  169. Private sDialogTitle As String     'internal buffer for DialogTitle property
  170. Private sFileName As String        'internal buffer for FileName property
  171. Private sFileTitle As String       'internal buffer for FileTitle property
  172. Private sFilter As String          'internal buffer for Filter property
  173. Private iFilterIndex As Integer    'internal buffer for FilterIndex property
  174. Private lFlags As Long             'internal buffer for Flags property
  175. Private bFontBold As Boolean       'internal buffer for FontBold property
  176. Private bFontItalic As Boolean     'internal buffer for FontItalic property
  177. Private sFontName As String        'internal buffer for FontName property
  178. Private lFontSize As Long          'internal buffer for FontSize property
  179. Private bFontStrikethru As Boolean 'internal buffer for FontStrikethru property
  180. Private bFontUnderline As Boolean  'internal buffer for FontUnderline property
  181. Private lFromPage As Long          'internal buffer for FromPage property
  182. Private lhdc As Long               'internal buffer for hdc property
  183. Private lhwndOwner As Long              'internal buffer for hWnd property
  184. Private lHelpCommand As Long       'internal buffer for HelpCommand property
  185. Private sHelpContext As String     'internal buffer for HelpContext property
  186. Private sHelpFile As String        'internal buffer for HelpFile property
  187. Private sHelpKey As String         'internal buffer for HelpKey property
  188. Private sInitDir As String         'internal buffer for InitDir property
  189. Private lMax As Long               'internal buffer for Max property
  190. Private lMaxFileSize As Long       'internal buffer for MaxFileSize property
  191. Private lMin As Long               'internal buffer for Min property
  192. Private objObject As Object        'internal buffer for Object property
  193. Private iPrinterDefault As Integer 'internal buffer for PrinterDefault property
  194. Private lToPage As Long            'internal buffer for ToPage property
  195.  
  196. Private lApiReturn As Long          'internal buffer for APIReturn property
  197. Private lExtendedError As Long      'internal buffer for ExtendedError property
  198.  
  199.  
  200.  
  201. 'constants for color dialog
  202.  
  203. Private Const CDERR_DIALOGFAILURE = &HFFFF
  204. Private Const CDERR_FINDRESFAILURE = &H6
  205. Private Const CDERR_GENERALCODES = &H0
  206. Private Const CDERR_INITIALIZATION = &H2
  207. Private Const CDERR_LOADRESFAILURE = &H7
  208. Private Const CDERR_LOADSTRFAILURE = &H5
  209. Private Const CDERR_LOCKRESFAILURE = &H8
  210. Private Const CDERR_MEMALLOCFAILURE = &H9
  211. Private Const CDERR_MEMLOCKFAILURE = &HA
  212. Private Const CDERR_NOHINSTANCE = &H4
  213. Private Const CDERR_NOHOOK = &HB
  214. Private Const CDERR_NOTEMPLATE = &H3
  215. Private Const CDERR_REGISTERMSGFAIL = &HC
  216. Private Const CDERR_STRUCTSIZE = &H1
  217.  
  218.  
  219. 'constants for file dialog
  220.  
  221. Private Const FNERR_BUFFERTOOSMALL = &H3003
  222. Private Const FNERR_FILENAMECODES = &H3000
  223. Private Const FNERR_INVALIDFILENAME = &H3002
  224. Private Const FNERR_SUBCLASSFAILURE = &H3001
  225.  
  226. Public Property Get Filter() As String
  227.     'return object's Filter property
  228.     Filter = sFilter
  229. End Property
  230.  
  231. Public Sub ShowColor()
  232.     'display the color dialog box
  233.    
  234.     Dim tChooseColor As CHOOSECOLOR
  235.     Dim alCustomColors(15) As Long
  236.     Dim lCustomColorSize As Long
  237.     Dim lCustomColorAddress As Long
  238.     Dim lMemHandle As Long
  239.    
  240.     Dim n As Integer
  241.        
  242.     On Error GoTo ShowColorError
  243.    
  244.    
  245.     '***    init property buffers
  246.    
  247.     iAction = 3  'Action property - ShowColor
  248.     lApiReturn = 0  'APIReturn property
  249.     lExtendedError = 0  'ExtendedError property
  250.    
  251.    
  252.     '***    prepare tChooseColor data
  253.    
  254.     'lStructSize As Long
  255.     tChooseColor.lStructSize = Len(tChooseColor)
  256.    
  257.     'hwndOwner As Long
  258.     tChooseColor.hwndOwner = lhwndOwner
  259.  
  260.     'hInstance As Long
  261.    
  262.     'rgbResult As Long
  263.     tChooseColor.rgbResult = lColor
  264.    
  265.     'lpCustColors As Long
  266.     ' Fill custom colors array with all white
  267.     For n = 0 To UBound(alCustomColors)
  268.         alCustomColors(n) = &HFFFFFF
  269.     Next
  270.     ' Get size of memory needed for custom colors
  271.     lCustomColorSize = Len(alCustomColors(0)) * 16
  272.     ' Get a global memory block to hold a copy of the custom colors
  273.     lMemHandle = GlobalAlloc(GHND, lCustomColorSize)
  274.    
  275.     If lMemHandle = 0 Then
  276.         Exit Sub
  277.     End If
  278.     ' Lock the custom color's global memory block
  279.     lCustomColorAddress = GlobalLock(lMemHandle)
  280.     If lCustomColorAddress = 0 Then
  281.         Exit Sub
  282.     End If
  283.     ' Copy custom colors to the global memory block
  284.     Call CopyMemory(ByVal lCustomColorAddress, alCustomColors(0), lCustomColorSize)
  285.  
  286.     tChooseColor.lpCustColors = lCustomColorAddress
  287.    
  288.     'flags As Long
  289.     tChooseColor.flags = lFlags
  290.        
  291.     'lCustData As Long
  292.     'lpfnHook As Long
  293.     'lpTemplateName As String
  294.    
  295.    
  296.     '***    call the ChooseColor API function
  297.     lApiReturn = CHOOSECOLOR(tChooseColor)
  298.    
  299.    
  300.     '***    handle return from ChooseColor API function
  301.     Select Case lApiReturn
  302.        
  303.         Case 0  'user canceled
  304.         If bCancelError = True Then
  305.             'generate an error
  306.             On Error GoTo 0
  307.             err.Raise number:=vbObjectError + 894, _
  308.                 Description:="Cancel Pressed"
  309.             Exit Sub
  310.         End If
  311.        
  312.         Case 1  'user selected a color
  313.             'update property buffer
  314.             lColor = tChooseColor.rgbResult
  315.        
  316.         Case Else   'an error occured
  317.             'call CommDlgExtendedError
  318.             lExtendedError = CommDlgExtendedError
  319.        
  320.     End Select
  321.  
  322. Exit Sub
  323.  
  324. ShowColorError:
  325.     Exit Sub
  326. End Sub
  327.  
  328. Public Sub ShowFont()
  329.     'display the font dialog box
  330.    
  331.     Dim tLogFont As LOGFONT
  332.     Dim tChooseFont As ChooseFont
  333.    
  334.     Dim lLogFontSize As Long
  335.     Dim lLogFontAddress As Long
  336.     Dim lMemHandle As Long
  337.    
  338.     Dim lReturn As Long
  339.     Dim sFont As String
  340.     Dim lBytePoint As Long
  341.     'On Error GoTo ShowFontError
  342.    On Error Resume Next
  343.     '***    init property buffers
  344.    
  345.     iAction = 4  'Action property - ShowFont
  346.     lApiReturn = 0  'APIReturn property
  347.     lExtendedError = 0  'ExtendedError property
  348.  
  349.    
  350.     '***    prepare tChooseFont data
  351.        
  352.     'tLogFont.lfHeight As Long
  353.     'tLogFont.lfWidth As Long
  354.     'tLogFont.lfEscapement As Long
  355.     'tLogFont.lfOrientation As Long
  356.    
  357.     'tLogFont.lfWeight As Long - init from FontBold property
  358.     If bFontBold = True Then
  359.         tLogFont.lfWeight = FW_BOLD
  360.     End If
  361.    
  362.     'tLogFont.lfItalic As Byte - init from FontItalic property
  363.     If bFontItalic = True Then
  364.         tLogFont.lfItalic = 1
  365.     End If
  366.    
  367.     'tLogFont.lfUnderline As Byte - init from FontUnderline property
  368.     If bFontUnderline = True Then
  369.         tLogFont.lfUnderline = 1
  370.     End If
  371.  
  372.     'tLogFont.lfStrikeOut As Byte - init from FontStrikethru property
  373.     If bFontStrikethru = True Then
  374.         tLogFont.lfStrikeOut = 1
  375.     End If
  376.  
  377.     'tLogFont.lfCharSet As Byte
  378.     'tLogFont.lfOutPrecision As Byte
  379.     'tLogFont.lfClipPrecision As Byte
  380.     'tLogFont.lfQuality As Byte
  381.     'tLogFont.lfPitchAndFamily As Byte
  382.     'tLogFont.lfFaceName(LF_FACESIZE) As Byte
  383.    
  384.     'tChooseFont.lStructSize As Long
  385.     tChooseFont.lStructSize = Len(tChooseFont)
  386.    
  387.     'tChooseFont.hwndOwner As Long
  388.     'tChooseFont.hdc As Long
  389.    
  390.     'tChooseFont.lpLogFont As Long
  391.     lLogFontSize = Len(tLogFont)
  392.    
  393.     ' Get a global memory block to hold a copy of tLogFont - exit on failure
  394.     lMemHandle = GlobalAlloc(GHND, lLogFontSize)
  395.     If lMemHandle = 0 Then
  396.         Exit Sub
  397.     End If
  398.    
  399.     ' Lock tLogFont's global memory block - exit on failure
  400.     lLogFontAddress = GlobalLock(lMemHandle)
  401.     If lLogFontAddress = 0 Then
  402.         Exit Sub
  403.     End If
  404.    
  405.     ' Copy tLogFont to the global memory block
  406.     Call CopyMemory(ByVal lLogFontAddress, tLogFont, lLogFontSize)
  407.  
  408.     tChooseFont.lpLogFont = lLogFontAddress
  409.    
  410.     'tChooseFont.iPointSize As Long - init from FontSize property
  411.     tChooseFont.iPointSize = lFontSize * 10
  412.    
  413.     'tChooseFont.flags As Long - init from Flags property
  414.     tChooseFont.flags = lFlags
  415.  
  416.     'tChooseFont.rgbColors As Long
  417.     'tChooseFont.lCustData As Long
  418.     'tChooseFont.lpfnHook As Long
  419.     'tChooseFont.lpTemplateName As String
  420.     'tChooseFont.hInstance As Long
  421.    
  422.     'tChooseFont.lpszStyle As String
  423.     'sFont = Chr$(0) & Space$(20) & Chr$(0)
  424.     'tChooseFont.lpszStyle = sFont
  425.    
  426.     'tChooseFont.nFontType As Integer
  427.     'tChooseFont.MISSING_ALIGNMENT As Integer
  428.     'tChooseFont.nSizeMin As Long
  429.     'tChooseFont.nSizeMax As Long
  430.                    
  431.    
  432.     '***    call the CHOOSEFONT API function
  433.     lApiReturn = ChooseFont(tChooseFont)    'store to APIReturn property
  434.    
  435.    
  436.     '***    handle return from CHOOSEFONT API function
  437.     Select Case lApiReturn
  438.        
  439.         Case 0  'user canceled
  440.         If bCancelError = True Then
  441.             'generate an error
  442.             err.Raise (2001)
  443.             Exit Sub
  444.         End If
  445.        
  446.         Case 1  'user selected a font
  447.             ' Copy global memory block to tLogFont
  448.             Call CopyMemory(tLogFont, ByVal lLogFontAddress, lLogFontSize)
  449.            
  450.             'tLogFont.lfWeight As Long  - store to FontBold property
  451.             If tLogFont.lfWeight >= FW_BOLD Then
  452.                 bFontBold = True
  453.             Else
  454.                 bFontBold = False
  455.             End If
  456.                        
  457.             'tLogFont.lfItalic As Byte - store to FontItalic property
  458.             If tLogFont.lfItalic = 1 Then
  459.                 bFontItalic = True
  460.             Else
  461.                 bFontItalic = False
  462.             End If
  463.            
  464.             'tLogFont.lfUnderline As Byte - store to FontUnderline property
  465.             If tLogFont.lfUnderline = 1 Then
  466.                 bFontUnderline = True
  467.             Else
  468.                 bFontUnderline = False
  469.             End If
  470.        
  471.             'tLogFont.lfStrikeOut As Byte - store to FontStrikethru property
  472.             If tLogFont.lfStrikeOut = 1 Then
  473.                 bFontStrikethru = True
  474.             Else
  475.                 bFontStrikethru = False
  476.             End If
  477.            
  478.             'tLogFont.lfFaceName(LF_FACESIZE) As Byte - store to FontName property
  479.             FontName = sByteArrayToString(tLogFont.lfFaceName())
  480.            
  481.             'tChooseFont.iPointSize As Long - store to FontSize property
  482.             lFontSize = CLng(tChooseFont.iPointSize / 10)
  483.        
  484.         Case Else   'an error occured
  485.             'call CommDlgExtendedError
  486.             lExtendedError = CommDlgExtendedError   'store to ExtendedError property
  487.        
  488.     End Select
  489. Exit Sub
  490.  
  491. ShowFontError:
  492.     Exit Sub
  493. End Sub
  494.  
  495. Public Sub ShowHelp()
  496.     'run winhelp.exe with the specified help file
  497.     Dim sHelpFileBuff As String
  498.     Dim lData As Long
  499.    
  500.     On Error GoTo ShowHelpError
  501.    
  502.     '***    init Private properties
  503.     iAction = 6  'Action property - ShowHelp
  504.     lApiReturn = 0  'APIReturn property
  505.     lExtendedError = 0  'ExtendedError property
  506.  
  507.     '***    prepare the buffers and parameters for the API function
  508.     'sHelpFile is a null terminated string
  509.     sHelpFileBuff = sHelpFile & Chr$(0)
  510.    
  511.     'sData is dependent on lHelpCommand
  512.     Select Case lHelpCommand
  513.         Case 0
  514.             lData = 0
  515.         Case Else
  516.             lData = 0
  517.     End Select
  518.    
  519.     '***    call the API function
  520.     lApiReturn = WinHelp(lhdc, sHelpFile, lHelpCommand, lData)    ' - Store to APIReturn property
  521.    
  522.     Select Case lApiReturn
  523.        
  524.         Case 0  '
  525.             'call CommDlgExtendedError
  526.             lExtendedError = CommDlgExtendedError   ' - store to ExtendedError property
  527.        
  528.         Case Else   '
  529.             'call CommDlgExtendedError
  530.             lExtendedError = CommDlgExtendedError
  531.        
  532.     End Select
  533.        
  534. Exit Sub
  535.  
  536. ShowHelpError:
  537.     Exit Sub
  538. End Sub
  539.  
  540.  
  541. Public Sub ShowOpen()
  542.    
  543.     'display the file open dialog box
  544.     ShowFileDialog (1)  'Action property - ShowOpen
  545.    
  546. End Sub
  547.  
  548. Public Sub ShowPrinter()
  549.     'display the print dialog
  550.     Dim tPrintDlg As PrintDlg
  551.    
  552.     On Error GoTo ShowPrinterError
  553.    
  554.     '***    init public properties
  555.     iAction = 5  'Action property - ShowPrint
  556.     lApiReturn = 0  'APIReturn property
  557.     lExtendedError = 0  'ExtendedError property
  558.  
  559.     '***    prepare tPrintDlg data
  560.    
  561.     'lStructSize As Long
  562.     tPrintDlg.lStructSize = Len(tPrintDlg)
  563.    
  564.     'hwndOwner As Long
  565.    
  566.     'hDevMode As Long
  567.    
  568.     'hDevNames As Long
  569.    
  570.     'hdc As Long - init from hDC property
  571.     tPrintDlg.hdc = lhdc
  572.    
  573.     'flags As Long - init from Flags property
  574.     tPrintDlg.flags = lFlags
  575.    
  576.     'nFromPage As Integer - init from FromPage property
  577.     tPrintDlg.nFromPage = lFromPage
  578.    
  579.     'nToPage As Integer - init from ToPage property
  580.     tPrintDlg.nToPage = lToPage
  581.    
  582.     'nMinPage As Integer - init from Min property
  583.     tPrintDlg.nMinPage = lMin
  584.    
  585.     'nMaxPage As Integer - init from Max property
  586.     tPrintDlg.nMaxPage = lMax
  587.    
  588.     'nCopies As Integer - init from Copies property
  589.     tPrintDlg.nCopies = lCopies
  590.    
  591.     'hInstance As Long
  592.    
  593.     'lCustData As Long
  594.    
  595.    
  596.     '***    Call the PrintDlg API function
  597.     lApiReturn = PrintDlg(tPrintDlg)
  598.    
  599.     '***    handle return from PrintDlg API function
  600.     Select Case lApiReturn
  601.        
  602.         Case 0  'user canceled
  603.             If bCancelError = True Then
  604.                 'generate an error
  605.                 err.Raise (2001)
  606.                 Exit Sub
  607.             End If
  608.        
  609.         Case 1  'user selected OK
  610.             'nFromPage As Integer - store to FromPage property
  611.             lFromPage = tPrintDlg.nFromPage
  612.            
  613.             'nToPage As Integer - store to ToPage property
  614.             lToPage = tPrintDlg.nToPage
  615.            
  616.             'nMinPage As Integer - store to Min property
  617.             lMin = tPrintDlg.nMinPage
  618.            
  619.             'nMaxPage As Integer - store to Max property
  620.             lMax = tPrintDlg.nMaxPage
  621.            
  622.             'nCopies As Integer - store to Copies property
  623.             lCopies = tPrintDlg.nCopies
  624.    
  625.         Case Else   'an error occured
  626.             'call CommDlgExtendedError
  627.             lExtendedError = CommDlgExtendedError   'store to ExtendedError property
  628.    
  629.     End Select
  630.  
  631. Exit Sub
  632.  
  633. ShowPrinterError:
  634.    
  635.     Exit Sub
  636.    
  637. End Sub
  638.  
  639.  
  640. Public Sub ShowSave()
  641.    
  642.     'display the file save dialog box
  643.     ShowFileDialog (2)  'Action property - ShowSave
  644.    
  645.  
  646. End Sub
  647.  
  648.  
  649. Public Property Get FileName() As String
  650.     'return object's FileName property
  651.     FileName = sFileName
  652. End Property
  653.  
  654. Public Property Let FileName(vNewValue As String)
  655.     'assign object's FileName property
  656.     sFileName = vNewValue
  657. End Property
  658.  
  659.  
  660. Public Property Let Filter(vNewValue As String)
  661.     'assign object's Filter property
  662.     sFilter = vNewValue
  663. End Property
  664.  
  665.  
  666. Private Function sLeftOfNull(ByVal sIn As String)
  667.     'returns the part of sIn preceding Chr$(0)
  668.     Dim lNullPos As Long
  669.    
  670.     'init output
  671.     sLeftOfNull = sIn
  672.    
  673.     'get position of first Chr$(0) in sIn
  674.     lNullPos = InStr(sIn, Chr$(0))
  675.    
  676.     'return part of sIn to left of first Chr$(0) if found
  677.     If lNullPos > 0 Then
  678.         sLeftOfNull = Mid$(sIn, 1, lNullPos - 1)
  679.     End If
  680.    
  681. End Function
  682.  
  683.  
  684. Public Property Get Action() As Integer
  685.     'Return object's Action property
  686.     Action = iAction
  687. End Property
  688.  
  689. Private Function sAPIFilter(sIn)
  690.     'prepares sIn for use as a filter string in API common dialog functions
  691.     Dim lChrNdx As Long
  692.     Dim sOneChr As String
  693.     Dim sOutStr As String
  694.    
  695.     'convert any | characters to nulls
  696.     For lChrNdx = 1 To Len(sIn)
  697.         sOneChr = Mid$(sIn, lChrNdx, 1)
  698.         If sOneChr = "|" Then
  699.             sOutStr = sOutStr & Chr$(0)
  700.         Else
  701.             sOutStr = sOutStr & sOneChr
  702.         End If
  703.     Next
  704.    
  705.     'add a null to the end
  706.     sOutStr = sOutStr & Chr$(0)
  707.    
  708.     'return sOutStr
  709.     sAPIFilter = sOutStr
  710.    
  711. End Function
  712.  
  713. Public Property Get FilterIndex() As Integer
  714.     'return object's FilterIndex property
  715.     FilterIndex = iFilterIndex
  716. End Property
  717.  
  718. Public Property Let FilterIndex(vNewValue As Integer)
  719.     iFilterIndex = vNewValue
  720. End Property
  721.  
  722. Public Property Get CancelError() As Boolean
  723.     'Return object's CancelError property
  724.     CancelError = bCancelError
  725. End Property
  726.  
  727. Public Property Let CancelError(vNewValue As Boolean)
  728.     'Assign object's CancelError property
  729.     bCancelError = vNewValue
  730. End Property
  731.  
  732. Public Property Get Color() As Long
  733.     'return object's Color property
  734.     Color = lColor
  735. End Property
  736.  
  737. Public Property Let Color(vNewValue As Long)
  738.     'assign object's Color property
  739.     lColor = vNewValue
  740. End Property
  741.  
  742. Public Property Get Copies() As Long
  743.     'return object's Copies property
  744.     Copies = lCopies
  745. End Property
  746.  
  747. Public Property Let Copies(vNewValue As Long)
  748.     'assign object's Copies property
  749.     lCopies = vNewValue
  750. End Property
  751.  
  752. Public Property Get DefaultExt() As String
  753.     'return object's DefaultExt property
  754.     DefaultExt = sDefaultExt
  755. End Property
  756.  
  757. Public Property Let DefaultExt(vNewValue As String)
  758.     'assign object's DefaultExt property
  759.     sDefaultExt = vNewValue
  760. End Property
  761.  
  762. Public Property Get DialogTitle() As String
  763.     'return object's FileName property
  764.     DialogTitle = sDialogTitle
  765. End Property
  766.  
  767. Public Property Let DialogTitle(vNewValue As String)
  768.     'assign object's DialogTitle property
  769.     sDialogTitle = vNewValue
  770. End Property
  771.  
  772. Public Property Get flags() As Long
  773.     'return object's Flags property
  774.     flags = lFlags
  775. End Property
  776.  
  777. Public Property Let flags(vNewValue As Long)
  778.     'assign object's Flags property
  779.     lFlags = vNewValue
  780. End Property
  781.  
  782. Public Property Get FontBold() As Boolean
  783.     'return object's FontBold property
  784.     FontBold = bFontBold
  785. End Property
  786.  
  787. Public Property Let FontBold(vNewValue As Boolean)
  788.     'Assign object's FontBold property
  789.     bFontBold = vNewValue
  790. End Property
  791.  
  792. Public Property Get FontItalic() As Boolean
  793.     'Return object's FontItalic property
  794.     FontItalic = bFontItalic
  795. End Property
  796.  
  797. Public Property Let FontItalic(vNewValue As Boolean)
  798.     'Assign object's FontItalic property
  799.     bFontItalic = vNewValue
  800. End Property
  801.  
  802. Public Property Get FontName() As String
  803.     'Return object's Fontname property
  804.     FontName = sFontName
  805. End Property
  806.  
  807. Public Property Let FontName(vNewValue As String)
  808.     'Assign object's FontName property
  809.     sFontName = vNewValue
  810. End Property
  811.  
  812. Public Property Get FontSize() As Long
  813.     'Return object's FontSize property
  814.     FontSize = lFontSize
  815. End Property
  816.  
  817. Public Property Let FontSize(vNewValue As Long)
  818.     'Assign object's FontSize property
  819.     lFontSize = vNewValue
  820. End Property
  821.  
  822. Public Property Get FontStrikethru() As Boolean
  823.     'Return object's FontStrikethru property
  824.     FontStrikethru = bFontStrikethru
  825. End Property
  826.  
  827. Public Property Let FontStrikethru(vNewValue As Boolean)
  828.     'Assign object's - property
  829.     bFontStrikethru = vNewValue
  830. End Property
  831.  
  832. Public Property Get FontUnderline() As Boolean
  833.     'Return object's FontUnderline property
  834.     FontUnderline = bFontUnderline
  835. End Property
  836.  
  837. Public Property Let FontUnderline(vNewValue As Boolean)
  838.     'Assign object's FontUnderline property
  839.     bFontUnderline = vNewValue
  840. End Property
  841.  
  842. Public Property Get FromPage() As Long
  843.     'Return object's FromPAge property
  844.     FromPage = lFromPage
  845. End Property
  846.  
  847. Public Property Let FromPage(vNewValue As Long)
  848.     'Assign object's FromPage property
  849.     lFromPage = vNewValue
  850. End Property
  851.  
  852. Public Property Get hwndOwner() As Long
  853.     'Return object's hWnd property
  854.     hwndOwner = lhwndOwner
  855. End Property
  856.  
  857. Public Property Let hwndOwner(vNewValue As Long)
  858.     'Assign object's hWnd property
  859.     lhwndOwner = vNewValue
  860. End Property
  861. Public Property Get hdc() As Long
  862.     'Return object's hWnd property
  863.     hdc = lhdc
  864. End Property
  865.  
  866. Public Property Let hdc(vNewValue As Long)
  867.     'Assign object's hWnd property
  868.     lhdc = vNewValue
  869. End Property
  870.  
  871. Public Property Get HelpCommand() As Long
  872.     'Return object's HelpCommand property
  873.     HelpCommand = lHelpCommand
  874. End Property
  875.  
  876. Public Property Let HelpCommand(vNewValue As Long)
  877.     'Assign object's HelpCommand property
  878.     lHelpCommand = vNewValue
  879. End Property
  880.  
  881. Public Property Get HelpContext() As String
  882.     'Return object's HelpContext property
  883.     HelpContext = sHelpContext
  884. End Property
  885.  
  886. Public Property Let HelpContext(vNewValue As String)
  887.     'Assign object's HelpContext property
  888.     sHelpContext = vNewValue
  889. End Property
  890.  
  891. Public Property Get HelpFile() As String
  892.     'Return object's HelpFile property
  893.     HelpFile = sHelpFile
  894. End Property
  895.  
  896. Public Property Let HelpFile(vNewValue As String)
  897.     'Assign object's HelpFile property
  898.     sHelpFile = vNewValue
  899. End Property
  900.  
  901. Public Property Get HelpKey() As String
  902.     'Return object's HelpKey property
  903.     HelpKey = sHelpKey
  904. End Property
  905.  
  906. Public Property Let HelpKey(vNewValue As String)
  907.     'Assign object's HelpKey property
  908.     sHelpKey = vNewValue
  909. End Property
  910.  
  911. Public Property Get InitDir() As String
  912.     'Return object's InitDir property
  913.     InitDir = sInitDir
  914. End Property
  915.  
  916. Public Property Let InitDir(vNewValue As String)
  917.     'Assign object's InitDir property
  918.     sInitDir = vNewValue
  919. End Property
  920.  
  921. Public Property Get Max() As Long
  922.     'Return object's Max property
  923.     Max = lMax
  924. End Property
  925.  
  926. Public Property Let Max(vNewValue As Long)
  927.     'Assign object's - property
  928.     lMax = vNewValue
  929. End Property
  930.  
  931. Public Property Get MaxFileSize() As Long
  932.     'Return object's MaxFileSize property
  933.     MaxFileSize = lMaxFileSize
  934. End Property
  935.  
  936. Public Property Let MaxFileSize(vNewValue As Long)
  937.     'Assign object's MaxFileSize property
  938.     lMaxFileSize = vNewValue
  939. End Property
  940.  
  941. Public Property Get Min() As Long
  942.     'Return object's Min property
  943.     Min = lMin
  944. End Property
  945.  
  946. Public Property Let Min(vNewValue As Long)
  947.     'Assign object's Min property
  948.     lMin = vNewValue
  949. End Property
  950.  
  951. Public Property Get Object() As Object
  952.     'Return object's Object property
  953.     Object = objObject
  954. End Property
  955.  
  956. Public Property Let Object(vNewValue As Object)
  957.     'Assign object's Object property
  958.     objObject = vNewValue
  959. End Property
  960.  
  961. Public Property Get PrinterDefault() As Integer
  962.     'Return object's PrinterDefault property
  963.     PrinterDefault = iPrinterDefault
  964. End Property
  965.  
  966. Public Property Let PrinterDefault(vNewValue As Integer)
  967.     'Assign object's PrinterDefault property
  968.     iPrinterDefault = vNewValue
  969. End Property
  970.  
  971. Public Property Get ToPage() As Long
  972.     'Return object's ToPage property
  973.     ToPage = lToPage
  974. End Property
  975.  
  976. Public Property Let ToPage(vNewValue As Long)
  977.     'Assign object's ToPage property
  978.     lToPage = vNewValue
  979. End Property
  980.  
  981. Public Property Get FileTitle() As String
  982.     'return object's FileTitle property
  983.     FileTitle = sFileTitle
  984. End Property
  985.  
  986. Public Property Let FileTitle(vNewValue As String)
  987.     'assign object's FileTitle property
  988.     sFileTitle = vNewValue
  989. End Property
  990.  
  991. Public Property Get APIReturn() As Long
  992.     'return object's APIReturn property
  993.     APIReturn = lApiReturn
  994. End Property
  995.  
  996. Public Property Get ExtendedError() As Long
  997.     'return object's ExtendedError property
  998.     ExtendedError = lExtendedError
  999. End Property
  1000.  
  1001.  
  1002. Private Function sByteArrayToString(abBytes() As Byte) As String
  1003.     'return a string from a byte array
  1004.     Dim lBytePoint As Long
  1005.     Dim lByteVal As Long
  1006.     Dim sOut As String
  1007.    
  1008.     'init array pointer
  1009.     lBytePoint = LBound(abBytes)
  1010.    
  1011.     'fill sOut with characters in array
  1012.     While lBytePoint <= UBound(abBytes)
  1013.        
  1014.         lByteVal = abBytes(lBytePoint)
  1015.        
  1016.         'return sOut and stop if Chr$(0) is encountered
  1017.         If lByteVal = 0 Then
  1018.             sByteArrayToString = sOut
  1019.             Exit Function
  1020.         Else
  1021.             sOut = sOut & Chr$(lByteVal)
  1022.         End If
  1023.        
  1024.         lBytePoint = lBytePoint + 1
  1025.    
  1026.     Wend
  1027.    
  1028.     'return sOut if Chr$(0) wasn't encountered
  1029.     sByteArrayToString = sOut
  1030.    
  1031. End Function
  1032. Private Sub ShowFileDialog(ByVal iAction As Integer)
  1033.    
  1034.     'display the file dialog for ShowOpen or ShowSave
  1035.    
  1036.     Dim tOpenFile As OPENFILENAME
  1037.     Dim lMaxSize As Long
  1038.     Dim sFileNameBuff As String
  1039.     Dim sFileTitleBuff As String
  1040.    
  1041.     On Error GoTo ShowFileDialogError
  1042.    
  1043.    
  1044.     '***    init property buffers
  1045.    
  1046.     iAction = iAction  'Action property
  1047.     lApiReturn = 0  'APIReturn property
  1048.     lExtendedError = 0  'ExtendedError property
  1049.        
  1050.    
  1051.     '***    prepare tOpenFile data
  1052.    
  1053.     'tOpenFile.lStructSize As Long
  1054.     tOpenFile.lStructSize = Len(tOpenFile)
  1055.    
  1056.     'tOpenFile.hWndOwner As Long - init from hdc property
  1057.     tOpenFile.hwndOwner = lhwndOwner
  1058.    
  1059.     'tOpenFile.lpstrFilter As String - init from Filter property
  1060.     tOpenFile.lpstrFilter = sAPIFilter(sFilter)
  1061.        
  1062.     'tOpenFile.iFilterIndex As Long - init from FilterIndex property
  1063.     tOpenFile.iFilterIndex = iFilterIndex
  1064.    
  1065.     'tOpenFile.lpstrFile As String
  1066.         'determine size of buffer from MaxFileSize property
  1067.         If lMaxFileSize > 0 Then
  1068.             lMaxSize = lMaxFileSize
  1069.         Else
  1070.             lMaxSize = 255
  1071.         End If
  1072.    
  1073.     'tOpenFile.lpstrFile As Long - init from FileName property
  1074.         'prepare sFileNameBuff
  1075.         sFileNameBuff = sFileName
  1076.         'pad with spaces
  1077.         While Len(sFileNameBuff) < lMaxSize - 1
  1078.             sFileNameBuff = sFileNameBuff & " "
  1079.         Wend
  1080.         'trim to length of lMaxFileSize - 1
  1081.         sFileNameBuff = Mid$(sFileNameBuff, 1, lMaxSize - 1)
  1082.         'null terminate
  1083.         sFileNameBuff = sFileNameBuff & Chr$(0)
  1084.     tOpenFile.lpstrFile = sFileNameBuff
  1085.    
  1086.     'nMaxFile As Long - init from MaxFileSize property
  1087. '    If lMaxFileSize <> 255 Then  'default is 255
  1088.         tOpenFile.nMaxFile = lMaxSize
  1089. '    End If
  1090.            
  1091.     'lpstrFileTitle As String - init from FileTitle property
  1092.         'prepare sFileTitleBuff
  1093.         sFileTitleBuff = sFileTitle
  1094.         'pad with spaces
  1095.         While Len(sFileTitleBuff) < lMaxSize - 1
  1096.             sFileTitleBuff = sFileTitleBuff & " "
  1097.         Wend
  1098.         'trim to length of lMaxFileSize - 1
  1099.         sFileTitleBuff = Mid$(sFileTitleBuff, 1, lMaxSize - 1)
  1100.         'null terminate
  1101.         sFileTitleBuff = sFileTitleBuff & Chr$(0)
  1102.     tOpenFile.lpstrFileTitle = sFileTitleBuff
  1103.        
  1104.     'tOpenFile.lpstrInitialDir As String - init from InitDir property
  1105.     tOpenFile.lpstrInitialDir = sInitDir
  1106.    
  1107.     'tOpenFile.lpstrTitle As String - init from DialogTitle property
  1108.     tOpenFile.lpstrTitle = sDialogTitle
  1109.    
  1110.     'tOpenFile.flags As Long - init from Flags property
  1111.     tOpenFile.flags = lFlags
  1112.        
  1113.     'tOpenFile.lpstrDefExt As String - init from DefaultExt property
  1114.     tOpenFile.lpstrDefExt = sDefaultExt
  1115.    
  1116.    
  1117.     '***    call the GetOpenFileName API function
  1118.     Select Case iAction
  1119.         Case 1  'ShowOpen
  1120.             lApiReturn = GetOpenFileName(tOpenFile)
  1121.         Case 2  'ShowSave
  1122.             lApiReturn = GetSaveFileName(tOpenFile)
  1123.         Case Else   'unknown action
  1124.             Exit Sub
  1125.     End Select
  1126.    
  1127.    
  1128.     '***    handle return from GetOpenFileName API function
  1129.     Select Case lApiReturn
  1130.        
  1131.         Case 0  'user canceled
  1132.         If bCancelError = True Then
  1133.             'generate an error
  1134.             err.Raise (2001)
  1135.             Exit Sub
  1136.         End If
  1137.        
  1138.         Case 1  'user selected or entered a file
  1139.             'sFileName gets part of tOpenFile.lpstrFile to the left of first Chr$(0)
  1140.             sFileName = sLeftOfNull(tOpenFile.lpstrFile)
  1141.             sFileTitle = sLeftOfNull(tOpenFile.lpstrFileTitle)
  1142.        
  1143.         Case Else   'an error occured
  1144.             'call CommDlgExtendedError
  1145.             lExtendedError = CommDlgExtendedError
  1146.        
  1147.     End Select
  1148.    
  1149.  
  1150. Exit Sub
  1151.  
  1152. ShowFileDialogError:
  1153.    
  1154.     Exit Sub
  1155.  
  1156. End Sub
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.