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