Advertisement
Java_Volume1 Windows API Call/ Explanation #90425

Dialogs

This is one of over a hundred modules I have developed for getting my work done faster. This module display various system dialog boxes to configure COM ports, printer ports, get the default printer, view printer properties, and view document properties.

AI

Résumé par IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Code source
original-source
'*************************************************
' modPrinterDialogs:
' This module displays a number of dialogs, which
' are provided by the following functions:
'
' ConfigureCOMPort():   Configure the specified COM port number (1-4)
' ConfigureLPTPort():   Configure the specified Printer port number (1-4)
' ConfigureAPort():    Configure a specified port
' GetDefaultPrinter():   This function retrieves the definition
'             of the default printer on this system
' ViewPrinterProperties(): View/change printer properties dialog
' ViewDocProperties():   View/change document properties
' ConnectToAPrinter():   Connect to a local/network printer
'
'EXAMPLES:
' Dim dm As DEVMODE         'used to gather data by ViewDocProperties()
'
' Call ConfigureAPort(Me, "COM2:") 'configure COM port 2
' Call ConfigureCOMPort(Me, 2)   'configure COM port 2
' Call ConfigureLPTPort(Me, 1)   'configure LPT port 1
' Debug.Print GetDefaultPrinter   'display default printer name, device, port
' Call ViewPrinterProperties(Me)  'view/change default printer's properties
' Call ConnectToAPrinter(Me)    'connect to a local/network printer
' Call ViewDocProperties(Me, dm)  'set up document printing options.
' Debug.Print "Copies = " & dm.dmCopies
' Debug.Print "Orientation = " & dm.dmOrientation
' Debug.Print "Quality = " & dm.dmPrintQuality
'*************************************************
''''INSERT API/Global goodies here
'*************************************************
' ConfigureCOMPort(): Configure the specified COM port number (1-4)
'*************************************************
Public Function ConfigureCOMPort(Frm As Form, PortNumber As Integer)
 ConfigureCOMPort = ConfigurePort("", Frm.hWnd, "COM" & CStr(PortNumber) & ":")
End Function
'*************************************************
' ConfigureLPTPort(): Configure the specified Printer port number (1-4)
'*************************************************
Public Function ConfigureLPTPort(Frm As Form, PortNumber As Integer)
 ConfigureLPTPort = ConfigurePort("", Frm.hWnd, "LPT" & CStr(PortNumber) & ":")
End Function
'*************************************************
' ConfigureAPort(): Configure a specified port
'*************************************************
Public Function ConfigureAPort(Frm As Form, PortName As String)
 ConfigureAPort = ConfigurePort("", Frm.hWnd, UCase$(PortName))
End Function
'*************************************************
' ViewPrinterProperties(): View/change printer properties dialog
'*************************************************
Public Sub ViewPrinterProperties(Frm As Form, Optional PrtDevice As String = "")
  Dim hPrinter As Long
  
  hPrinter& = OpenAPrinter(PrtDevice)
  If hPrinter = 0 Then
    If PrtDevice = "" Then
     MsgBox "Unable to open default printer"
    Else
     MsgBox "Unable to open " & PrtDevice & " printer"
    End If
    Exit Sub
  End If
  Call PrinterProperties(Frm.hWnd, hPrinter)
  Call ClosePrinter(hPrinter)
End Sub
'*************************************************
' ViewDocProperties(): View/change document properties
'*************************************************
Public Sub ViewDocProperties(Frm As Form, MyDevMode As DEVMODE, Optional DeviceName As String = "")
  Dim bufsize As Long, res As Long
  Dim dmInBuf As String
  Dim dmOutBuf As String
  Dim hPrinter As Long
    
  hPrinter = OpenAPrinter(DeviceName)
  If hPrinter = 0 Then
   If DeviceName = "" Then
    MsgBox "Unable to open default printer"
   Else
    MsgBox "Unable to open " & DeviceName & " printer"
   End If
   Exit Sub
  End If
  ' The output DEVMODE structure will reflect any changes
  ' made by the printer setup dialog box.
  ' Note that no changes will be made to the default
  ' printer settings!
  bufsize = DocumentProperties(Frm.hWnd, hPrinter, DeviceName, 0, 0, 0)
  dmInBuf = String(bufsize, 0)
  dmOutBuf = String(bufsize, 0)
  
  res = DocumentPropertiesStr(Frm.hWnd, hPrinter, DeviceName, dmOutBuf, dmInBuf, DM_IN_PROMPT Or DM_OUT_BUFFER)
    
  ' Copy the data buffer into the DEVMODE structure
  CopyMemoryDM MyDevMode, dmOutBuf, Len(MyDevMode)
ClosePrinter hPrinter
End Sub
'*************************************************
' ConnectToAPrinter(): Connect to a local/network printer
'*************************************************
Public Sub ConnectToAPrinter(Frm As Form)
 Call ConnectToPrinterDlg(Frm.hWnd, 0)
End Sub
'*************************************************
' GetDefaultPrinter(): This function retrieves the definition
'           of the default printer on this system
'*************************************************
Public Function GetDefaultPrinter() As String
  Dim def As String
  Dim di As Long
  def = String(128, 0)
  di = GetProfileString("WINDOWS", "DEVICE", "", def, 127)
  If di Then GetDefaultPrinter = Left$(def, di - 1)
End Function
'*************************************************
' OpenAPrinter(): open a printer (default or user-specified)
'*************************************************
Private Function OpenAPrinter(Optional DeviceName As String = "") As Long
  Dim dev$, devname As String, devoutput As String
  Dim hPrinter As Long, res As Long
  Dim pdefs As PRINTER_DEFAULTS
  
  pdefs.pDatatype = vbNullString
  pdefs.pDevMode = 0
  pdefs.DesiredAccess = PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE
  If DeviceName = "" Then
   dev = GetDefaultPrinter() ' Get default printer info
   If dev = "" Then Exit Function
   DeviceName = GetDeviceName(dev)
  End If
  devname = DeviceName
  
  ' You can use OpenPrinterBynum to pass a zero as the
  ' third parameter, but you won't have full access to
  ' edit the printer properties
  res = OpenPrinter(devname, hPrinter, pdefs)
  If res <> 0 Then OpenAPrinter = hPrinter
End Function
'*************************************************
'  Retrieves the name portion of a device string
'*************************************************
Private Function GetDeviceName(dev As String) As String
  Dim npos As Integer
  
  npos = InStr(dev, ",")
  GetDeviceName = Left$(dev, npos - 1)
End Function
Commentaires originaux (3)
Récupéré via Wayback Machine