home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Real-time_2028201112006.psc / cCommonDialog.cls < prev    next >
Text File  |  2005-06-06  |  28KB  |  863 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 = "cCommonDialog"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. 'I'm pretty sure I got this common dialog wrapper from http://vbaccelerator.com
  15. 'This is an older version of this project, but it still works like a champ.  Many
  16. 'thanks to Steve McMahon and his sweet site, http://vbaccelerator.com
  17.  
  18. Option Explicit
  19.  
  20.  
  21. Public Enum EErrorCommonDialog
  22.     eeBaseCommonDialog = 13450  ' CommonDialog
  23. End Enum
  24.  
  25. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
  26. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  27. Private Declare Function GlobalCompact Lib "kernel32" (ByVal dwMinFree As Long) As Long
  28. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  29. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
  30. Private Declare Function GlobalReAlloc Lib "kernel32" (ByVal hMem As Long, ByVal dwBytes As Long, ByVal wFlags As Long) As Long
  31. Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
  32. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  33. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  34.     lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  35. Private Declare Sub CopyMemoryStr Lib "kernel32" Alias "RtlMoveMemory" ( _
  36.     lpvDest As Any, ByVal lpvSource As String, ByVal cbCopy As Long)
  37.  
  38. Private Const MAX_PATH = 260
  39. Private Const MAX_FILE = 260
  40.  
  41. Private Type OPENFILENAME
  42.     lStructSize As Long          ' Filled with UDT size
  43.     hWndOwner As Long            ' Tied to Owner
  44.     hInstance As Long            ' Ignored (used only by templates)
  45.     lpstrFilter As String        ' Tied to Filter
  46.     lpstrCustomFilter As String  ' Ignored (exercise for reader)
  47.     nMaxCustFilter As Long       ' Ignored (exercise for reader)
  48.     nFilterIndex As Long         ' Tied to FilterIndex
  49.     lpstrFile As String          ' Tied to FileName
  50.     nMaxFile As Long             ' Handled internally
  51.     lpstrFileTitle As String     ' Tied to FileTitle
  52.     nMaxFileTitle As Long        ' Handled internally
  53.     lpstrInitialDir As String    ' Tied to InitDir
  54.     lpstrTitle As String         ' Tied to DlgTitle
  55.     flags As Long                ' Tied to Flags
  56.     nFileOffset As Integer       ' Ignored (exercise for reader)
  57.     nFileExtension As Integer    ' Ignored (exercise for reader)
  58.     lpstrDefExt As String        ' Tied to DefaultExt
  59.     lCustData As Long            ' Ignored (needed for hooks)
  60.     lpfnHook As Long             ' Ignored (good luck with hooks)
  61.     lpTemplateName As Long       ' Ignored (good luck with templates)
  62. End Type
  63.  
  64. Private Declare Function GetOpenFileName Lib "COMDLG32" _
  65.     Alias "GetOpenFileNameA" (file As OPENFILENAME) As Long
  66. Private Declare Function GetSaveFileName Lib "COMDLG32" _
  67.     Alias "GetSaveFileNameA" (file As OPENFILENAME) As Long
  68. Private Declare Function GetFileTitle Lib "COMDLG32" _
  69.     Alias "GetFileTitleA" (ByVal szFile As String, _
  70.     ByVal szTitle As String, ByVal cbBuf As Long) As Long
  71.  
  72. Public Enum EOpenFile
  73.     OFN_READONLY = &H1
  74.     OFN_OVERWRITEPROMPT = &H2
  75.     OFN_HIDEREADONLY = &H4
  76.     OFN_NOCHANGEDIR = &H8
  77.     OFN_SHOWHELP = &H10
  78.     OFN_ENABLEHOOK = &H20
  79.     OFN_ENABLETEMPLATE = &H40
  80.     OFN_ENABLETEMPLATEHANDLE = &H80
  81.     OFN_NOVALIDATE = &H100
  82.     OFN_ALLOWMULTISELECT = &H200
  83.     OFN_EXTENSIONDIFFERENT = &H400
  84.     OFN_PATHMUSTEXIST = &H800
  85.     OFN_FILEMUSTEXIST = &H1000
  86.     OFN_CREATEPROMPT = &H2000
  87.     OFN_SHAREAWARE = &H4000
  88.     OFN_NOREADONLYRETURN = &H8000&
  89.     OFN_NOTESTFILECREATE = &H10000
  90.     OFN_NONETWORKBUTTON = &H20000
  91.     OFN_NOLONGNAMES = &H40000
  92.     OFN_EXPLORER = &H80000
  93.     OFN_NODEREFERENCELINKS = &H100000
  94.     OFN_LONGNAMES = &H200000
  95. End Enum
  96.  
  97. Private Type TCHOOSECOLOR
  98.     lStructSize As Long
  99.     hWndOwner As Long
  100.     hInstance As Long
  101.     rgbResult As Long
  102.     lpCustColors As Long
  103.     flags As Long
  104.     lCustData As Long
  105.     lpfnHook As Long
  106.     lpTemplateName As Long
  107. End Type
  108.  
  109. Private Declare Function ChooseColor Lib "COMDLG32.DLL" _
  110.     Alias "ChooseColorA" (Color As TCHOOSECOLOR) As Long
  111.  
  112. Public Enum EChooseColor
  113.     CC_RGBInit = &H1
  114.     CC_FullOpen = &H2
  115.     CC_PreventFullOpen = &H4
  116.     CC_ColorShowHelp = &H8
  117. ' Win95 only
  118.     CC_SolidColor = &H80
  119.     CC_AnyColor = &H100
  120. ' End Win95 only
  121.     CC_ENABLEHOOK = &H10
  122.     CC_ENABLETEMPLATE = &H20
  123.     CC_EnableTemplateHandle = &H40
  124. End Enum
  125. Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
  126.  
  127. Private Type TCHOOSEFONT
  128.     lStructSize As Long         ' Filled with UDT size
  129.     hWndOwner As Long           ' Caller's window handle
  130.     hdc As Long                 ' Printer DC/IC or NULL
  131.     lpLogFont As Long           ' Pointer to LOGFONT
  132.     iPointSize As Long          ' 10 * size in points of font
  133.     flags As Long               ' Type flags
  134.     rgbColors As Long           ' Returned text color
  135.     lCustData As Long           ' Data passed to hook function
  136.     lpfnHook As Long            ' Pointer to hook function
  137.     lpTemplateName As Long      ' Custom template name
  138.     hInstance As Long           ' Instance handle for template
  139.     lpszStyle As String         ' Return style field
  140.     nFontType As Integer        ' Font type bits
  141.     iAlign As Integer           ' Filler
  142.     nSizeMin As Long            ' Minimum point size allowed
  143.     nSizeMax As Long            ' Maximum point size allowed
  144. End Type
  145. Private Declare Function ChooseFont Lib "COMDLG32" _
  146.     Alias "ChooseFontA" (chfont As TCHOOSEFONT) As Long
  147.  
  148. Private Const LF_FACESIZE = 32
  149. Private Type LOGFONT
  150.     lfHeight As Long
  151.     lfWidth As Long
  152.     lfEscapement As Long
  153.     lfOrientation As Long
  154.     lfWeight As Long
  155.     lfItalic As Byte
  156.     lfUnderline As Byte
  157.     lfStrikeOut As Byte
  158.     lfCharSet As Byte
  159.     lfOutPrecision As Byte
  160.     lfClipPrecision As Byte
  161.     lfQuality As Byte
  162.     lfPitchAndFamily As Byte
  163.     lfFaceName(LF_FACESIZE) As Byte
  164. End Type
  165.  
  166. Public Enum EChooseFont
  167.     CF_ScreenFonts = &H1
  168.     CF_PrinterFonts = &H2
  169.     CF_BOTH = &H3
  170.     CF_FontShowHelp = &H4
  171.     CF_UseStyle = &H80
  172.     CF_EFFECTS = &H100
  173.     CF_AnsiOnly = &H400
  174.     CF_NoVectorFonts = &H800
  175.     CF_NoOemFonts = CF_NoVectorFonts
  176.     CF_NoSimulations = &H1000
  177.     CF_LimitSize = &H2000
  178.     CF_FixedPitchOnly = &H4000
  179.     CF_WYSIWYG = &H8000&  ' Must also have ScreenFonts And PrinterFonts
  180.     CF_ForceFontExist = &H10000
  181.     CF_ScalableOnly = &H20000
  182.     CF_TTOnly = &H40000
  183.     CF_NoFaceSel = &H80000
  184.     CF_NoStyleSel = &H100000
  185.     CF_NoSizeSel = &H200000
  186.     ' Win95 only
  187.     CF_SelectScript = &H400000
  188.     CF_NoScriptSel = &H800000
  189.     CF_NoVertFonts = &H1000000
  190.  
  191.     CF_InitToLogFontStruct = &H40
  192.     CF_Apply = &H200
  193.     CF_EnableHook = &H8
  194.     CF_EnableTemplate = &H10
  195.     CF_EnableTemplateHandle = &H20
  196.     CF_FontNotSupported = &H238
  197. End Enum
  198.  
  199. ' These are extra nFontType bits that are added to what is returned to the
  200. ' EnumFonts callback routine
  201.  
  202. Public Enum EFontType
  203.     Simulated_FontType = &H8000&
  204.     Printer_FontType = &H4000
  205.     Screen_FontType = &H2000
  206.     Bold_FontType = &H100
  207.     Italic_FontType = &H200
  208.     Regular_FontType = &H400
  209. End Enum
  210.  
  211. Private Type TPRINTDLG
  212.     lStructSize As Long
  213.     hWndOwner As Long
  214.     hDevMode As Long
  215.     hDevNames As Long
  216.     hdc As Long
  217.     flags As Long
  218.     nFromPage As Integer
  219.     nToPage As Integer
  220.     nMinPage As Integer
  221.     nMaxPage As Integer
  222.     nCopies As Integer
  223.     hInstance As Long
  224.     lCustData As Long
  225.     lpfnPrintHook As Long
  226.     lpfnSetupHook As Long
  227.     lpPrintTemplateName As Long
  228.     lpSetupTemplateName As Long
  229.     hPrintTemplate As Long
  230.     hSetupTemplate As Long
  231. End Type
  232.  
  233. '  DEVMODE collation selections
  234. Private Const DMCOLLATE_FALSE = 0
  235. Private Const DMCOLLATE_TRUE = 1
  236.  
  237. Private Declare Function PrintDlg Lib "COMDLG32.DLL" _
  238.     Alias "PrintDlgA" (prtdlg As TPRINTDLG) As Integer
  239.  
  240. Public Enum EPrintDialog
  241.     PD_ALLPAGES = &H0
  242.     PD_SELECTION = &H1
  243.     PD_PAGENUMS = &H2
  244.     PD_NOSELECTION = &H4
  245.     PD_NOPAGENUMS = &H8
  246.     PD_COLLATE = &H10
  247.     PD_PRINTTOFILE = &H20
  248.     PD_PRINTSETUP = &H40
  249.     PD_NOWARNING = &H80
  250.     PD_RETURNDC = &H100
  251.     PD_RETURNIC = &H200
  252.     PD_RETURNDEFAULT = &H400
  253.     PD_SHOWHELP = &H800
  254.     PD_ENABLEPRINTHOOK = &H1000
  255.     PD_ENABLESETUPHOOK = &H2000
  256.     PD_ENABLEPRINTTEMPLATE = &H4000
  257.     PD_ENABLESETUPTEMPLATE = &H8000&
  258.     PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
  259.     PD_ENABLESETUPTEMPLATEHANDLE = &H20000
  260.     PD_USEDEVMODECOPIES = &H40000
  261.     PD_USEDEVMODECOPIESANDCOLLATE = &H40000
  262.     PD_DISABLEPRINTTOFILE = &H80000
  263.     PD_HIDEPRINTTOFILE = &H100000
  264.     PD_NONETWORKBUTTON = &H200000
  265. End Enum
  266.  
  267. Private Type DEVNAMES
  268.     wDriverOffset As Integer
  269.     wDeviceOffset As Integer
  270.     wOutputOffset As Integer
  271.     wDefault As Integer
  272. End Type
  273.  
  274. Private Const CCHDEVICENAME = 32
  275. Private Const CCHFORMNAME = 32
  276. Private Type DevMode
  277.     dmDeviceName As String * CCHDEVICENAME
  278.     dmSpecVersion As Integer
  279.     dmDriverVersion As Integer
  280.     dmSize As Integer
  281.     dmDriverExtra As Integer
  282.     dmFields As Long
  283.     dmOrientation As Integer
  284.     dmPaperSize As Integer
  285.     dmPaperLength As Integer
  286.     dmPaperWidth As Integer
  287.     dmScale As Integer
  288.     dmCopies As Integer
  289.     dmDefaultSource As Integer
  290.     dmPrintQuality As Integer
  291.     dmColor As Integer
  292.     dmDuplex As Integer
  293.     dmYResolution As Integer
  294.     dmTTOption As Integer
  295.     dmCollate As Integer
  296.     dmFormName As String * CCHFORMNAME
  297.     dmUnusedPadding As Integer
  298.     dmBitsPerPel As Integer
  299.     dmPelsWidth As Long
  300.     dmPelsHeight As Long
  301.     dmDisplayFlags As Long
  302.     dmDisplayFrequency As Long
  303. End Type
  304.  
  305. ' New Win95 Page Setup dialogs are up to you
  306. Private Type POINTL
  307.     x As Long
  308.     Y As Long
  309. End Type
  310. Private Type RECT
  311.     Left As Long
  312.     TOp As Long
  313.     Right As Long
  314.     Bottom As Long
  315. End Type
  316.  
  317.  
  318. Private Type TPAGESETUPDLG
  319.     lStructSize                 As Long
  320.     hWndOwner                   As Long
  321.     hDevMode                    As Long
  322.     hDevNames                   As Long
  323.     flags                       As Long
  324.     ptPaperSize                 As POINTL
  325.     rtMinMargin                 As RECT
  326.     rtMargin                    As RECT
  327.     hInstance                   As Long
  328.     lCustData                   As Long
  329.     lpfnPageSetupHook           As Long
  330.     lpfnPagePaintHook           As Long
  331.     lpPageSetupTemplateName     As Long
  332.     hPageSetupTemplate          As Long
  333. End Type
  334.  
  335. ' EPaperSize constants same as vbPRPS constants
  336. Public Enum EPaperSize
  337.     epsLetter = 1          ' Letter, 8 1/2 x 11 in.
  338.     epsLetterSmall         ' Letter Small, 8 1/2 x 11 in.
  339.     epsTabloid             ' Tabloid, 11 x 17 in.
  340.     epsLedger              ' Ledger, 17 x 11 in.
  341.     epsLegal               ' Legal, 8 1/2 x 14 in.
  342.     epsStatement           ' Statement, 5 1/2 x 8 1/2 in.
  343.     epsExecutive           ' Executive, 7 1/2 x 10 1/2 in.
  344.     epsA3                  ' A3, 297 x 420 mm
  345.     epsA4                  ' A4, 210 x 297 mm
  346.     epsA4Small             ' A4 Small, 210 x 297 mm
  347.     epsA5                  ' A5, 148 x 210 mm
  348.     epsB4                  ' B4, 250 x 354 mm
  349.     epsB5                  ' B5, 182 x 257 mm
  350.     epsFolio               ' Folio, 8 1/2 x 13 in.
  351.     epsQuarto              ' Quarto, 215 x 275 mm
  352.     eps10x14               ' 10 x 14 in.
  353.     eps11x17               ' 11 x 17 in.
  354.     epsNote                ' Note, 8 1/2 x 11 in.
  355.     epsEnv9                ' Envelope #9, 3 7/8 x 8 7/8 in.
  356.     epsEnv10               ' Envelope #10, 4 1/8 x 9 1/2 in.
  357.     epsEnv11               ' Envelope #11, 4 1/2 x 10 3/8 in.
  358.     epsEnv12               ' Envelope #12, 4 1/2 x 11 in.
  359.     epsEnv14               ' Envelope #14, 5 x 11 1/2 in.
  360.     epsCSheet              ' C size sheet
  361.     epsDSheet              ' D size sheet
  362.     epsESheet              ' E size sheet
  363.     epsEnvDL               ' Envelope DL, 110 x 220 mm
  364.     epsEnvC3               ' Envelope C3, 324 x 458 mm
  365.     epsEnvC4               ' Envelope C4, 229 x 324 mm
  366.     epsEnvC5               ' Envelope C5, 162 x 229 mm
  367.     epsEnvC6               ' Envelope C6, 114 x 162 mm
  368.     epsEnvC65              ' Envelope C65, 114 x 229 mm
  369.     epsEnvB4               ' Envelope B4, 250 x 353 mm
  370.     epsEnvB5               ' Envelope B5, 176 x 250 mm
  371.     epsEnvB6               ' Envelope B6, 176 x 125 mm
  372.     epsEnvItaly            ' Envelope, 110 x 230 mm
  373.     epsenvmonarch          ' Envelope Monarch, 3 7/8 x 7 1/2 in.
  374.     epsEnvPersonal         ' Envelope, 3 5/8 x 6 1/2 in.
  375.     epsFanfoldUS           ' U.S. Standard Fanfold, 14 7/8 x 11 in.
  376.     epsFanfoldStdGerman    ' German Standard Fanfold, 8 1/2 x 12 in.
  377.     epsFanfoldLglGerman    ' German Legal Fanfold, 8 1/2 x 13 in.
  378.     epsUser = 256          ' User-defined
  379. End Enum
  380.  
  381. ' EPrintQuality constants same as vbPRPQ constants
  382. Public Enum EPrintQuality
  383.     epqDraft = -1
  384.     epqLow = -2
  385.     epqMedium = -3
  386.     epqHigh = -4
  387. End Enum
  388.  
  389. Public Enum EOrientation
  390.     eoPortrait = 1
  391.     eoLandscape
  392. End Enum
  393.  
  394. Private Declare Function PageSetupDlg Lib "COMDLG32" _
  395.     Alias "PageSetupDlgA" (lppage As TPAGESETUPDLG) As Boolean
  396.  
  397. Public Enum EPageSetup
  398.     PSD_Defaultminmargins = &H0 ' Default (printer's)
  399.     PSD_InWinIniIntlMeasure = &H0
  400.     PSD_MINMARGINS = &H1
  401.     PSD_MARGINS = &H2
  402.     PSD_INTHOUSANDTHSOFINCHES = &H4
  403.     PSD_INHUNDREDTHSOFMILLIMETERS = &H8
  404.     PSD_DISABLEMARGINS = &H10
  405.     PSD_DISABLEPRINTER = &H20
  406.     PSD_NoWarning = &H80
  407.     PSD_DISABLEORIENTATION = &H100
  408.     PSD_ReturnDefault = &H400
  409.     PSD_DISABLEPAPER = &H200
  410.     PSD_ShowHelp = &H800
  411.     PSD_EnablePageSetupHook = &H2000
  412.     PSD_EnablePageSetupTemplate = &H8000&
  413.     PSD_EnablePageSetupTemplateHandle = &H20000
  414.     PSD_EnablePagePaintHook = &H40000
  415.     PSD_DisablePagePainting = &H80000
  416. End Enum
  417.  
  418. Public Enum EPageSetupUnits
  419.     epsuInches
  420.     epsuMillimeters
  421. End Enum
  422.  
  423. ' Common dialog errors
  424.  
  425. Private Declare Function CommDlgExtendedError Lib "COMDLG32" () As Long
  426.  
  427. Public Enum EDialogError
  428.     CDERR_DIALOGFAILURE = &HFFFF
  429.  
  430.     CDERR_GENERALCODES = &H0
  431.     CDERR_STRUCTSIZE = &H1
  432.     CDERR_INITIALIZATION = &H2
  433.     CDERR_NOTEMPLATE = &H3
  434.     CDERR_NOHINSTANCE = &H4
  435.     CDERR_LOADSTRFAILURE = &H5
  436.     CDERR_FINDRESFAILURE = &H6
  437.     CDERR_LOADRESFAILURE = &H7
  438.     CDERR_LOCKRESFAILURE = &H8
  439.     CDERR_MEMALLOCFAILURE = &H9
  440.     CDERR_MEMLOCKFAILURE = &HA
  441.     CDERR_NOHOOK = &HB
  442.     CDERR_REGISTERMSGFAIL = &HC
  443.  
  444.     PDERR_PRINTERCODES = &H1000
  445.     PDERR_SETUPFAILURE = &H1001
  446.     PDERR_PARSEFAILURE = &H1002
  447.     PDERR_RETDEFFAILURE = &H1003
  448.     PDERR_LOADDRVFAILURE = &H1004
  449.     PDERR_GETDEVMODEFAIL = &H1005
  450.     PDERR_INITFAILURE = &H1006
  451.     PDERR_NODEVICES = &H1007
  452.     PDERR_NODEFAULTPRN = &H1008
  453.     PDERR_DNDMMISMATCH = &H1009
  454.     PDERR_CREATEICFAILURE = &H100A
  455.     PDERR_PRINTERNOTFOUND = &H100B
  456.     PDERR_DEFAULTDIFFERENT = &H100C
  457.  
  458.     CFERR_CHOOSEFONTCODES = &H2000
  459.     CFERR_NOFONTS = &H2003
  460.     CFERR_MAXLESSTHANMIN = &H2003
  461.  
  462.     FNERR_FILENAMECODES = &H3000
  463.     FNERR_SUBCLASSFAILURE = &H3001
  464.     FNERR_INVALIDFILENAME = &H3002
  465.     FNERR_BUFFERTOOSMALL = &H3003
  466.  
  467.     CCERR_CHOOSECOLORCODES = &H5000
  468. End Enum
  469.  
  470. ' Array of custom colors lasts for life of app
  471. Private alCustom(0 To 15) As Long, fNotFirst As Boolean
  472.  
  473. Public Enum EPrintRange
  474.     eprAll
  475.     eprPageNumbers
  476.     eprSelection
  477. End Enum
  478. Private m_lApiReturn As Long
  479. Private m_lExtendedError As Long
  480. Private m_dvmode As DevMode
  481.  
  482. Public Property Get APIReturn() As Long
  483.     'return object's APIReturn property
  484.     APIReturn = m_lApiReturn
  485. End Property
  486. Public Property Get ExtendedError() As Long
  487.     'return object's ExtendedError property
  488.     ExtendedError = m_lExtendedError
  489. End Property
  490.  
  491. #If fComponent Then
  492. Private Sub Class_Initialize()
  493.     InitColors
  494. End Sub
  495. #End If
  496.  
  497. Function VBGetOpenFileName(Filename As String, _
  498.                            Optional FileTitle As String, _
  499.                            Optional FileMustExist As Boolean = True, _
  500.                            Optional MultiSelect As Boolean = False, _
  501.                            Optional ReadOnly As Boolean = False, _
  502.                            Optional HideReadOnly As Boolean = False, _
  503.                            Optional Filter As String = "All (*.*)| *.*", _
  504.                            Optional FilterIndex As Long = 1, _
  505.                            Optional InitDir As String, _
  506.                            Optional DlgTitle As String, _
  507.                            Optional DefaultExt As String, _
  508.                            Optional Owner As Long = -1, _
  509.                            Optional flags As Long = 0) As Boolean
  510.  
  511.     Dim opfile As OPENFILENAME, s As String
  512.     
  513.     m_lApiReturn = 0
  514.     m_lExtendedError = 0
  515.  
  516. With opfile
  517.     .lStructSize = Len(opfile)
  518.     
  519.     ' Add in specific flags and strip out non-VB flags
  520.     
  521.     .flags = (-FileMustExist * OFN_FILEMUSTEXIST) Or _
  522.             (-MultiSelect * OFN_ALLOWMULTISELECT) Or _
  523.              (-ReadOnly * OFN_READONLY) Or _
  524.              (-HideReadOnly * OFN_HIDEREADONLY) Or _
  525.              (flags And CLng(Not (OFN_ENABLEHOOK Or _
  526.                                   OFN_ENABLETEMPLATE)))
  527.     ' Owner can take handle of owning window
  528.     If Owner <> -1 Then .hWndOwner = Owner
  529.     ' InitDir can take initial directory string
  530.     .lpstrInitialDir = InitDir
  531.     ' DefaultExt can take default extension
  532.     .lpstrDefExt = DefaultExt
  533.     ' DlgTitle can take dialog box title
  534.     .lpstrTitle = DlgTitle
  535.     
  536.     ' To make Windows-style filter, replace | and : with nulls
  537.     Dim ch As String, i As Integer
  538.     For i = 1 To Len(Filter)
  539.         ch = Mid$(Filter, i, 1)
  540.         If ch = "|" Or ch = ":" Then
  541.             s = s & vbNullChar
  542.         Else
  543.             s = s & ch
  544.         End If
  545.     Next
  546.     ' Put double null at end
  547.     s = s & vbNullChar & vbNullChar
  548.     .lpstrFilter = s
  549.     .nFilterIndex = FilterIndex
  550.  
  551.     ' Pad file and file title buffers to maximum path
  552.     s = Filename & String$(MAX_PATH - Len(Filename), 0)
  553.     .lpstrFile = s
  554.     .nMaxFile = MAX_PATH
  555.     s = FileTitle & String$(MAX_FILE - Len(FileTitle), 0)
  556.     .lpstrFileTitle = s
  557.     .nMaxFileTitle = MAX_FILE
  558.     ' All other fields set to zero
  559.     
  560.     m_lApiReturn = GetOpenFileName(opfile)
  561.     Select Case m_lApiReturn
  562.     Case 1
  563.         ' Success
  564.         VBGetOpenFileName = True
  565.         Filename = StrZToStr(.lpstrFile)
  566.         FileTitle = StrZToStr(.lpstrFileTitle)
  567.         flags = .flags
  568.         ' Return the filter index
  569.         FilterIndex = .nFilterIndex
  570.         ' Look up the filter the user selected and return that
  571.         Filter = FilterLookup(.lpstrFilter, FilterIndex)
  572.         If (.flags And OFN_READONLY) Then ReadOnly = True
  573.     Case 0
  574.         ' Cancelled
  575.         VBGetOpenFileName = False
  576.         Filename = ""
  577.         FileTitle = ""
  578.         flags = 0
  579.         FilterIndex = -1
  580.         Filter = ""
  581.     Case Else
  582.         ' Extended error
  583.         m_lExtendedError = CommDlgExtendedError()
  584.         VBGetOpenFileName = False
  585.         Filename = ""
  586.         FileTitle = ""
  587.         flags = 0
  588.         FilterIndex = -1
  589.         Filter = ""
  590.     End Select
  591. End With
  592. End Function
  593. Private Function StrZToStr(s As String) As String
  594.     StrZToStr = Left$(s, lstrlen(s))
  595. End Function
  596.  
  597. Function VBGetSaveFileName(Filename As String, _
  598.                            Optional FileTitle As String, _
  599.                            Optional OverWritePrompt As Boolean = True, _
  600.                            Optional Filter As String = "All (*.*)| *.*", _
  601.                            Optional FilterIndex As Long = 1, _
  602.                            Optional InitDir As String, _
  603.                            Optional DlgTitle As String, _
  604.                            Optional DefaultExt As String, _
  605.                            Optional Owner As Long = -1, _
  606.                            Optional flags As Long) As Boolean
  607.             
  608.     Dim opfile As OPENFILENAME, s As String
  609.  
  610.     m_lApiReturn = 0
  611.     m_lExtendedError = 0
  612.  
  613. With opfile
  614.     .lStructSize = Len(opfile)
  615.     
  616.     ' Add in specific flags and strip out non-VB flags
  617.     .flags = (-OverWritePrompt * OFN_OVERWRITEPROMPT) Or _
  618.              OFN_HIDEREADONLY Or _
  619.              (flags And CLng(Not (OFN_ENABLEHOOK Or _
  620.                                   OFN_ENABLETEMPLATE)))
  621.     ' Owner can take handle of owning window
  622.     If Owner <> -1 Then .hWndOwner = Owner
  623.     ' InitDir can take initial directory string
  624.     .lpstrInitialDir = InitDir
  625.     ' DefaultExt can take default extension
  626.     .lpstrDefExt = DefaultExt
  627.     ' DlgTitle can take dialog box title
  628.     .lpstrTitle = DlgTitle
  629.     
  630.     ' Make new filter with bars (|) replacing nulls and double null at end
  631.     Dim ch As String, i As Integer
  632.     For i = 1 To Len(Filter)
  633.         ch = Mid$(Filter, i, 1)
  634.         If ch = "|" Or ch = ":" Then
  635.             s = s & vbNullChar
  636.         Else
  637.             s = s & ch
  638.         End If
  639.     Next
  640.     ' Put double null at end
  641.     s = s & vbNullChar & vbNullChar
  642.     .lpstrFilter = s
  643.     .nFilterIndex = FilterIndex
  644.  
  645.     ' Pad file and file title buffers to maximum path
  646.     s = Filename & String$(MAX_PATH - Len(Filename), 0)
  647.     .lpstrFile = s
  648.     .nMaxFile = MAX_PATH
  649.     s = FileTitle & String$(MAX_FILE - Len(FileTitle), 0)
  650.     .lpstrFileTitle = s
  651.     .nMaxFileTitle = MAX_FILE
  652.     ' All other fields zero
  653.     
  654.     m_lApiReturn = GetSaveFileName(opfile)
  655.     Select Case m_lApiReturn
  656.     Case 1
  657.         VBGetSaveFileName = True
  658.         Filename = StrZToStr(.lpstrFile)
  659.         FileTitle = StrZToStr(.lpstrFileTitle)
  660.         flags = .flags
  661.         ' Return the filter index
  662.         FilterIndex = .nFilterIndex
  663.         ' Look up the filter the user selected and return that
  664.         Filter = FilterLookup(.lpstrFilter, FilterIndex)
  665.     Case 0
  666.         ' Cancelled:
  667.         VBGetSaveFileName = False
  668.         Filename = ""
  669.         FileTitle = ""
  670.         flags = 0
  671.         FilterIndex = 0
  672.         Filter = ""
  673.     Case Else
  674.         ' Extended error:
  675.         VBGetSaveFileName = False
  676.         m_lExtendedError = CommDlgExtendedError()
  677.         Filename = ""
  678.         FileTitle = ""
  679.         flags = 0
  680.         FilterIndex = 0
  681.         Filter = ""
  682.     End Select
  683. End With
  684. End Function
  685.  
  686. Private Function FilterLookup(ByVal sFilters As String, ByVal iCur As Long) As String
  687.     Dim iStart As Long, iEnd As Long, s As String
  688.     iStart = 1
  689.     If sFilters = "" Then Exit Function
  690.     Do
  691.         ' Cut out both parts marked by null character
  692.         iEnd = InStr(iStart, sFilters, vbNullChar)
  693.         If iEnd = 0 Then Exit Function
  694.         iEnd = InStr(iEnd + 1, sFilters, vbNullChar)
  695.         If iEnd Then
  696.             s = Mid$(sFilters, iStart, iEnd - iStart)
  697.         Else
  698.             s = Mid$(sFilters, iStart)
  699.         End If
  700.         iStart = iEnd + 1
  701.         If iCur = 1 Then
  702.             FilterLookup = s
  703.             Exit Function
  704.         End If
  705.         iCur = iCur - 1
  706.     Loop While iCur
  707. End Function
  708.  
  709. Function VBGetFileTitle(sFile As String) As String
  710.     Dim sFileTitle As String, cFileTitle As Integer
  711.  
  712.     cFileTitle = MAX_PATH
  713.     sFileTitle = String$(MAX_PATH, 0)
  714.     cFileTitle = GetFileTitle(sFile, sFileTitle, MAX_PATH)
  715.     If cFileTitle Then
  716.         VBGetFileTitle = ""
  717.     Else
  718.         VBGetFileTitle = Left$(sFileTitle, InStr(sFileTitle, vbNullChar) - 1)
  719.     End If
  720.  
  721. End Function
  722.  
  723. ' ChooseColor wrapper
  724. Function VBChooseColor(Color As Long, _
  725.                        Optional AnyColor As Boolean = True, _
  726.                        Optional FullOpen As Boolean = False, _
  727.                        Optional DisableFullOpen As Boolean = False, _
  728.                        Optional Owner As Long = -1, _
  729.                        Optional flags As Long) As Boolean
  730.  
  731.     Dim chclr As TCHOOSECOLOR
  732.     chclr.lStructSize = Len(chclr)
  733.     
  734.     ' Color must get reference variable to receive result
  735.     ' Flags can get reference variable or constant with bit flags
  736.     ' Owner can take handle of owning window
  737.     If Owner <> -1 Then chclr.hWndOwner = Owner
  738.  
  739.     ' Assign color (default uninitialized value of zero is good default)
  740.     chclr.rgbResult = Color
  741.  
  742.     ' Mask out unwanted bits
  743.     Dim afMask As Long
  744.     afMask = CLng(Not (CC_ENABLEHOOK Or _
  745.                        CC_ENABLETEMPLATE))
  746.     ' Pass in flags
  747.     chclr.flags = afMask And (CC_RGBInit Or _
  748.                   IIf(AnyColor, CC_AnyColor, CC_SolidColor) Or _
  749.                   (-FullOpen * CC_FullOpen) Or _
  750.                   (-DisableFullOpen * CC_PreventFullOpen))
  751.  
  752.     ' If first time, initialize to white
  753.     If fNotFirst = False Then InitColors
  754.  
  755.     chclr.lpCustColors = VarPtr(alCustom(0))
  756.     ' All other fields zero
  757.     
  758.     m_lApiReturn = ChooseColor(chclr)
  759.     Select Case m_lApiReturn
  760.     Case 1
  761.         ' Success
  762.         VBChooseColor = True
  763.         Color = chclr.rgbResult
  764.     Case 0
  765.         ' Cancelled
  766.         VBChooseColor = False
  767.         Color = -1
  768.     Case Else
  769.         ' Extended error
  770.         m_lExtendedError = CommDlgExtendedError()
  771.         VBChooseColor = False
  772.         Color = -1
  773.     End Select
  774.  
  775. End Function
  776.  
  777. Private Sub InitColors()
  778.     Dim i As Integer
  779.     ' Initialize with first 16 system interface colors
  780.     For i = 0 To 15
  781.         alCustom(i) = GetSysColor(i)
  782.     Next
  783.     fNotFirst = True
  784. End Sub
  785.  
  786. ' Property to read or modify custom colors (use to save colors in registry)
  787. Public Property Get CustomColor(i As Integer) As Long
  788.     ' If first time, initialize to white
  789.     If fNotFirst = False Then InitColors
  790.     If i >= 0 And i <= 15 Then
  791.         CustomColor = alCustom(i)
  792.     Else
  793.         CustomColor = -1
  794.     End If
  795. End Property
  796.  
  797. Public Property Let CustomColor(i As Integer, iValue As Long)
  798.     ' If first time, initialize to system colors
  799.     If fNotFirst = False Then InitColors
  800.     If i >= 0 And i <= 15 Then
  801.         alCustom(i) = iValue
  802.     End If
  803. End Property
  804.  
  805. Private Property Get DevMode() As DevMode
  806.     DevMode = m_dvmode
  807. End Property
  808.  
  809. #If fComponent = 0 Then
  810. Private Sub ErrRaise(e As Long)
  811.     Dim sText As String, sSource As String
  812.     If e > 1000 Then
  813.         sSource = App.EXEName & ".CommonDialog"
  814.         Err.Raise COMError(e), sSource, sText
  815.     Else
  816.         ' Raise standard Visual Basic error
  817.         sSource = App.EXEName & ".VBError"
  818.         Err.Raise e, sSource
  819.     End If
  820. End Sub
  821. #End If
  822.  
  823.  
  824. Private Sub StrToBytes(ab() As Byte, s As String)
  825.     If IsArrayEmpty(ab) Then
  826.         ' Assign to empty array
  827.         ab = StrConv(s, vbFromUnicode)
  828.     Else
  829.         Dim cab As Long
  830.         ' Copy to existing array, padding or truncating if necessary
  831.         cab = UBound(ab) - LBound(ab) + 1
  832.         If Len(s) < cab Then s = s & String$(cab - Len(s), 0)
  833.         'If UnicodeTypeLib Then
  834.         '    Dim st As String
  835.         '    st = StrConv(s, vbFromUnicode)
  836.         '    CopyMemoryStr ab(LBound(ab)), st, cab
  837.         'Else
  838.             CopyMemoryStr ab(LBound(ab)), s, cab
  839.         'End If
  840.     End If
  841. End Sub
  842.  
  843.  
  844. Private Function BytesToStr(ab() As Byte) As String
  845.     BytesToStr = StrConv(ab, vbUnicode)
  846. End Function
  847.  
  848. Private Function COMError(e As Long) As Long
  849.     COMError = e Or vbObjectError
  850. End Function
  851. '
  852. Private Function IsArrayEmpty(va As Variant) As Boolean
  853.     Dim v As Variant
  854.     On Error Resume Next
  855.     v = va(LBound(va))
  856.     IsArrayEmpty = (Err <> 0)
  857. End Function
  858.  
  859.  
  860.  
  861.  
  862.  
  863.