home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / FormShaper3204310302001.psc / cCommonDialog / cCommonDialog.cls
Encoding:
Visual Basic class definition  |  2001-10-29  |  39.3 KB  |  1,188 lines

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