home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / swbback / commondl.bas next >
Encoding:
BASIC Source File  |  1999-02-17  |  10.7 KB  |  322 lines

  1. Attribute VB_Name = "CommonDlg"
  2. Option Explicit
  3.  
  4. '======================================================
  5. '  Common Dialog functions used to remove dependency
  6. '  on (large) Common Dialog control.
  7. '======================================================
  8.  
  9. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  10. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  11. Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" (ByVal hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData As Long) As Long
  12.  
  13. Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
  14. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  15. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
  16. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  17. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  18. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  19.  
  20. Public Const HELP_COMMAND = &H102&
  21. Public Const HELP_CONTENTS = &H3&
  22. Public Const HELP_CONTEXT = &H1
  23. Public Const HELP_CONTEXTPOPUP = &H8&
  24. Public Const HELP_FORCEFILE = &H9&
  25. Public Const HELP_HELPONHELP = &H4
  26. Public Const HELP_INDEX = &H3
  27. Public Const HELP_KEY = &H101
  28. Public Const HELP_MULTIKEY = &H201&
  29. Public Const HELP_PARTIALKEY = &H105&
  30. Public Const HELP_QUIT = &H2
  31. Public Const HELP_SETCONTENTS = &H5&
  32. Public Const HELP_SETINDEX = &H5
  33. Public Const HELP_SETWINPOS = &H203&
  34. Public Const HELP_FINDER = &HB
  35.  
  36. Public Const OFN_FILEMUSTEXIST = &H1000
  37. Public Const OFN_HIDEREADONLY = &H4
  38. Public Const OFN_NOCHANGEDIR = &H8
  39. Public Const OFN_NOVALIDATE = &H100
  40. Public Const OFN_OVERWRITEPROMPT = &H2
  41. Public Const OFN_PATHMUSTEXIST = &H800
  42. Public Const OFN_READONLY = &H1
  43. Public Const OFN_NOREADONLYRETURN = &H8000
  44. Public Const OFN_SHARENOWARN = 1
  45. Public Const OFN_SHAREWARN = 0
  46. Public Const OFN_SHOWHELP = &H10
  47. Public Const OFS_MAXPATHNAME = 128
  48.  
  49. Public Const CCHDEVICENAME = 32
  50. Public Const CCHFORMNAME = 32
  51. Public Const GMEM_FIXED = &H0
  52. Public Const GMEM_MOVEABLE = &H2
  53. Public Const GMEM_ZEROINIT = &H40
  54. Public Const DM_DUPLEX = &H1000&
  55. Public Const DM_ORIENTATION = &H1&
  56. Public Const PD_ALLPAGES = &H0
  57. Public Const PD_COLLATE = &H10
  58. Public Const PD_DISABLEPRINTTOFILE = &H80000
  59. Public Const PD_ENABLEPRINTHOOK = &H1000
  60. Public Const PD_ENABLEPRINTTEMPLATE = &H4000
  61. Public Const PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
  62. Public Const PD_ENABLESETUPHOOK = &H2000
  63. Public Const PD_ENABLESETUPTEMPLATE = &H8000
  64. Public Const PD_ENABLESETUPTEMPLATEHANDLE = &H20000
  65. Public Const PD_HIDEPRINTTOFILE = &H100000
  66. Public Const PD_NONETWORKBUTTON = &H200000
  67. Public Const PD_NOPAGENUMS = &H8
  68. Public Const PD_NOSELECTION = &H4
  69. Public Const PD_NOWARNING = &H80
  70. Public Const PD_PAGENUMS = &H2
  71. Public Const PD_PRINTSETUP = &H40
  72. Public Const PD_PRINTTOFILE = &H20
  73. Public Const PD_RETURNDC = &H100
  74. Public Const PD_RETURNDEFAULT = &H400
  75. Public Const PD_RETURNIC = &H200
  76. Public Const PD_SELECTION = &H1
  77. Public Const PD_SHOWHELP = &H800
  78. Public Const PD_USEDEVMODECOPIES = &H40000
  79. Public Const PD_USEDEVMODECOPIESANDCOLLATE = &H40000
  80.  
  81. 'Custom Global Constants
  82. Public Const DLG_PRINT = 0
  83. Public Const DLG_PRINTSETUP = 1
  84.  
  85. 'type definitions:
  86. Type PRINTDLG_TYPE
  87.     lStructSize As Long
  88.     hwndOwner As Long
  89.     hDevMode As Long
  90.     hDevNames As Long
  91.     hdc As Long
  92.     flags As Long
  93.     nFromPage As Integer
  94.     nToPage As Integer
  95.     nMinPage As Integer
  96.     nMaxPage As Integer
  97.     nCopies As Integer
  98.     hInstance As Long
  99.     lCustData As Long
  100.     lpfnPrintHook As Long
  101.  
  102.     lpfnSetupHook As Long
  103.     lpPrintTemplateName As String
  104.     lpSetupTemplateName As String
  105.     hPrintTemplate As Long
  106.     hSetupTemplate As Long
  107. End Type
  108.  
  109. Type DEVNAMES_TYPE
  110.     wDriverOffset As Integer
  111.     wDeviceOffset As Integer
  112.     wOutputOffset As Integer
  113.     wDefault As Integer
  114.     extra As String * 100
  115. End Type
  116.  
  117. Type DEVMODE_TYPE
  118.     dmDeviceName As String * CCHDEVICENAME
  119.     dmSpecVersion As Integer
  120.     dmDriverVersion As Integer
  121.     dmSize As Integer
  122.     dmDriverExtra As Integer
  123.     dmFields As Long
  124.     dmOrientation As Integer
  125.     dmPaperSize As Integer
  126.     dmPaperLength As Integer
  127.     dmPaperWidth As Integer
  128.     dmScale As Integer
  129.     dmCopies As Integer
  130.     dmDefaultSource As Integer
  131.     dmPrintQuality As Integer
  132.     dmColor As Integer
  133.     dmDuplex As Integer
  134.     dmYResolution As Integer
  135.     dmTTOption As Integer
  136.     dmCollate As Integer
  137.     dmFormName As String * CCHFORMNAME
  138.     dmUnusedPadding As Integer
  139.     dmBitsPerPel As Integer
  140.     dmPelsWidth As Long
  141.     dmPelsHeight As Long
  142.     dmDisplayFlags As Long
  143.     dmDisplayFrequency As Long
  144. End Type
  145.  
  146. Private Type OPENFILENAME
  147.     StructSize As Long
  148.     Owner As Long
  149.     Instance As Long
  150.     Filter As String
  151.     CustomFilter As String
  152.     MaxCustomFilter As Long
  153.     FilterIndex As Long
  154.     FileName As String
  155.     MaxFileNameLen As Long
  156.     FileTitle As String
  157.     MaxFileTitleLen As Long
  158.     InitDir As String
  159.     Title As String
  160.     flags As Long
  161.     FileOffset As Integer
  162.     FileExtension As Integer
  163.     DefaultExt As String
  164.     CustData As Long
  165.     Hook As Long
  166.     TemplateName As String
  167. End Type
  168.  
  169. Public Function FileOpenDlg(ByVal OwnerForm As Long, ByVal Title As String, ByVal Filters As String, ByVal InitDir As String, ByVal DfltExt As String, ByVal flags As Long, FileName As String) As Boolean
  170.     Dim OFN As OPENFILENAME
  171.     Dim Result As Long
  172.     Dim Filter As String
  173.     
  174.     FileName = String$(256, Chr$(0))
  175.     
  176.     OFN.StructSize = Len(OFN)
  177.     OFN.Owner = OwnerForm
  178.     OFN.Instance = App.hInstance
  179.     OFN.Filter = Filters
  180.     OFN.FilterIndex = 1
  181.     OFN.FileName = FileName
  182.     OFN.MaxFileNameLen = Len(OFN.FileName) - 1
  183.     OFN.FileTitle = OFN.FileName
  184.     OFN.MaxFileTitleLen = OFN.MaxFileNameLen
  185.     OFN.InitDir = InitDir
  186.     OFN.Title = Title
  187.     OFN.DefaultExt = DfltExt
  188.     OFN.flags = flags
  189.     
  190.     Result = GetOpenFileName(OFN)
  191.     
  192.     FileName = Left$(OFN.FileName, InStr(OFN.FileName, Chr$(0)) - 1)
  193.     
  194.     FileOpenDlg = IIf(Result = 1, True, False)
  195. End Function
  196.  
  197. Public Function FileSaveDlg(ByVal OwnerForm As Long, ByVal Title As String, ByVal Filters As String, ByVal InitDir As String, ByVal DfltExt As String, ByVal flags As Long, FileName As String) As Boolean
  198.     Dim OFN As OPENFILENAME
  199.     Dim Result As Long
  200.     Dim Filter As String
  201.     
  202.     OFN.StructSize = Len(OFN)
  203.     OFN.Owner = OwnerForm
  204.     OFN.Instance = App.hInstance
  205.     OFN.Filter = Filters
  206.     OFN.FilterIndex = 1
  207.     OFN.FileName = String$(257, 0)
  208.     OFN.MaxFileNameLen = Len(OFN.FileName) - 1
  209.     OFN.FileTitle = OFN.FileName
  210.     OFN.MaxFileTitleLen = OFN.MaxFileNameLen
  211.     OFN.InitDir = InitDir
  212.     OFN.Title = Title
  213.     OFN.DefaultExt = DfltExt
  214.     OFN.flags = flags
  215.     
  216.     Result = GetSaveFileName(OFN)
  217.     
  218.     FileName = Left$(OFN.FileName, InStr(OFN.FileName, Chr$(0)) - 1)
  219.     
  220.     FileSaveDlg = IIf(Result = 1, True, False)
  221. End Function
  222.  
  223. Public Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Long)
  224.  
  225.     Dim PrintDlg As PRINTDLG_TYPE
  226.     Dim DevMode As DEVMODE_TYPE
  227.     Dim DevName As DEVNAMES_TYPE
  228.  
  229.     Dim lpDevMode As Long, lpDevName As Long
  230.     Dim bReturn As Integer
  231.     Dim objPrinter As Printer, NewPrinterName As String
  232.     Dim strSetting As String
  233.  
  234.     ' Use PrintDialog to get the handle to a memory
  235.     ' block with a DevMode and DevName structures
  236.  
  237.     PrintDlg.lStructSize = Len(PrintDlg)
  238.     PrintDlg.hwndOwner = frmOwner.hwnd
  239.  
  240.     PrintDlg.flags = PrintFlags
  241.  
  242.     'Set the current orientation and duplex setting
  243.     DevMode.dmDeviceName = Printer.DeviceName
  244.     DevMode.dmSize = Len(DevMode)
  245.     DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
  246.     DevMode.dmOrientation = Printer.Orientation
  247.     On Error Resume Next
  248.     DevMode.dmDuplex = Printer.Duplex
  249.     On Error GoTo 0
  250.  
  251.     'Allocate memory for the initialization hDevMode structure
  252.     'and copy the settings gathered above into this memory
  253.     PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevMode))
  254.     lpDevMode = GlobalLock(PrintDlg.hDevMode)
  255.     If lpDevMode > 0 Then
  256.         CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
  257.         bReturn = GlobalUnlock(PrintDlg.hDevMode)
  258.     End If
  259.  
  260.     'Set the current driver, device, and port name strings
  261.     With DevName
  262.         .wDriverOffset = 8
  263.         .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
  264.         .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.DeviceName)
  265.         .wDefault = 0
  266.     End With
  267.     With Printer
  268.         DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0)
  269.     End With
  270.  
  271.     'Allocate memory for the initial hDevName structure
  272.     'and copy the settings gathered above into this memory
  273.     PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevName))
  274.     lpDevName = GlobalLock(PrintDlg.hDevNames)
  275.     If lpDevName > 0 Then
  276.         CopyMemory ByVal lpDevName, DevName, Len(DevName)
  277.         bReturn = GlobalUnlock(PrintDlg.hDevNames)
  278.     End If
  279.  
  280.     'Call the print dialog up and let the user make changes
  281.     If PrintDialog(PrintDlg) Then
  282.  
  283.         'First get the DevName structure.
  284.         lpDevName = GlobalLock(PrintDlg.hDevNames)
  285.         CopyMemory DevName, ByVal lpDevName, 45
  286.         bReturn = GlobalUnlock(PrintDlg.hDevNames)
  287.  
  288.         'Next get the DevMode structure and set the printer
  289.         'properties appropriately
  290.         lpDevMode = GlobalLock(PrintDlg.hDevMode)
  291.         CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
  292.         bReturn = GlobalUnlock(PrintDlg.hDevMode)
  293.         
  294.         NewPrinterName = UCase$(Left(DevMode.dmDeviceName, InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
  295.         If Printer.DeviceName <> NewPrinterName Then
  296.             For Each objPrinter In Printers
  297.                If UCase$(objPrinter.DeviceName) = NewPrinterName Then
  298.                     Set Printer = objPrinter
  299.                End If
  300.             Next
  301.         End If
  302.         On Error Resume Next
  303.  
  304.         'Set printer object properties according to selections made
  305.         'by user
  306.         DoEvents
  307.         With Printer
  308.             .Copies = DevMode.dmCopies
  309.             .Duplex = DevMode.dmDuplex
  310.             .Orientation = DevMode.dmOrientation
  311.         End With
  312.         On Error GoTo 0
  313.     End If
  314.     
  315.     GlobalFree PrintDlg.hDevNames
  316.     GlobalFree PrintDlg.hDevMode
  317. End Sub
  318.  
  319. Public Sub ShowHelp(OwnerForm As Long, HelpFileName As String, HelpCommand As Long, HelpData As Long)
  320.     WinHelp OwnerForm, HelpFileName, HelpCommand, HelpData
  321. End Sub
  322.