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