none
如何保存打印机设置 RRS feed

答案

全部回复

  • 可以用api PrinterProperties调出打印机属性对话框进行设置

    2008年1月24日 9:20
    版主
  • 请问,如果我想在程序中修改默认的打印机,以及其他的设置该怎么做呢?
    2008年1月25日 5:37
  •  liuyc 写:
    请问,如果我想在程序中修改默认的打印机,以及其他的设置该怎么做呢?

     

    http://blog.csdn.net/lzmtw/archive/2006/12/21/1451620.aspx

     

    需要引用System.Management空间再Imports System.Management
    使用:

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            
    For Each printer As String In PrinterService.GetPrinterNames(".")
                Console.WriteLine(printer)
            
    Next
        
    End Sub

        
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Console.WriteLine(PrinterService.SetDefaultPrinter(
    ".""EPSON LQ-1600KIII"))
        
    End Sub

     

    Public Class PrinterService

        
    Public Shared Function GetPrinterNames(ByVal computerName As StringAs String()
            
    Dim mResult(-1As String

            
    Dim gManagementObject As New ManagementObject
            gManagementObject.Scope 
    = New ManagementScope(String.Format("\\{0}\root\cimv2", computerName))
            
    Dim gSearcher As New ManagementObjectSearcher(String.Format("SELECT DeviceID FROM {0}""Win32_Printer"))
            gSearcher.Scope 
    = gManagementObject.Scope
            
    Try
                
    For Each mManagementObject As ManagementObject In gSearcher.Get()
                    
    ReDim Preserve mResult(mResult.Length)
                    mResult(mResult.Length 
    - 1= mManagementObject.Properties.Item("DeviceID").Value
                
    Next
            
    Catch ex As Exception
                Console.WriteLine(ex.Message)
            
    Finally
                gSearcher.Dispose()
                gManagementObject.Dispose()
            
    End Try

            
    Return mResult
        
    End Function

        
    Public Shared Function SetDefaultPrinter(ByVal computerName As StringByVal PrinterName As StringAs Boolean
            
    Dim mResult As Boolean = False

            
    Dim gManagementObject As New ManagementObject
            gManagementObject.Scope 
    = New ManagementScope(String.Format("\\{0}\root\cimv2", computerName))
            
    Dim gSearcher As New ManagementObjectSearcher(String.Format("SELECT DeviceID FROM {0}""Win32_Printer"))
            gSearcher.Scope 
    = gManagementObject.Scope
            
    Try
                
    For Each mManagementObject As ManagementObject In gSearcher.Get()
                    
    If mManagementObject.Properties.Item("DeviceID").Value.ToString.Equals(PrinterName) Then
                        mManagementObject.InvokeMethod(
    "SetDefaultPrinter"Nothing)
                        
    Exit For
                    
    End If
                
    Next

                mResult 
    = True
            
    Catch ex As Exception
                Console.WriteLine(ex.Message)
            
    Finally
                gSearcher.Dispose()
                gManagementObject.Dispose()
            
    End Try

            
    Return mResult
        
    End Function
    End Class

     

     

    2008年1月25日 6:38
    版主
  • feiyun0112谢谢了:)

    麻烦看看下面的代码。原代码是在VB6下使用API调用打印设置对话框(这个对话框与VB2005提供的样子不同)。

    Code Snippet

    Public Const PD_HIDEPRINTTOFILE = &H100000
    Public Const CCHDEVICENAME = 32
    Public Const CCHFORMNAME = 32

    'type definitions:
    Type PRINTDLG_TYPE
           lStructSize As Long
           hwndOwner As Long
           hDevMode As Long
           hDevNames As Long
           hdc As Long
           flags As Long
           nFromPage As Integer
           nToPage As Integer
           nMinPage As Integer
           nMaxPage As Integer
           nCopies As Integer
           hInstance As Long
           lCustData As Long
           lpfnPrintHook As Long

           lpfnSetupHook As Long
           lpPrintTemplateName As String
           lpSetupTemplateName As String
           hPrintTemplate As Long
           hSetupTemplate As Long
    End Type

    Type DEVNAMES_TYPE
           wDriverOffset As Integer
           wDeviceOffset As Integer
           wOutputOffset As Integer
           wDefault As Integer
           extra As String * 100
    End Type

    Type DEVMODE_TYPE
           dmDeviceName As String * CCHDEVICENAME
           dmSpecVersion As Integer
           dmDriverVersion As Integer
           dmSize As Integer
           dmDriverExtra As Integer
           dmFields As Long
           dmOrientation As Integer
           dmPaperSize As Integer
           dmPaperLength As Integer
           dmPaperWidth As Integer
           dmScale As Integer
           dmCopies As Integer
           dmDefaultSource As Integer
           dmPrintQuality As Integer
           dmColor As Integer
           dmDuplex As Integer
           dmYResolution As Integer
           dmTTOption As Integer
           dmCollate As Integer
           dmFormName As String * CCHFORMNAME
           dmUnusedPadding As Integer
           dmBitsPerPel As Integer
           dmPelsWidth As Long
           dmPelsHeight As Long
           dmDisplayFlags As Long
           dmDisplayFrequency As Long
    End Type

    'API declarations:
    Public Declare Function PrintDialog Lib "comdlg32.dll" _
      Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long

    Public Declare Sub CopyMemory Lib "kernel32" _
      Alias "RtlMoveMemory" _
      (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

    Public Declare Function GlobalLock Lib "kernel32" _
      (ByVal hMem As Long) As Long

    Public Declare Function GlobalUnlock Lib "kernel32" _
      (ByVal hMem As Long) As Long

    Public Declare Function GlobalAlloc Lib "kernel32" _
      (ByVal wFlags As Long, ByVal dwBytes As Long) As Long

    Public Declare Function GlobalFree Lib "kernel32" _
      (ByVal hMem As Long) As Long
     
    Private Declare Function SetDefaultPrinter Lib "Winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long


    Public Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Variant)
      
       Dim PrintDlg As PRINTDLG_TYPE
       Dim DevMode As DEVMODE_TYPE
       Dim DevName As DEVNAMES_TYPE

       Dim lpDevMode As Long, lpDevName As Long
       Dim bReturn As Integer
     
       ' Use PrintDialog to get the handle to a memory
       ' block with a DevMode and DevName structures

       PrintDlg.lStructSize = Len(PrintDlg)
       PrintDlg.hwndOwner = frmOwner.hWnd

       If VarType(PrintFlags) <> vbLong Then
           PrintFlags = 0
       End If

       PrintDlg.flags = PrintFlags

       'Set the current orientation and duplex setting
       DevMode.dmDeviceName = Printer.DeviceName
       DevMode.dmSize = Len(DevMode)
       'DevMode.dmFields = DM_ORIENTATION
       'DevMode.dmOrientation = Printer.Orientation
       'DevMode.dmFields = DM_PAPERSIZE
       'DevMode.dmPaperSize = Printer.PaperSize
       On Error Resume Next
       On Error GoTo 0

       'Allocate memory for the initialization hDevMode structure
       'and copy the settings gathered above into this memory
       PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or _
          GMEM_ZEROINIT, Len(DevMode))
       lpDevMode = GlobalLock(PrintDlg.hDevMode)
       If lpDevMode > 0 Then
           CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
           bReturn = GlobalUnlock(lpDevMode)
       End If

       'Set the current driver, device, and port name strings
       With DevName
           .wDriverOffset = 8
           .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
           .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
           .wDefault = 0
       End With
     

       'Call the print dialog up and let the user make changes
       If PrintDialog(PrintDlg) Then

           'First get the DevName structure.
           lpDevName = GlobalLock(PrintDlg.hDevNames)
               CopyMemory DevName, ByVal lpDevName, 45
           bReturn = GlobalUnlock(lpDevName)
           GlobalFree PrintDlg.hDevNames

           'Next get the DevMode structure and set the printer
           'properties appropriately
           lpDevMode = GlobalLock(PrintDlg.hDevMode)
               CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
           bReturn = GlobalUnlock(PrintDlg.hDevMode)
           GlobalFree PrintDlg.hDevMode
           NewPrinterName = UCase$(Left(DevMode.dmDeviceName, _
               InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
     
       End If
      
       Call SetDefaultPrinter(NewPrinterName)

    End Sub




    升级到Vb2005后,使用CopyMemmory就取不到选择的打印机名称,所以后面的设置直接无效了:(

    调用的代码如下
    Call ShowPrinter(Me, PD_PRINTSETUP + PD_HIDEPRINTTOFILE)
    2008年1月25日 8:50
  • 不用这么麻烦,因为.NET提供了PrinterSettings类可以完成大部分工作

     

     

    Code Snippet
    Imports System.Runtime.InteropServices
    Imports System.Drawing.Printing

     

     

    Code Snippet

    Public Const PD_PRINTSETUP As Short = &H40s
     Public Const PD_HIDEPRINTTOFILE As Integer = &H100000

     


        _
         _
         Friend Class PRINT_DLG
            Public lStructSize As Int32
            Public hwndOwner As IntPtr
            Public hDevMode As IntPtr
            Public hDevNames As IntPtr
            Public hDC As IntPtr = IntPtr.Zero
            Public Flags As Int32
            Public FromPage As Int16 = 0
            Public ToPage As Int16 = 0
            Public MinPage As Int16 = 0
            Public MaxPage As Int16 = 0
            Public Copies As Int16 = 0
            Public hInstance As IntPtr = IntPtr.Zero
            Public lCustData As IntPtr = IntPtr.Zero
            Public lpfnPrintHook As IntPtr = IntPtr.Zero
            Public lpfnSetupHook As IntPtr = IntPtr.Zero
            Public lpPrintTemplateName As IntPtr = IntPtr.Zero
            Public lpSetupTemplateName As IntPtr = IntPtr.Zero
            Public hPrintTemplate As IntPtr = IntPtr.Zero
            Public hSetupTemplate As IntPtr = IntPtr.Zero
        End Class

        Private Declare Auto Function PrintDlg Lib "comdlg32.dll" (<[In](), Out()> ByVal lppd As PRINT_DLG) As Boolean
        Private Declare Function SetDefaultPrinter Lib "Winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Integer

     

     

       

    Code Snippet

    Public Sub ShowPrinter(ByRef frmOwner As System.Windows.Forms.Form, ByVal PrintFlags As Integer)
            Dim _pd As New PRINT_DLG
            Dim printerSettings As PrinterSettings
            Dim document As New PrintDocument()

            printerSettings = document.PrinterSettings

            _pd.lStructSize = Marshal.SizeOf(_pd)
            _pd.hwndOwner = frmOwner.Handle

            _pd.hDevMode = printerSettings.GetHdevmode()
            _pd.hDevNames = printerSettings.GetHdevnames()

            _pd.Flags = PrintFlags

            PrintDlg(_pd)

            printerSettings.SetHdevmode(_pd.hDevMode)
            printerSettings.SetHdevnames(_pd.hDevNames)

            Dim NewPrinterName As String
            NewPrinterName = printerSettings.PrinterName

            SetDefaultPrinter(NewPrinterName)
        End Sub

     

     

    2008年1月25日 9:55
    版主
  • feiyun0112,谢谢你提供的这个方法。但是很遗憾它跑不起来呀:(
    2008年1月28日 8:11
  •  liuyc 写:
    feiyun0112,谢谢你提供的这个方法。但是很遗憾它跑不起来呀:(

     

    少了2句话

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1)> _
        <System.Runtime.InteropServices.ComVisible(False)> _
        Friend Class PRINT_DLG

    2008年1月28日 8:47
    版主
  • 好了:)
    太谢谢你了,
    feiyun0112
    有机会到大连来,俺请客
    (
    、)
    (
    2008年1月28日 11:42