home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD44023302000.psc / GCommonDialog.cls < prev    next >
Encoding:
Visual Basic class definition  |  2000-03-25  |  51.0 KB  |  1,513 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 = "GCommonDialog"
  10. Attribute VB_GlobalNameSpace = True
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Public Enum EErrorCommonDialog
  17.     eeBaseCommonDialog = 13450  ' CommonDialog
  18. End Enum
  19.  
  20. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
  21. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  22. Private Declare Function GlobalCompact Lib "kernel32" (ByVal dwMinFree As Long) As Long
  23. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  24. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
  25. Private Declare Function GlobalReAlloc Lib "kernel32" (ByVal hMem As Long, ByVal dwBytes As Long, ByVal wFlags As Long) As Long
  26. Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
  27. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  28. Private Declare Sub CopyMemoryStr Lib "kernel32" Alias "RtlMoveMemory" ( _
  29.     lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  30.  
  31. Private Const MAX_PATH = 260
  32. Private Const MAX_FILE = 260
  33.  
  34. Private Type OPENFILENAME
  35.     lStructSize As Long          ' Filled with UDT size
  36.     hWndOwner As Long            ' Tied to Owner
  37.     hInstance As Long            ' Ignored (used only by templates)
  38.     lpstrFilter As String        ' Tied to Filter
  39.     lpstrCustomFilter As String  ' Ignored (exercise for reader)
  40.     nMaxCustFilter As Long       ' Ignored (exercise for reader)
  41.     nFilterIndex As Long         ' Tied to FilterIndex
  42.     lpstrFile As String          ' Tied to FileName
  43.     nMaxFile As Long             ' Handled internally
  44.     lpstrFileTitle As String     ' Tied to FileTitle
  45.     nMaxFileTitle As Long        ' Handled internally
  46.     lpstrInitialDir As String    ' Tied to InitDir
  47.     lpstrTitle As String         ' Tied to DlgTitle
  48.     flags As Long                ' Tied to Flags
  49.     nFileOffset As Integer       ' Ignored (exercise for reader)
  50.     nFileExtension As Integer    ' Ignored (exercise for reader)
  51.     lpstrDefExt As String        ' Tied to DefaultExt
  52.     lCustData As Long            ' Ignored (needed for hooks)
  53.     lpfnHook As Long             ' Ignored (good luck with hooks)
  54.     lpTemplateName As Long       ' Ignored (good luck with templates)
  55. End Type
  56.  
  57. Private Declare Function GetOpenFileName Lib "COMDLG32" _
  58.     Alias "GetOpenFileNameA" (file As OPENFILENAME) As Long
  59. Private Declare Function GetSaveFileName Lib "COMDLG32" _
  60.     Alias "GetSaveFileNameA" (file As OPENFILENAME) As Long
  61. Private Declare Function GetFileTitle Lib "COMDLG32" _
  62.     Alias "GetFileTitleA" (ByVal szFile As String, _
  63.     ByVal szTitle As String, ByVal cbBuf As Long) As Long
  64.  
  65. Public Enum EOpenFile
  66.     OFN_READONLY = &H1
  67.     OFN_OVERWRITEPROMPT = &H2
  68.     OFN_HIDEREADONLY = &H4
  69.     OFN_NOCHANGEDIR = &H8
  70.     OFN_SHOWHELP = &H10
  71.     OFN_ENABLEHOOK = &H20
  72.     OFN_ENABLETEMPLATE = &H40
  73.     OFN_ENABLETEMPLATEHANDLE = &H80
  74.     OFN_NOVALIDATE = &H100
  75.     OFN_ALLOWMULTISELECT = &H200
  76.     OFN_EXTENSIONDIFFERENT = &H400
  77.     OFN_PATHMUSTEXIST = &H800
  78.     OFN_FILEMUSTEXIST = &H1000
  79.     OFN_CREATEPROMPT = &H2000
  80.     OFN_SHAREAWARE = &H4000
  81.     OFN_NOREADONLYRETURN = &H8000
  82.     OFN_NOTESTFILECREATE = &H10000
  83.     OFN_NONETWORKBUTTON = &H20000
  84.     OFN_NOLONGNAMES = &H40000
  85.     OFN_EXPLORER = &H80000
  86.     OFN_NODEREFERENCELINKS = &H100000
  87.     OFN_LONGNAMES = &H200000
  88. End Enum
  89.  
  90. Private Type TCHOOSECOLOR
  91.     lStructSize As Long
  92.     hWndOwner As Long
  93.     hInstance As Long
  94.     rgbResult As Long
  95.     lpCustColors As Long
  96.     flags As Long
  97.     lCustData As Long
  98.     lpfnHook As Long
  99.     lpTemplateName As Long
  100. End Type
  101.  
  102. Private Declare Function ChooseColor Lib "COMDLG32.DLL" _
  103.     Alias "ChooseColorA" (Color As TCHOOSECOLOR) As Long
  104.  
  105. Public Enum EChooseColor
  106.     CC_RGBInit = &H1
  107.     CC_FullOpen = &H2
  108.     CC_PreventFullOpen = &H4
  109.     CC_ColorShowHelp = &H8
  110. ' Win95 only
  111.     CC_SolidColor = &H80
  112.     CC_AnyColor = &H100
  113. ' End Win95 only
  114.     CC_ENABLEHOOK = &H10
  115.     CC_ENABLETEMPLATE = &H20
  116.     CC_EnableTemplateHandle = &H40
  117. End Enum
  118. Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
  119.  
  120. Private Type TCHOOSEFONT
  121.     lStructSize As Long         ' Filled with UDT size
  122.     hWndOwner As Long           ' Caller's window handle
  123.     hdc As Long                 ' Printer DC/IC or NULL
  124.     lpLogFont As Long           ' Pointer to LOGFONT
  125.     iPointSize As Long          ' 10 * size in points of font
  126.     flags As Long               ' Type flags
  127.     rgbColors As Long           ' Returned text color
  128.     lCustData As Long           ' Data passed to hook function
  129.     lpfnHook As Long            ' Pointer to hook function
  130.     lpTemplateName As Long      ' Custom template name
  131.     hInstance As Long           ' Instance handle for template
  132.     lpszStyle As String         ' Return style field
  133.     nFontType As Integer        ' Font type bits
  134.     iAlign As Integer           ' Filler
  135.     nSizeMin As Long            ' Minimum point size allowed
  136.     nSizeMax As Long            ' Maximum point size allowed
  137. End Type
  138. Private Declare Function ChooseFont Lib "COMDLG32" _
  139.     Alias "ChooseFontA" (chfont As TCHOOSEFONT) As Long
  140.  
  141. Private Const LF_FACESIZE = 32
  142. Private Type LOGFONT
  143.     lfHeight As Long
  144.     lfWidth As Long
  145.     lfEscapement As Long
  146.     lfOrientation As Long
  147.     lfWeight As Long
  148.     lfItalic As Byte
  149.     lfUnderline As Byte
  150.     lfStrikeOut As Byte
  151.     lfCharSet As Byte
  152.     lfOutPrecision As Byte
  153.     lfClipPrecision As Byte
  154.     lfQuality As Byte
  155.     lfPitchAndFamily As Byte
  156.     lfFaceName(LF_FACESIZE) As Byte
  157. End Type
  158.  
  159. Public Enum EChooseFont
  160.     CF_ScreenFonts = &H1
  161.     CF_PrinterFonts = &H2
  162.     CF_BOTH = &H3
  163.     CF_FontShowHelp = &H4
  164.     CF_UseStyle = &H80
  165.     CF_EFFECTS = &H100
  166.     CF_AnsiOnly = &H400
  167.     CF_NoVectorFonts = &H800
  168.     CF_NoOemFonts = CF_NoVectorFonts
  169.     CF_NoSimulations = &H1000
  170.     CF_LimitSize = &H2000
  171.     CF_FixedPitchOnly = &H4000
  172.     CF_WYSIWYG = &H8000  ' Must also have ScreenFonts And PrinterFonts
  173.     CF_ForceFontExist = &H10000
  174.     CF_ScalableOnly = &H20000
  175.     CF_TTOnly = &H40000
  176.     CF_NoFaceSel = &H80000
  177.     CF_NoStyleSel = &H100000
  178.     CF_NoSizeSel = &H200000
  179.     ' Win95 only
  180.     CF_SelectScript = &H400000
  181.     CF_NoScriptSel = &H800000
  182.     CF_NoVertFonts = &H1000000
  183.  
  184.     CF_InitToLogFontStruct = &H40
  185.     CF_Apply = &H200
  186.     CF_EnableHook = &H8
  187.     CF_EnableTemplate = &H10
  188.     CF_EnableTemplateHandle = &H20
  189.     CF_FontNotSupported = &H238
  190. End Enum
  191.  
  192. ' These are extra nFontType bits that are added to what is returned to the
  193. ' EnumFonts callback routine
  194.  
  195. Public Enum EFontType
  196.     Simulated_FontType = &H8000
  197.     Printer_FontType = &H4000
  198.     Screen_FontType = &H2000
  199.     Bold_FontType = &H100
  200.     Italic_FontType = &H200
  201.     Regular_FontType = &H400
  202. End Enum
  203.  
  204. Private Type TPRINTDLG
  205.     lStructSize As Long
  206.     hWndOwner As Long
  207.     hDevMode As Long
  208.     hDevNames As Long
  209.     hdc As Long
  210.     flags As Long
  211.     nFromPage As Integer
  212.     nToPage As Integer
  213.     nMinPage As Integer
  214.     nMaxPage As Integer
  215.     nCopies As Integer
  216.     hInstance As Long
  217.     lCustData As Long
  218.     lpfnPrintHook As Long
  219.     lpfnSetupHook As Long
  220.     lpPrintTemplateName As Long
  221.     lpSetupTemplateName As Long
  222.     hPrintTemplate As Long
  223.     hSetupTemplate As Long
  224. End Type
  225.  
  226. '  DEVMODE collation selections
  227. Private Const DMCOLLATE_FALSE = 0
  228. Private Const DMCOLLATE_TRUE = 1
  229.  
  230. Private Declare Function PrintDlg Lib "COMDLG32.DLL" _
  231.     Alias "PrintDlgA" (prtdlg As TPRINTDLG) As Integer
  232.  
  233. Public Enum EPrintDialog
  234.     PD_ALLPAGES = &H0
  235.     PD_SELECTION = &H1
  236.     PD_PAGENUMS = &H2
  237.     PD_NOSELECTION = &H4
  238.     PD_NOPAGENUMS = &H8
  239.     PD_COLLATE = &H10
  240.     PD_PRINTTOFILE = &H20
  241.     PD_PRINTSETUP = &H40
  242.     PD_NOWARNING = &H80
  243.     PD_RETURNDC = &H100
  244.     PD_RETURNIC = &H200
  245.     PD_RETURNDEFAULT = &H400
  246.     PD_SHOWHELP = &H800
  247.     PD_ENABLEPRINTHOOK = &H1000
  248.     PD_ENABLESETUPHOOK = &H2000
  249.     PD_ENABLEPRINTTEMPLATE = &H4000
  250.     PD_ENABLESETUPTEMPLATE = &H8000
  251.     PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
  252.     PD_ENABLESETUPTEMPLATEHANDLE = &H20000
  253.     PD_USEDEVMODECOPIES = &H40000
  254.     PD_USEDEVMODECOPIESANDCOLLATE = &H40000
  255.     PD_DISABLEPRINTTOFILE = &H80000
  256.     PD_HIDEPRINTTOFILE = &H100000
  257.     PD_NONETWORKBUTTON = &H200000
  258. End Enum
  259.  
  260. Private Type DEVNAMES
  261.     wDriverOffset As Integer
  262.     wDeviceOffset As Integer
  263.     wOutputOffset As Integer
  264.     wDefault As Integer
  265. End Type
  266.  
  267. Private Const CCHDEVICENAME = 32
  268. Private Const CCHFORMNAME = 32
  269. Private Type DevMode
  270.     dmDeviceName As String * CCHDEVICENAME
  271.     dmSpecVersion As Integer
  272.     dmDriverVersion As Integer
  273.     dmSize As Integer
  274.     dmDriverExtra As Integer
  275.     dmFields As Long
  276.     dmOrientation As Integer
  277.     dmPaperSize As Integer
  278.     dmPaperLength As Integer
  279.     dmPaperWidth As Integer
  280.     dmScale As Integer
  281.     dmCopies As Integer
  282.     dmDefaultSource As Integer
  283.     dmPrintQuality As Integer
  284.     dmColor As Integer
  285.     dmDuplex As Integer
  286.     dmYResolution As Integer
  287.     dmTTOption As Integer
  288.     dmCollate As Integer
  289.     dmFormName As String * CCHFORMNAME
  290.     dmUnusedPadding As Integer
  291.     dmBitsPerPel As Integer
  292.     dmPelsWidth As Long
  293.     dmPelsHeight As Long
  294.     dmDisplayFlags As Long
  295.     dmDisplayFrequency As Long
  296. End Type
  297.  
  298. ' New Win95 Page Setup dialogs are up to you
  299. Private Type POINTL
  300.     x As Long
  301.     y As Long
  302. End Type
  303. Private Type RECT
  304.     Left As Long
  305.     Top As Long
  306.     Right As Long
  307.     Bottom As Long
  308. End Type
  309.  
  310.  
  311. Private Type TPAGESETUPDLG
  312.     lStructSize                 As Long
  313.     hWndOwner                   As Long
  314.     hDevMode                    As Long
  315.     hDevNames                   As Long
  316.     flags                       As Long
  317.     ptPaperSize                 As POINTL
  318.     rtMinMargin                 As RECT
  319.     rtMargin                    As RECT
  320.     hInstance                   As Long
  321.     lCustData                   As Long
  322.     lpfnPageSetupHook           As Long
  323.     lpfnPagePaintHook           As Long
  324.     lpPageSetupTemplateName     As Long
  325.     hPageSetupTemplate          As Long
  326. End Type
  327.  
  328. ' EPaperSize constants same as vbPRPS constants
  329. Public Enum EPaperSize
  330.     epsLetter = 1          ' Letter, 8 1/2 x 11 in.
  331.     epsLetterSmall         ' Letter Small, 8 1/2 x 11 in.
  332.     epsTabloid             ' Tabloid, 11 x 17 in.
  333.     epsLedger              ' Ledger, 17 x 11 in.
  334.     epsLegal               ' Legal, 8 1/2 x 14 in.
  335.     epsStatement           ' Statement, 5 1/2 x 8 1/2 in.
  336.     epsExecutive           ' Executive, 7 1/2 x 10 1/2 in.
  337.     epsA3                  ' A3, 297 x 420 mm
  338.     epsA4                  ' A4, 210 x 297 mm
  339.     epsA4Small             ' A4 Small, 210 x 297 mm
  340.     epsA5                  ' A5, 148 x 210 mm
  341.     epsB4                  ' B4, 250 x 354 mm
  342.     epsB5                  ' B5, 182 x 257 mm
  343.     epsFolio               ' Folio, 8 1/2 x 13 in.
  344.     epsQuarto              ' Quarto, 215 x 275 mm
  345.     eps10x14               ' 10 x 14 in.
  346.     eps11x17               ' 11 x 17 in.
  347.     epsNote                ' Note, 8 1/2 x 11 in.
  348.     epsEnv9                ' Envelope #9, 3 7/8 x 8 7/8 in.
  349.     epsEnv10               ' Envelope #10, 4 1/8 x 9 1/2 in.
  350.     epsEnv11               ' Envelope #11, 4 1/2 x 10 3/8 in.
  351.     epsEnv12               ' Envelope #12, 4 1/2 x 11 in.
  352.     epsEnv14               ' Envelope #14, 5 x 11 1/2 in.
  353.     epsCSheet              ' C size sheet
  354.     epsDSheet              ' D size sheet
  355.     epsESheet              ' E size sheet
  356.     epsEnvDL               ' Envelope DL, 110 x 220 mm
  357.     epsEnvC3               ' Envelope C3, 324 x 458 mm
  358.     epsEnvC4               ' Envelope C4, 229 x 324 mm
  359.     epsEnvC5               ' Envelope C5, 162 x 229 mm
  360.     epsEnvC6               ' Envelope C6, 114 x 162 mm
  361.     epsEnvC65              ' Envelope C65, 114 x 229 mm
  362.     epsEnvB4               ' Envelope B4, 250 x 353 mm
  363.     epsEnvB5               ' Envelope B5, 176 x 250 mm
  364.     epsEnvB6               ' Envelope B6, 176 x 125 mm
  365.     epsEnvItaly            ' Envelope, 110 x 230 mm
  366.     epsenvmonarch          ' Envelope Monarch, 3 7/8 x 7 1/2 in.
  367.     epsEnvPersonal         ' Envelope, 3 5/8 x 6 1/2 in.
  368.     epsFanfoldUS           ' U.S. Standard Fanfold, 14 7/8 x 11 in.
  369.     epsFanfoldStdGerman    ' German Standard Fanfold, 8 1/2 x 12 in.
  370.     epsFanfoldLglGerman    ' German Legal Fanfold, 8 1/2 x 13 in.
  371.     epsUser = 256          ' User-defined
  372. End Enum
  373.  
  374. ' EPrintQuality constants same as vbPRPQ constants
  375. Public Enum EPrintQuality
  376.     epqDraft = -1
  377.     epqLow = -2
  378.     epqMedium = -3
  379.     epqHigh = -4
  380. End Enum
  381.  
  382. Public Enum EOrientation
  383.     eoPortrait = 1
  384.     eoLandscape
  385. End Enum
  386.  
  387. Private Declare Function PageSetupDlg Lib "COMDLG32" _
  388.     Alias "PageSetupDlgA" (lppage As TPAGESETUPDLG) As Boolean
  389.  
  390. Public Enum EPageSetup
  391.     PSD_Defaultminmargins = &H0 ' Default (printer's)
  392.     PSD_InWinIniIntlMeasure = &H0
  393.     PSD_MINMARGINS = &H1
  394.     PSD_MARGINS = &H2
  395.     PSD_INTHOUSANDTHSOFINCHES = &H4
  396.     PSD_INHUNDREDTHSOFMILLIMETERS = &H8
  397.     PSD_DISABLEMARGINS = &H10
  398.     PSD_DISABLEPRINTER = &H20
  399.     PSD_NoWarning = &H80
  400.     PSD_DISABLEORIENTATION = &H100
  401.     PSD_ReturnDefault = &H400
  402.     PSD_DISABLEPAPER = &H200
  403.     PSD_ShowHelp = &H800
  404.     PSD_EnablePageSetupHook = &H2000
  405.     PSD_EnablePageSetupTemplate = &H8000
  406.     PSD_EnablePageSetupTemplateHandle = &H20000
  407.     PSD_EnablePagePaintHook = &H40000
  408.     PSD_DisablePagePainting = &H80000
  409. End Enum
  410.  
  411.  
  412. Public Enum EPageSetupUnits
  413.     epsuInches
  414.     epsuMillimeters
  415. End Enum
  416.  
  417. ' Common dialog errors
  418.  
  419. Private Declare Function CommDlgExtendedError Lib "COMDLG32" () As Long
  420.  
  421. Public Enum EDialogError
  422.     CDERR_DIALOGFAILURE = &HFFFF
  423.  
  424.     CDERR_GENERALCODES = &H0&
  425.     CDERR_STRUCTSIZE = &H1&
  426.     CDERR_INITIALIZATION = &H2&
  427.     CDERR_NOTEMPLATE = &H3&
  428.     CDERR_NOHINSTANCE = &H4&
  429.     CDERR_LOADSTRFAILURE = &H5&
  430.     CDERR_FINDRESFAILURE = &H6&
  431.     CDERR_LOADRESFAILURE = &H7&
  432.     CDERR_LOCKRESFAILURE = &H8&
  433.     CDERR_MEMALLOCFAILURE = &H9&
  434.     CDERR_MEMLOCKFAILURE = &HA&
  435.     CDERR_NOHOOK = &HB&
  436.     CDERR_REGISTERMSGFAIL = &HC&
  437.  
  438.     PDERR_PRINTERCODES = &H1000&
  439.     PDERR_SETUPFAILURE = &H1001&
  440.     PDERR_PARSEFAILURE = &H1002&
  441.     PDERR_RETDEFFAILURE = &H1003&
  442.     PDERR_LOADDRVFAILURE = &H1004&
  443.     PDERR_GETDEVMODEFAIL = &H1005&
  444.     PDERR_INITFAILURE = &H1006&
  445.     PDERR_NODEVICES = &H1007&
  446.     PDERR_NODEFAULTPRN = &H1008&
  447.     PDERR_DNDMMISMATCH = &H1009&
  448.     PDERR_CREATEICFAILURE = &H100A&
  449.     PDERR_PRINTERNOTFOUND = &H100B&
  450.     PDERR_DEFAULTDIFFERENT = &H100C&
  451.  
  452.     CFERR_CHOOSEFONTCODES = &H2000&
  453.     CFERR_NOFONTS = &H2001&
  454.     CFERR_MAXLESSTHANMIN = &H2002&
  455.  
  456.     FNERR_FILENAMECODES = &H3000&
  457.     FNERR_SUBCLASSFAILURE = &H3001&
  458.     FNERR_INVALIDFILENAME = &H3002&
  459.     FNERR_BUFFERTOOSMALL = &H3003&
  460.  
  461.     CCERR_CHOOSECOLORCODES = &H5000&
  462. End Enum
  463.  
  464. ' Hook and notification support:
  465. Private Type NMHDR
  466.     hwndFrom As Long
  467.     idfrom As Long
  468.     code As Long
  469. End Type
  470. '// Structure used for all file based OpenFileName notifications
  471. Private Type OFNOTIFY
  472.     hdr As NMHDR
  473.     lpOFN As Long           ' Long pointer to OFN structure
  474.     pszFile As String ';        // May be NULL
  475. End Type
  476.  
  477. '// Structure used for all object based OpenFileName notifications
  478. Private Type OFNOTIFYEX
  479.     hdr As NMHDR
  480.     lpOFN As Long       ' Long pointer to OFN structure
  481.     psf As Long
  482.     LPVOID As Long          '// May be NULL
  483. End Type
  484.  
  485. Private Type OFNOTIFYshort
  486.     hdr As NMHDR
  487.     lpOFN As Long
  488. End Type
  489.  
  490. ' Messages:
  491. Private Const WM_DESTROY = &H2
  492. Private Const WM_NOTIFY = &H4E
  493. Private Const WM_NCDESTROY = &H82
  494. Private Const WM_GETDLGCODE = &H87
  495. Private Const WM_INITDIALOG = &H110
  496. Private Const WM_COMMAND = &H111
  497. Private Const WM_USER = &H400
  498.  
  499.  
  500. ' Notification codes:
  501. Private Const H_MAX As Long = &HFFFF + 1
  502. Private Const CDN_FIRST = (H_MAX - 601)
  503. Private Const CDN_LAST = (H_MAX - 699)
  504.  
  505. '// Notifications when Open or Save dialog status changes
  506. Private Const CDN_INITDONE = (CDN_FIRST - &H0)
  507. Private Const CDN_SELCHANGE = (CDN_FIRST - &H1)
  508. Private Const CDN_FOLDERCHANGE = (CDN_FIRST - &H2)
  509. Private Const CDN_SHAREVIOLATION = (CDN_FIRST - &H3)
  510. Private Const CDN_HELP = (CDN_FIRST - &H4)
  511. Private Const CDN_FILEOK = (CDN_FIRST - &H5)
  512. Private Const CDN_TYPECHANGE = (CDN_FIRST - &H6)
  513. Private Const CDN_INCLUDEITEM = (CDN_FIRST - &H7)
  514.  
  515. ' Messages which can be sent to the standard dialog elements
  516. Private Const CDM_FIRST = (WM_USER + 100)
  517. Private Const CDM_LAST = (WM_USER + 200)
  518.  
  519. Private Const CDM_GETSPEC = (CDM_FIRST + &H0)
  520. Private Const CDM_GETFILEPATH = (CDM_FIRST + &H1)
  521. Private Const CDM_GETFOLDERPATH = (CDM_FIRST + &H2)
  522. Private Const CDM_GETFOLDERIDLIST = (CDM_FIRST + &H3)
  523. Private Const CDM_SETCONTROLTEXT = (CDM_FIRST + &H4)
  524. Private Const CDM_HIDECONTROL = (CDM_FIRST + &H5)
  525. Private Const CDM_SETDEFEXT = (CDM_FIRST + &H6)
  526.  
  527. ' IDs for standard common dialog controls
  528. Private Const ID_OPEN = &H1  'Open or Save button
  529. Private Const ID_CANCEL = &H2 'Cancel Button
  530. Private Const ID_HELP = &H40E 'Help Button
  531. Private Const ID_READONLY = &H410 'Read-only check box
  532. Private Const ID_FILETYPELABEL = &H441 'Files of type label
  533. Private Const ID_FILELABEL = &H442 'File name label
  534. Private Const ID_FOLDERLABEL = &H443 'Look in label
  535. Private Const ID_LIST = &H461 'Parent of file list
  536. Private Const ID_FORMAT = &H470 'File type combo box
  537. Private Const ID_FOLDER = &H471 'Folder combo box
  538. Private Const ID_FILETEXT = &H480 'File name text box
  539.  
  540. Private Const DWL_MSGRESULT = 0
  541. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  542.  
  543. ' ==========================================================================
  544. ' Implementation:
  545. ' ==========================================================================
  546.  
  547. ' Array of custom colors lasts for life of app
  548. Private alCustom(0 To 15) As Long, fNotFirst As Boolean
  549. Public Enum EPrintRange
  550.     eprAll
  551.     eprPageNumbers
  552.     eprSelection
  553. End Enum
  554. Private m_lApiReturn As Long
  555. Private m_lExtendedError As Long
  556. Private m_dvmode As DevMode
  557. Private m_oEventSink As Object
  558.  
  559. Public Function DialogHook( _
  560.         ByVal hDlg As Long, _
  561.         ByVal msg As Long, _
  562.         ByVal wParam As Long, _
  563.         ByVal lParam As Long _
  564.     )
  565. Dim tNMH As NMHDR
  566. Dim tOFNs As OFNOTIFYshort
  567. Dim tOF As OPENFILENAME
  568.  
  569.     If Not (m_oEventSink Is Nothing) Then
  570.         
  571.         Select Case msg
  572.         Case WM_INITDIALOG
  573.             DialogHook = m_oEventSink.InitDialog(hDlg)
  574.             
  575.         Case WM_NOTIFY
  576.             CopyMemory tNMH, ByVal lParam, Len(tNMH)
  577.             Select Case tNMH.code
  578.             Case CDN_SELCHANGE
  579.                 ' Changed selected file:
  580.                 DialogHook = m_oEventSink.FileChange(hDlg)
  581.             Case CDN_FOLDERCHANGE
  582.                 ' Changed folder:
  583.                 DialogHook = m_oEventSink.FolderChange(hDlg)
  584.             Case CDN_FILEOK
  585.                 ' Clicked OK:
  586.                 If Not m_oEventSink.ConfirmOK() Then
  587.                     SetWindowLong hDlg, DWL_MSGRESULT, 1
  588.                     DialogHook = 1
  589.                 Else
  590.                     SetWindowLong hDlg, DWL_MSGRESULT, 0
  591.                 End If
  592.             Case CDN_HELP
  593.                 ' Help clicked
  594.             Case CDN_TYPECHANGE
  595.                 DialogHook = m_oEventSink.TypeChange(hDlg)
  596.             Case CDN_INCLUDEITEM
  597.                 ' Hmmm
  598.             End Select
  599.         
  600.         Case WM_COMMAND
  601.             m_oEventSink.WMCommand hDlg, wParam, lParam
  602.             
  603.         Case WM_DESTROY
  604.             Debug.Print "WM_DESTROY"
  605.             m_oEventSink.DialogClose
  606.  
  607.             
  608.         End Select
  609.     End If
  610. End Function
  611.  
  612.  
  613. Public Property Get APIReturn() As Long
  614.     'return object's APIReturn property
  615.     APIReturn = m_lApiReturn
  616. End Property
  617. Public Property Get ExtendedError() As Long
  618.     'return object's ExtendedError property
  619.     ExtendedError = m_lExtendedError
  620. End Property
  621.  
  622. Private Sub Class_Initialize()
  623. #If fComponent Then
  624.     InitColors
  625. #End If
  626. End Sub
  627.  
  628. Function VBGetOpenFileName2(Filename As String, _
  629.                            Optional FileTitle As String, _
  630.                            Optional FileMustExist As Boolean = True, _
  631.                            Optional MultiSelect As Boolean = False, _
  632.                            Optional ReadOnly As Boolean = False, _
  633.                            Optional HideReadOnly As Boolean = False, _
  634.                            Optional Filter As String = "All (*.*)| *.*", _
  635.                            Optional FilterIndex As Long = 1, _
  636.                            Optional InitDir As String, _
  637.                            Optional DlgTitle As String, _
  638.                            Optional DefaultExt As String, _
  639.                            Optional Owner As Long = -1, _
  640.                            Optional flags As Long = 0, _
  641.                            Optional Hook As Boolean = False, _
  642.                            Optional hInstance As Long = 0, _
  643.                            Optional TemplateName As Long = 0, _
  644.                            Optional EventSink As cCommonDialog _
  645.                         ) As Boolean
  646. Dim opfile As OPENFILENAME, s As String, afFlags As Long
  647.     
  648.    m_lApiReturn = 0
  649.    m_lExtendedError = 0
  650.  
  651.    With opfile
  652.       .lStructSize = Len(opfile)
  653.       
  654.       ' Add in specific flags and strip out non-VB flags
  655.       
  656.       .flags = (-FileMustExist * OFN_FILEMUSTEXIST) Or _
  657.              (-MultiSelect * OFN_ALLOWMULTISELECT) Or _
  658.               (-ReadOnly * OFN_READONLY) Or _
  659.               (-HideReadOnly * OFN_HIDEREADONLY)
  660.       .flags = .flags And Not OFN_ENABLEHOOK
  661.       
  662.       ' Owner can take handle of owning window
  663.       If Owner <> -1 Then .hWndOwner = Owner
  664.       ' InitDir can take initial directory string
  665.       .lpstrInitialDir = InitDir
  666.       ' DefaultExt can take default extension
  667.       .lpstrDefExt = DefaultExt
  668.       ' DlgTitle can take dialog box title
  669.       .lpstrTitle = DlgTitle
  670.       
  671.       If (.flags And OFN_ALLOWMULTISELECT) = OFN_ALLOWMULTISELECT Then
  672.          .flags = .flags Or OFN_EXPLORER
  673.       End If
  674.       
  675.       If (Hook) Then
  676.          HookedDialog = Me
  677.          .lpfnHook = lHookAddress(AddressOf DialogHookFunction)
  678.          .flags = .flags Or OFN_ENABLEHOOK Or OFN_EXPLORER
  679.          Set m_oEventSink = EventSink
  680.       End If
  681.       
  682.       If flags And OFN_ENABLETEMPLATE Then
  683.          If hInstance > 0 Then
  684.             .flags = .flags Or OFN_ENABLETEMPLATE
  685.             .hInstance = hInstance
  686.             .lpTemplateName = TemplateName
  687.          End If
  688.       End If
  689.     
  690.    ' To make Windows-style filter, replace | and : with nulls
  691.    Dim ch As String, i As Integer
  692.    For i = 1 To Len(Filter)
  693.       ch = Mid$(Filter, i, 1)
  694.       If ch = "|" Or ch = ":" Then
  695.           s = s & vbNullChar
  696.       Else
  697.           s = s & ch
  698.       End If
  699.    Next
  700.    
  701.    ' Put double null at end
  702.    s = s & vbNullChar & vbNullChar
  703.    .lpstrFilter = s
  704.    .nFilterIndex = FilterIndex
  705.    
  706.    ' Pad file and file title buffers to maximum path
  707.    If (.flags And OFN_ALLOWMULTISELECT) = OFN_ALLOWMULTISELECT Then
  708.       s = Filename & String$(8192 - Len(Filename), 0)
  709.       .lpstrFile = s
  710.       .nMaxFile = 8192
  711.       s = FileTitle & String$(8192 - Len(FileTitle), 0)
  712.       .lpstrFileTitle = s
  713.       .nMaxFileTitle = 8192
  714.    Else
  715.        s = Filename & String$(MAX_PATH - Len(Filename), 0)
  716.       .lpstrFile = s
  717.       .nMaxFile = MAX_PATH
  718.       s = FileTitle & String$(MAX_FILE - Len(FileTitle), 0)
  719.       .lpstrFileTitle = s
  720.       .nMaxFileTitle = MAX_FILE
  721.    End If
  722.    ' All other fields set to zero
  723.        
  724.    m_lApiReturn = GetOpenFileName(opfile)
  725.    
  726.    Set m_oEventSink = Nothing
  727.    ClearHookedDialog
  728.    Select Case m_lApiReturn
  729.    Case 1
  730.       ' Success
  731.       VBGetOpenFileName2 = True
  732.    
  733.       If (.flags And OFN_ALLOWMULTISELECT) = OFN_ALLOWMULTISELECT Then
  734.          Filename = .lpstrFile
  735.       Else
  736.          Filename = StrZToStr(.lpstrFile)
  737.          FileTitle = StrZToStr(.lpstrFileTitle)
  738.       End If
  739.       flags = .flags
  740.       ' Return the filter index
  741.       FilterIndex = .nFilterIndex
  742.       ' Look up the filter the user selected and return that
  743.       Filter = FilterLookup(.lpstrFilter, FilterIndex)
  744.       If (.flags And OFN_READONLY) Then ReadOnly = True
  745.       
  746.    Case 0
  747.       ' Cancelled
  748.       VBGetOpenFileName2 = False
  749.       Filename = ""
  750.       FileTitle = ""
  751.       flags = 0
  752.       FilterIndex = -1
  753.       Filter = ""
  754.       
  755.    Case Else
  756.       ' Extended error
  757.       m_lExtendedError = CommDlgExtendedError()
  758.       VBGetOpenFileName2 = False
  759.       Filename = ""
  760.       FileTitle = ""
  761.       flags = 0
  762.       FilterIndex = -1
  763.       Filter = ""
  764.       
  765.    End Select
  766.    
  767.    Set m_oEventSink = Nothing
  768. End With
  769.  
  770. End Function
  771.  
  772. Function VBGetOpenFileName(Filename As String, _
  773.                            Optional FileTitle As String, _
  774.                            Optional FileMustExist As Boolean = True, _
  775.                            Optional MultiSelect As Boolean = False, _
  776.                            Optional ReadOnly As Boolean = False, _
  777.                            Optional HideReadOnly As Boolean = False, _
  778.                            Optional Filter As String = "All (*.*)| *.*", _
  779.                            Optional FilterIndex As Long = 1, _
  780.                            Optional InitDir As String, _
  781.                            Optional DlgTitle As String, _
  782.                            Optional DefaultExt As String, _
  783.                            Optional Owner As Long = -1, _
  784.                            Optional flags As Long = 0, _
  785.                            Optional Hook As Boolean = False, _
  786.                            Optional EventSink As cCommonDialog _
  787.                         ) As Boolean
  788.    flags = flags And Not OFN_ENABLETEMPLATE
  789.    VBGetOpenFileName = VBGetOpenFileName2( _
  790.             Filename, FileTitle, FileMustExist, MultiSelect, _
  791.             ReadOnly, HideReadOnly, Filter, FilterIndex, InitDir, DlgTitle, _
  792.             DefaultExt, Owner, flags, Hook, , , EventSink)
  793. End Function
  794. Private Function lHookAddress(lPtr As Long) As Long
  795.     'Debug.Print lPtr
  796.     lHookAddress = lPtr
  797. End Function
  798. Private Function StrZToStr(s As String) As String
  799.     StrZToStr = Left$(s, lstrlen(s))
  800. End Function
  801.  
  802. Public Function VBGetSaveFileName2(Filename As String, _
  803.                            Optional FileTitle As String, _
  804.                            Optional OverWritePrompt As Boolean = True, _
  805.                            Optional Filter As String = "All (*.*)| *.*", _
  806.                            Optional FilterIndex As Long = 1, _
  807.                            Optional InitDir As String, _
  808.                            Optional DlgTitle As String, _
  809.                            Optional DefaultExt As String, _
  810.                            Optional Owner As Long = -1, _
  811.                            Optional flags As Long, _
  812.                            Optional Hook As Boolean = False, _
  813.                            Optional hInstance As Long = 0, _
  814.                            Optional TemplateName As Long = 0, _
  815.                            Optional EventSink As cCommonDialog _
  816.                         ) As Boolean
  817. Dim opfile As OPENFILENAME, s As String
  818.  
  819. m_lApiReturn = 0
  820. m_lExtendedError = 0
  821.  
  822. With opfile
  823.    .lStructSize = Len(opfile)
  824.    
  825.    ' Add in specific flags and strip out non-VB flags
  826.    .flags = (-OverWritePrompt * OFN_OVERWRITEPROMPT) Or _
  827.             OFN_HIDEREADONLY
  828.    .flags = .flags And Not OFN_ENABLEHOOK
  829.    
  830.    ' Owner can take handle of owning window
  831.    If Owner <> -1 Then .hWndOwner = Owner
  832.    ' InitDir can take initial directory string
  833.    .lpstrInitialDir = InitDir
  834.    ' DefaultExt can take default extension
  835.    .lpstrDefExt = DefaultExt
  836.    ' DlgTitle can take dialog box title
  837.    .lpstrTitle = DlgTitle
  838.    
  839.    If (Hook) Then
  840.       HookedDialog = Me
  841.       .lpfnHook = lHookAddress(AddressOf DialogHookFunction)
  842.       .flags = .flags Or OFN_ENABLEHOOK Or OFN_EXPLORER
  843.       Set m_oEventSink = EventSink
  844.    End If
  845.       
  846.    If flags And OFN_ENABLETEMPLATE Then
  847.       If hInstance > 0 Then
  848.          .flags = .flags Or OFN_ENABLETEMPLATE
  849.          .hInstance = hInstance
  850.          .lpTemplateName = TemplateName
  851.       End If
  852.    End If
  853.    
  854.    ' Make new filter with bars (|) replacing nulls and double null at end
  855.    Dim ch As String, i As Integer
  856.    For i = 1 To Len(Filter)
  857.       ch = Mid$(Filter, i, 1)
  858.       If ch = "|" Or ch = ":" Then
  859.          s = s & vbNullChar
  860.       Else
  861.          s = s & ch
  862.       End If
  863.    Next
  864.    ' Put double null at end
  865.    s = s & vbNullChar & vbNullChar
  866.    .lpstrFilter = s
  867.    .nFilterIndex = FilterIndex
  868.  
  869.    ' Pad file and file title buffers to maximum path
  870.    s = Filename & String$(MAX_PATH - Len(Filename), 0)
  871.    .lpstrFile = s
  872.    .nMaxFile = MAX_PATH
  873.    s = FileTitle & String$(MAX_FILE - Len(FileTitle), 0)
  874.    .lpstrFileTitle = s
  875.    .nMaxFileTitle = MAX_FILE
  876.    ' All other fields zero
  877.    
  878.    m_lApiReturn = GetSaveFileName(opfile)
  879.    
  880.    Set m_oEventSink = Nothing
  881.    ClearHookedDialog
  882.    
  883.    Select Case m_lApiReturn
  884.    Case 1
  885.       VBGetSaveFileName2 = True
  886.       Filename = StrZToStr(.lpstrFile)
  887.       FileTitle = StrZToStr(.lpstrFileTitle)
  888.       flags = .flags
  889.       ' Return the filter index
  890.       FilterIndex = .nFilterIndex
  891.       ' Look up the filter the user selected and return that
  892.       Filter = FilterLookup(.lpstrFilter, FilterIndex)
  893.       
  894.    Case 0
  895.       ' Cancelled:
  896.       VBGetSaveFileName2 = False
  897.       Filename = ""
  898.       FileTitle = ""
  899.       flags = 0
  900.       FilterIndex = 0
  901.       Filter = ""
  902.       
  903.    Case Else
  904.       ' Extended error:
  905.       VBGetSaveFileName2 = False
  906.       m_lExtendedError = CommDlgExtendedError()
  907.       Filename = ""
  908.       FileTitle = ""
  909.       flags = 0
  910.       FilterIndex = 0
  911.       Filter = ""
  912.       
  913.    End Select
  914. End With
  915.  
  916. End Function
  917.  
  918. Function VBGetSaveFileName(Filename As String, _
  919.                            Optional FileTitle As String, _
  920.                            Optional OverWritePrompt As Boolean = True, _
  921.                            Optional Filter As String = "All (*.*)| *.*", _
  922.                            Optional FilterIndex As Long = 1, _
  923.                            Optional InitDir As String, _
  924.                            Optional DlgTitle As String, _
  925.                            Optional DefaultExt As String, _
  926.                            Optional Owner As Long = -1, _
  927.                            Optional flags As Long, _
  928.                            Optional Hook As Boolean = False, _
  929.                            Optional EventSink As cCommonDialog _
  930.                         ) As Boolean
  931.    flags = flags And Not OFN_ENABLETEMPLATE
  932.    VBGetSaveFileName = VBGetSaveFileName2(Filename, FileTitle, OverWritePrompt, _
  933.             Filter, FilterIndex, InitDir, DlgTitle, DefaultExt, _
  934.             Owner, flags, Hook, , , EventSink)
  935. End Function
  936.  
  937. Private Function FilterLookup(ByVal sFilters As String, ByVal iCur As Long) As String
  938.     Dim iStart As Long, iEnd As Long, s As String
  939.     iStart = 1
  940.     If sFilters = "" Then Exit Function
  941.     Do
  942.         ' Cut out both parts marked by null character
  943.         iEnd = InStr(iStart, sFilters, vbNullChar)
  944.         If iEnd = 0 Then Exit Function
  945.         iEnd = InStr(iEnd + 1, sFilters, vbNullChar)
  946.         If iEnd Then
  947.             s = Mid$(sFilters, iStart, iEnd - iStart)
  948.         Else
  949.             s = Mid$(sFilters, iStart)
  950.         End If
  951.         iStart = iEnd + 1
  952.         If iCur = 1 Then
  953.             FilterLookup = s
  954.             Exit Function
  955.         End If
  956.         iCur = iCur - 1
  957.     Loop While iCur
  958. End Function
  959.  
  960. Function VBGetFileTitle(sFIle As String) As String
  961.     Dim sFileTitle As String, cFileTitle As Integer
  962.  
  963.     cFileTitle = MAX_PATH
  964.     sFileTitle = String$(MAX_PATH, 0)
  965.     cFileTitle = GetFileTitle(sFIle, sFileTitle, MAX_PATH)
  966.     If cFileTitle Then
  967.         VBGetFileTitle = ""
  968.     Else
  969.         VBGetFileTitle = Left$(sFileTitle, InStr(sFileTitle, vbNullChar) - 1)
  970.     End If
  971.  
  972. End Function
  973.  
  974. ' ChooseColor wrapper
  975. Function VBChooseColor(Color As Long, _
  976.                        Optional AnyColor As Boolean = True, _
  977.                        Optional FullOpen As Boolean = False, _
  978.                        Optional DisableFullOpen As Boolean = False, _
  979.                        Optional Owner As Long = -1, _
  980.                        Optional flags As Long, _
  981.                        Optional Hook As Boolean = False, _
  982.                       Optional EventSink As cCommonDialog _
  983.                     ) As Boolean
  984.  
  985.     Dim chclr As TCHOOSECOLOR
  986.     chclr.lStructSize = Len(chclr)
  987.     
  988.     ' Color must get reference variable to receive result
  989.     ' Flags can get reference variable or constant with bit flags
  990.     ' Owner can take handle of owning window
  991.     If Owner <> -1 Then chclr.hWndOwner = Owner
  992.  
  993.     ' Assign color (default uninitialized value of zero is good default)
  994.     chclr.rgbResult = Color
  995.  
  996.     ' Mask out unwanted bits
  997.     Dim afMask As Long
  998.     afMask = CLng(Not (CC_ENABLEHOOK Or _
  999.                        CC_ENABLETEMPLATE))
  1000.     ' Pass in flags
  1001.     chclr.flags = afMask And (CC_RGBInit Or _
  1002.                   IIf(AnyColor, CC_AnyColor, CC_SolidColor) Or _
  1003.                   (-FullOpen * CC_FullOpen) Or _
  1004.                   (-DisableFullOpen * CC_PreventFullOpen))
  1005.  
  1006.     If (Hook) Then
  1007.         HookedDialog = Me
  1008.         chclr.lpfnHook = lHookAddress(AddressOf CCHookProc)
  1009.         chclr.flags = chclr.flags Or CC_ENABLEHOOK
  1010.         Set m_oEventSink = EventSink
  1011.     End If
  1012.     
  1013.     ' If first time, initialize to white
  1014.     If fNotFirst = False Then InitColors
  1015.  
  1016.     chclr.lpCustColors = VarPtr(alCustom(0))
  1017.     ' All other fields zero
  1018.     
  1019.     m_lApiReturn = ChooseColor(chclr)
  1020.     Set m_oEventSink = Nothing
  1021.     ClearHookedDialog
  1022.     
  1023.     Select Case m_lApiReturn
  1024.     Case 1
  1025.         ' Success
  1026.         VBChooseColor = True
  1027.         Color = chclr.rgbResult
  1028.     Case 0
  1029.         ' Cancelled
  1030.         VBChooseColor = False
  1031.         Color = -1
  1032.     Case Else
  1033.         ' Extended error
  1034.         m_lExtendedError = CommDlgExtendedError()
  1035.         VBChooseColor = False
  1036.         Color = -1
  1037.     End Select
  1038.  
  1039. End Function
  1040.  
  1041. Friend Sub InitColors()
  1042.     Dim i As Integer
  1043.     ' Initialize with first 16 system interface colors
  1044.     For i = 0 To 15
  1045.         alCustom(i) = GetSysColor(i)
  1046.     Next
  1047.     fNotFirst = True
  1048. End Sub
  1049.  
  1050. ' Property to read or modify custom colors (use to save colors in registry)
  1051. Public Property Get CustomColor(i As Integer) As Long
  1052.     ' If first time, initialize to white
  1053.     If fNotFirst = False Then InitColors
  1054.     If i >= 0 And i <= 15 Then
  1055.         CustomColor = alCustom(i)
  1056.     Else
  1057.         CustomColor = -1
  1058.     End If
  1059. End Property
  1060.  
  1061. Public Property Let CustomColor(i As Integer, iValue As Long)
  1062.     ' If first time, initialize to system colors
  1063.     If fNotFirst = False Then InitColors
  1064.     If i >= 0 And i <= 15 Then
  1065.         alCustom(i) = iValue
  1066.     End If
  1067. End Property
  1068.  
  1069. ' ChooseFont wrapper
  1070. Function VBChooseFont(CurFont As Font, _
  1071.                       Optional PrinterDC As Long = -1, _
  1072.                       Optional Owner As Long = -1, _
  1073.                       Optional Color As Long = vbBlack, _
  1074.                       Optional MinSize As Long = 0, _
  1075.                       Optional MaxSize As Long = 0, _
  1076.                       Optional flags As Long = 0, _
  1077.                       Optional Hook As Boolean = False, _
  1078.                       Optional EventSink As cCommonDialog _
  1079.                     ) As Boolean
  1080.  
  1081.     m_lApiReturn = 0
  1082.     m_lExtendedError = 0
  1083.  
  1084.     ' Unwanted Flags bits
  1085.     Const CF_FontNotSupported = CF_Apply Or CF_EnableHook Or CF_EnableTemplate
  1086.     
  1087.     ' Flags can get reference variable or constant with bit flags
  1088.     ' PrinterDC can take printer DC
  1089.     If PrinterDC = -1 Then
  1090.         PrinterDC = 0
  1091.         If flags And CF_PrinterFonts Then PrinterDC = Printer.hdc
  1092.     Else
  1093.         flags = flags Or CF_PrinterFonts
  1094.     End If
  1095.     ' Must have some fonts
  1096.     If (flags And CF_PrinterFonts) = 0 Then flags = flags Or CF_ScreenFonts
  1097.     ' Color can take initial color, receive chosen color
  1098.     If Color <> vbBlack Then flags = flags Or CF_EFFECTS
  1099.     ' MinSize can be minimum size accepted
  1100.     If MinSize Then flags = flags Or CF_LimitSize
  1101.     ' MaxSize can be maximum size accepted
  1102.     If MaxSize Then flags = flags Or CF_LimitSize
  1103.  
  1104.     ' Put in required internal flags and remove unsupported
  1105.     flags = (flags Or CF_InitToLogFontStruct) And Not CF_FontNotSupported
  1106.     
  1107.     ' Initialize LOGFONT variable
  1108.     Dim fnt As LOGFONT
  1109.     Const PointsPerTwip = 1440 / 72
  1110.     fnt.lfHeight = -(CurFont.Size * (PointsPerTwip / Screen.TwipsPerPixelY))
  1111.     fnt.lfWeight = CurFont.Weight
  1112.     fnt.lfItalic = CurFont.Italic
  1113.     fnt.lfUnderline = CurFont.Underline
  1114.     fnt.lfStrikeOut = CurFont.Strikethrough
  1115.     ' Other fields zero
  1116.     StrToBytes fnt.lfFaceName, CurFont.Name
  1117.  
  1118.     ' Initialize TCHOOSEFONT variable
  1119.     Dim cf As TCHOOSEFONT
  1120.     cf.lStructSize = Len(cf)
  1121.     If Owner <> -1 Then cf.hWndOwner = Owner
  1122.     cf.hdc = PrinterDC
  1123.     cf.lpLogFont = VarPtr(fnt)
  1124.     cf.iPointSize = CurFont.Size * 10
  1125.     cf.flags = flags
  1126.     cf.rgbColors = Color
  1127.     cf.nSizeMin = MinSize
  1128.     cf.nSizeMax = MaxSize
  1129.     
  1130.     If (Hook) Then
  1131.         HookedDialog = Me
  1132.         cf.lpfnHook = lHookAddress(AddressOf CFHookProc)
  1133.         cf.flags = cf.flags Or CF_EnableHook
  1134.         Set m_oEventSink = EventSink
  1135.     End If
  1136.     
  1137.     ' All other fields zero
  1138.     m_lApiReturn = ChooseFont(cf)
  1139.     Set m_oEventSink = Nothing
  1140.     ClearHookedDialog
  1141.     Select Case m_lApiReturn
  1142.     Case 1
  1143.         ' Success
  1144.         VBChooseFont = True
  1145.         flags = cf.flags
  1146.         Color = cf.rgbColors
  1147.         CurFont.Bold = cf.nFontType And Bold_FontType
  1148.         'CurFont.Italic = cf.nFontType And Italic_FontType
  1149.         CurFont.Italic = fnt.lfItalic
  1150.         CurFont.Strikethrough = fnt.lfStrikeOut
  1151.         CurFont.Underline = fnt.lfUnderline
  1152.         CurFont.Weight = fnt.lfWeight
  1153.         CurFont.Size = cf.iPointSize / 10
  1154.         CurFont.Name = BytesToStr(fnt.lfFaceName)
  1155.     Case 0
  1156.         ' Cancelled
  1157.         VBChooseFont = False
  1158.     Case Else
  1159.         ' Extended error
  1160.         m_lExtendedError = CommDlgExtendedError()
  1161.         VBChooseFont = False
  1162.     End Select
  1163.         
  1164. End Function
  1165.  
  1166. ' PrintDlg wrapper
  1167. Function VBPrintDlg(hdc As Long, _
  1168.                     Optional PrintRange As EPrintRange = eprAll, _
  1169.                     Optional DisablePageNumbers As Boolean, _
  1170.                     Optional FromPage As Long = 1, _
  1171.                     Optional ToPage As Long = &HFFFF, _
  1172.                     Optional DisableSelection As Boolean, _
  1173.                     Optional Copies As Integer, _
  1174.                     Optional ShowPrintToFile As Boolean, _
  1175.                     Optional DisablePrintToFile As Boolean = True, _
  1176.                     Optional PrintToFile As Boolean, _
  1177.                     Optional Collate As Boolean, _
  1178.                     Optional PreventWarning As Boolean, _
  1179.                     Optional Owner As Long, _
  1180.                     Optional Printer As Object, _
  1181.                     Optional flags As Long, _
  1182.                     Optional Hook As Boolean = False, _
  1183.                     Optional EventSink As cCommonDialog _
  1184.                 ) As Boolean
  1185.     Dim afFlags As Long
  1186.     
  1187.     m_lApiReturn = 0
  1188.     m_lExtendedError = 0
  1189.     
  1190.     ' Set PRINTDLG flags
  1191.     afFlags = flags
  1192.     afFlags = afFlags Or (Abs(DisablePageNumbers) * PD_NOPAGENUMS) Or _
  1193.               (Abs(DisablePrintToFile) * PD_DISABLEPRINTTOFILE) Or _
  1194.               (Abs(DisableSelection) * PD_NOSELECTION) Or _
  1195.               (Abs(PrintToFile) * PD_PRINTTOFILE) Or _
  1196.               (Abs(Not ShowPrintToFile) * PD_HIDEPRINTTOFILE) Or _
  1197.               (Abs(PreventWarning) * PD_NOWARNING) Or _
  1198.               (Abs(Collate) * PD_COLLATE) Or _
  1199.               PD_USEDEVMODECOPIESANDCOLLATE Or _
  1200.               PD_RETURNDC
  1201.     If PrintRange = eprPageNumbers Then
  1202.         afFlags = afFlags Or PD_PAGENUMS
  1203.     ElseIf PrintRange = eprSelection Then
  1204.         afFlags = afFlags Or PD_SELECTION
  1205.     End If
  1206.     ' Mask out unwanted bits
  1207.     afFlags = afFlags And Not PD_ENABLEPRINTHOOK
  1208.     afFlags = afFlags And Not PD_ENABLEPRINTTEMPLATE
  1209.     afFlags = afFlags And Not PD_ENABLESETUPHOOK
  1210.     afFlags = afFlags And Not PD_ENABLESETUPTEMPLATE
  1211.         
  1212.     ' Fill in PRINTDLG structure
  1213.     Dim pd As TPRINTDLG
  1214.     pd.lStructSize = Len(pd)
  1215.     pd.hWndOwner = Owner
  1216.     pd.flags = afFlags
  1217.     pd.nFromPage = FromPage
  1218.     pd.nToPage = ToPage
  1219.     pd.nMinPage = 1
  1220.     pd.nMaxPage = &HFFFF
  1221.     If (Hook) Then
  1222.         HookedDialog = Me
  1223.         Set m_oEventSink = EventSink
  1224.         If (pd.flags And PD_PRINTSETUP) = PD_PRINTSETUP Then
  1225.             pd.flags = pd.flags Or PD_ENABLESETUPHOOK
  1226.             pd.lpfnSetupHook = lHookAddress(AddressOf PrintSetupHookProc)
  1227.         Else
  1228.             pd.flags = pd.flags Or PD_ENABLEPRINTHOOK
  1229.             pd.lpfnPrintHook = lHookAddress(AddressOf PrintHookProc)
  1230.         End If
  1231.     End If
  1232.     
  1233.     ' Show Print dialog
  1234.     m_lApiReturn = PrintDlg(pd)
  1235.     ClearHookedDialog
  1236.     Set m_oEventSink = Nothing
  1237.     Select Case m_lApiReturn
  1238.     Case 1
  1239.         VBPrintDlg = True
  1240.         ' Return dialog values in parameters
  1241.         hdc = pd.hdc
  1242.         If (pd.flags And PD_PAGENUMS) Then
  1243.             PrintRange = eprPageNumbers
  1244.         ElseIf (pd.flags And PD_SELECTION) Then
  1245.             PrintRange = eprSelection
  1246.         Else
  1247.             PrintRange = eprAll
  1248.         End If
  1249.         FromPage = pd.nFromPage
  1250.         ToPage = pd.nToPage
  1251.         PrintToFile = (pd.flags And PD_PRINTTOFILE)
  1252.         ' Get DEVMODE structure from PRINTDLG
  1253.         
  1254.         Dim pDevMode As Long
  1255.         pDevMode = GlobalLock(pd.hDevMode)
  1256.         CopyMemory m_dvmode, ByVal pDevMode, Len(m_dvmode)
  1257.         GlobalUnlock pd.hDevMode
  1258.         If (pd.flags And PD_COLLATE) = PD_COLLATE Then
  1259.             ' User selected collate option but printer driver
  1260.             ' does not support collation.
  1261.             ' Collation option must be set from the
  1262.             ' PRINTDLG structure:
  1263.             Collate = True
  1264.             Copies = pd.nCopies
  1265.         Else
  1266.             ' Print driver supports collation or collation
  1267.             ' not switched on.
  1268.             ' DEVMODE structure contains Collation and copy
  1269.             ' information
  1270.             ' Get Copies and Collate settings from DEVMODE structure
  1271.             Collate = (m_dvmode.dmCollate = DMCOLLATE_TRUE)
  1272.             Copies = m_dvmode.dmCopies
  1273.         End If
  1274.         
  1275.         ' Set default printer properties
  1276.         On Error Resume Next
  1277.         If Not (Printer Is Nothing) Then
  1278.             Printer.Copies = Copies
  1279.             Printer.Orientation = m_dvmode.dmOrientation
  1280.             Printer.PaperSize = m_dvmode.dmPaperSize
  1281.             Printer.PrintQuality = m_dvmode.dmPrintQuality
  1282.         End If
  1283.         On Error GoTo 0
  1284.     Case 0
  1285.         ' Cancelled
  1286.         VBPrintDlg = False
  1287.     Case Else
  1288.         ' Extended error:
  1289.         m_lExtendedError = CommDlgExtendedError()
  1290.         VBPrintDlg = False
  1291.     End Select
  1292.     
  1293. End Function
  1294. Friend Property Get DevMode() As DevMode
  1295.     DevMode = m_dvmode
  1296. End Property
  1297. Public Function VBPageSetupDlg2( _
  1298.         Optional Owner As Long, _
  1299.         Optional DisableMargins As Boolean, _
  1300.         Optional DisableOrientation As Boolean, _
  1301.         Optional DisablePaper As Boolean, _
  1302.         Optional DisablePrinter As Boolean, _
  1303.         Optional LeftMargin As Single, _
  1304.         Optional MinLeftMargin As Single, _
  1305.         Optional RightMargin As Single, _
  1306.         Optional MinRightMargin As Single, _
  1307.         Optional TopMargin As Single, _
  1308.         Optional MinTopMargin As Single, _
  1309.         Optional BottomMargin As Single, _
  1310.         Optional MinBottomMargin As Single, _
  1311.         Optional PaperSize As EPaperSize = epsLetter, _
  1312.         Optional Orientation As EOrientation = eoPortrait, _
  1313.         Optional PrintQuality As EPrintQuality = epqDraft, _
  1314.         Optional Units As EPageSetupUnits = epsuInches, _
  1315.         Optional Printer As Object, _
  1316.         Optional flags As Long, _
  1317.         Optional Hook As Boolean = False, _
  1318.         Optional EventSink As cCommonDialog _
  1319.     ) As Boolean
  1320. Dim afFlags As Long, afMask As Long
  1321.         
  1322.     m_lApiReturn = 0
  1323.     m_lExtendedError = 0
  1324.     ' Mask out unwanted bits
  1325.     afMask = Not (PSD_EnablePagePaintHook Or _
  1326.                   PSD_EnablePageSetupHook Or _
  1327.                   PSD_EnablePageSetupTemplate)
  1328.     ' Set TPAGESETUPDLG flags
  1329.     afFlags = (-DisableMargins * PSD_DISABLEMARGINS) Or _
  1330.               (-DisableOrientation * PSD_DISABLEORIENTATION) Or _
  1331.               (-DisablePaper * PSD_DISABLEPAPER) Or _
  1332.               (-DisablePrinter * PSD_DISABLEPRINTER) _
  1333.                And afMask
  1334.     If (flags And PSD_Defaultminmargins) = PSD_Defaultminmargins Then
  1335.         afFlags = afFlags Or PSD_Defaultminmargins
  1336.     Else
  1337.         afFlags = afFlags Or PSD_MARGINS
  1338.     End If
  1339.     Dim lUnits As Long
  1340.     If Units = epsuInches Then
  1341.         afFlags = afFlags Or PSD_INTHOUSANDTHSOFINCHES
  1342.         lUnits = 1000
  1343.     Else
  1344.         afFlags = afFlags Or PSD_INHUNDREDTHSOFMILLIMETERS
  1345.         lUnits = 100
  1346.     End If
  1347.     
  1348.     Dim psd As TPAGESETUPDLG
  1349.     ' Fill in PRINTDLG structure
  1350.     psd.lStructSize = Len(psd)
  1351.     psd.hWndOwner = Owner
  1352.     psd.rtMargin.Top = TopMargin * lUnits
  1353.     psd.rtMargin.Left = LeftMargin * lUnits
  1354.     psd.rtMargin.Bottom = BottomMargin * lUnits
  1355.     psd.rtMargin.Right = RightMargin * lUnits
  1356.     psd.rtMinMargin.Top = MinTopMargin * lUnits
  1357.     psd.rtMinMargin.Left = MinLeftMargin * lUnits
  1358.     psd.rtMinMargin.Bottom = MinBottomMargin * lUnits
  1359.     psd.rtMinMargin.Right = MinRightMargin * lUnits
  1360.     psd.flags = afFlags
  1361.     If (Hook) Then
  1362.         HookedDialog = Me
  1363.         Set m_oEventSink = EventSink
  1364.         psd.lpfnPageSetupHook = lHookAddress(AddressOf PageSetupHook)
  1365.         psd.flags = psd.flags Or PSD_EnablePageSetupHook
  1366.     End If
  1367.     
  1368.     ' Show Print dialog
  1369.     If PageSetupDlg(psd) Then
  1370.         VBPageSetupDlg2 = True
  1371.         ' Return dialog values in parameters
  1372.         TopMargin = psd.rtMargin.Top / lUnits
  1373.         LeftMargin = psd.rtMargin.Left / lUnits
  1374.         BottomMargin = psd.rtMargin.Bottom / lUnits
  1375.         RightMargin = psd.rtMargin.Right / lUnits
  1376.         MinTopMargin = psd.rtMinMargin.Top / lUnits
  1377.         MinLeftMargin = psd.rtMinMargin.Left / lUnits
  1378.         MinBottomMargin = psd.rtMinMargin.Bottom / lUnits
  1379.         MinRightMargin = psd.rtMinMargin.Right / lUnits
  1380.         
  1381.         ' Get DEVMODE structure from PRINTDLG
  1382.         Dim dvmode As DevMode, pDevMode As Long
  1383.         pDevMode = GlobalLock(psd.hDevMode)
  1384.         CopyMemory dvmode, ByVal pDevMode, Len(dvmode)
  1385.         GlobalUnlock psd.hDevMode
  1386.         PaperSize = dvmode.dmPaperSize
  1387.         Orientation = dvmode.dmOrientation
  1388.         PrintQuality = dvmode.dmPrintQuality
  1389.         ' Set default printer properties
  1390.         On Error Resume Next
  1391.         If Not (Printer Is Nothing) Then
  1392.             Printer.Copies = dvmode.dmCopies
  1393.             Printer.Orientation = dvmode.dmOrientation
  1394.             Printer.PaperSize = dvmode.dmPaperSize
  1395.             Printer.PrintQuality = dvmode.dmPrintQuality
  1396.         End If
  1397.         On Error GoTo 0
  1398.     End If
  1399.     Set m_oEventSink = Nothing
  1400.     ClearHookedDialog
  1401.     
  1402. End Function
  1403.  
  1404. ' PageSetupDlg wrapper
  1405. Function VBPageSetupDlg(Optional Owner As Long, _
  1406.                         Optional DisableMargins As Boolean, _
  1407.                         Optional DisableOrientation As Boolean, _
  1408.                         Optional DisablePaper As Boolean, _
  1409.                         Optional DisablePrinter As Boolean, _
  1410.                         Optional LeftMargin As Long, _
  1411.                         Optional MinLeftMargin As Long, _
  1412.                         Optional RightMargin As Long, _
  1413.                         Optional MinRightMargin As Long, _
  1414.                         Optional TopMargin As Long, _
  1415.                         Optional MinTopMargin As Long, _
  1416.                         Optional BottomMargin As Long, _
  1417.                         Optional MinBottomMargin As Long, _
  1418.                         Optional PaperSize As EPaperSize = epsLetter, _
  1419.                         Optional Orientation As EOrientation = eoPortrait, _
  1420.                         Optional PrintQuality As EPrintQuality = epqDraft, _
  1421.                         Optional Units As EPageSetupUnits = epsuInches, _
  1422.                         Optional Printer As Object, _
  1423.                         Optional flags As Long, _
  1424.                         Optional Hook As Boolean = False, _
  1425.                         Optional EventSink As cCommonDialog _
  1426.                     ) As Boolean
  1427. Dim fLeftMargin As Single
  1428. Dim fMinLeftMargin As Single
  1429. Dim fRightMargin As Single
  1430. Dim fMinRightMargin As Single
  1431. Dim fTopMargin As Single
  1432. Dim fMinTopMargin As Single
  1433. Dim fBottomMargin As Single
  1434. Dim fMinBottomMargin As Single
  1435.  
  1436.     VBPageSetupDlg2 _
  1437.         Owner, _
  1438.         DisableMargins, _
  1439.         DisableOrientation, _
  1440.         DisablePaper, _
  1441.         DisablePrinter, _
  1442.         fLeftMargin, _
  1443.         fMinLeftMargin, _
  1444.         fRightMargin, _
  1445.         fMinRightMargin, _
  1446.         fTopMargin, _
  1447.         fMinTopMargin, _
  1448.         fBottomMargin, _
  1449.         fMinBottomMargin, _
  1450.         PaperSize, _
  1451.         Orientation, _
  1452.         PrintQuality, _
  1453.         Units, _
  1454.         Printer, _
  1455.         flags, _
  1456.         Hook, _
  1457.         EventSink
  1458.     LeftMargin = fLeftMargin
  1459.     MinLeftMargin = fMinLeftMargin
  1460.     RightMargin = fRightMargin
  1461.     MinRightMargin = fMinRightMargin
  1462.     TopMargin = fTopMargin
  1463.     MinTopMargin = fMinTopMargin
  1464.     BottomMargin = fBottomMargin
  1465.     MinBottomMargin = fMinBottomMargin
  1466. End Function
  1467.  
  1468. #If fComponent = 0 Then
  1469. Private Sub ErrRaise(e As Long)
  1470.     Dim sText As String, sSource As String
  1471.     If e > 1000 Then
  1472.         sSource = App.EXEName & ".CommonDialog"
  1473.         Err.Raise COMError(e), sSource, sText
  1474.     Else
  1475.         ' Raise standard Visual Basic error
  1476.         sSource = App.EXEName & ".VBError"
  1477.         Err.Raise e, sSource
  1478.     End If
  1479. End Sub
  1480. #End If
  1481.  
  1482.  
  1483. Private Sub StrToBytes(ab() As Byte, s As String)
  1484.     If IsArrayEmpty(ab) Then
  1485.         ' Assign toiAssmMargiublePaper * PSD_DIyEmpt    Or _
  1486.         fMinRagePaintHook O
  1487.     ile  End Ifurce =Hook OPaintHook O
  1488.    String)
  1489.   argin
  1490. End 
  1491.         DrtMinMargin.RightMinRightMargality =9,n
  1492. k O
  1493.     ile  End Ifurce =Hook OPaintHring)
  1494.   arargin.BottopDev_
  1495.     ' Success
  1496.  tional EventSink As cCommon        Orientreceive resulttrcCommon  rfrgin
  1497.     MinTopMargin Dev_s=e)
  1498.      mmon  P|   ' Shdctional Orientaceive reev_stionVcmon  P|  * = 0
  1499. m_llags Or PSD_EnablePageSWEB       Optional flags As Long = 0, _
  1500.            ott           ott           ott       Er0ru       nterDC
  1501.        = fMi     CopyMemoro               t       Egin rgin = fRightMargin         bokAddress = lPtr   Printer.CopkAddal HideRe      Printer.OlUnits
  1502.      s =e = Len(cf)
  1503.     If Owner <> -1 Then cf.hWndOwner = Owner
  1504.     cfVhopDev_
  1505.     ' Success
  1506.  tiOwn,e A,Option
  1507.     n, _
  1508.   s_
  1509.   s_
  1510.  s_
  1511.  v_
  1512.   ientation its
  1513.    rientation As Ey