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