' 發展單位:風禹科技驗證有限公司 ' 撰寫人:鄭子璉(Tzu-Lien, Cheng, 璉璉) ,成大水博肄,微軟最有價值專家 ' Web: http://tlcheng.twbbs.org/TLCheng/ E-Mail: qvb3377@ms5.hinet.net ' -------------------------------------------------------------------------------------- Imports System.Runtime.InteropServices Imports System.ComponentModel Module modPrinter _ Private Function OpenPrinter( _ ByVal pPrinterName As String, ByRef hPrinter As IntPtr, _ ByVal pDefault As IntPtr) As Boolean End Function _ Private Function ClosePrinter( _ ByVal hPrinter As IntPtr) As Boolean End Function _ Private Function GetPrinter( _ ByVal hPrinter As IntPtr, ByVal dwLevel As Integer, _ ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, _ ByRef pcbNeeded As Integer) As Boolean End Function _ Public Structure PRINTER_INFO_2 Public pServerName As String Public pPrinterName As String Public pShareName As String Public pPortName As String Public pDriverName As String Public pComment As String Public pLocation As String Public pDevMode As IntPtr Public pSepFile As String Public pPrintProcessor As String Public pDatatype As String Public pParameters As String Public pSecurityDescriptor As IntPtr Public Attributes As System.UInt32 Public Priority As System.UInt32 Public DefaultPriority As System.UInt32 Public StartTime As System.UInt32 Public UntilTime As System.UInt32 Public Status As System.UInt32 Public cJobs As System.UInt32 Public AveragePPM As System.UInt32 End Structure Public Function GetPrintPageArea(ByVal docPageSetting As System.Drawing.Printing.PageSettings) As System.Drawing.RectangleF Dim rectPrintingArea As RectangleF With docPageSetting Dim printSize As Size If .Landscape Then printSize = New Size(.PaperSize.Height - .Margins.Left - .Margins.Right, .PaperSize.Width - .Margins.Top - .Margins.Bottom) Else printSize = New Size(.PaperSize.Width - .Margins.Left - .Margins.Right, .PaperSize.Height - .Margins.Top - .Margins.Bottom) End If rectPrintingArea = New RectangleF(.Margins.Left, .Margins.Top, printSize.Width, printSize.Height) End With Return rectPrintingArea End Function Public Function SetPrinterMargins(ByVal vPageSetings As Printing.PageSettings, Optional ByVal Left As Double = 100, Optional ByVal Top As Double = 100, Optional ByVal Right As Double = 100, Optional ByVal Bottom As Double = 100, Optional ByVal bUseMetric As Boolean = False) As System.Drawing.Printing.Margins With vPageSetings If bUseMetric Then Dim Scale As Double = 100 / 25.4 Left *= Scale Top *= Scale Right *= Scale Bottom *= Scale End If .Margins.Left = Math.Max(Left, .PrintableArea.Left) .Margins.Top = Math.Max(Top, .PrintableArea.Top) .Margins.Right = Math.Max(Right, .PaperSize.Width - .PrintableArea.Right) .Margins.Bottom = Math.Max(Bottom, .PaperSize.Height - .PrintableArea.Bottom) Return .Margins End With End Function Public Function GetPrinterInfo(ByVal printerName As String) As PRINTER_INFO_2 Dim hPrinter As IntPtr If Not OpenPrinter(printerName, hPrinter, IntPtr.Zero) Then Throw New Win32Exception(Marshal.GetLastWin32Error()) End If Dim pPrinterInfo As IntPtr = IntPtr.Zero Try Dim needed As Integer GetPrinter(hPrinter, 2, IntPtr.Zero, 0, needed) If needed <= 0 Then Throw New Exception("擷取失敗") End If pPrinterInfo = Marshal.AllocHGlobal(needed) Dim temp As Integer If Not GetPrinter(hPrinter, 2, pPrinterInfo, needed, temp) Then Throw New Win32Exception(Marshal.GetLastWin32Error()) End If Dim printerInfo As PRINTER_INFO_2 = CType(Marshal.PtrToStructure(pPrinterInfo, GetType(PRINTER_INFO_2)), PRINTER_INFO_2) Return printerInfo Finally ClosePrinter(hPrinter) Marshal.FreeHGlobal(pPrinterInfo) End Try End Function Public Function GetPrinterPortByName(Optional ByVal strPrinterName As String = "") As String If Len(strPrinterName) = 0 Then strPrinterName = GetDefaultPrinterName() End If Dim prn2 As PRINTER_INFO_2 = GetPrinterInfo(strPrinterName) Return prn2.pPortName End Function Public Function GetDefaultPrinterName() As String Dim prn As New Printing.PageSettings Return prn.PrinterSettings.PrinterName() End Function Public Function SetDefaultPrinterByName(Optional ByVal strPrinterName As String = "") As String Dim prn As New Printing.PageSettings Dim rtnPrinterName As String rtnPrinterName = prn.PrinterSettings.PrinterName() 'prn.PrinterSettings.PrinterName = strPrinterName If Len(strPrinterName) > 0 Then ' Script 5.6 線上手冊 Dim t As Type = Type.GetTypeFromProgID("WScript.Network") Dim wshNetwork As Object = Activator.CreateInstance(t) t.InvokeMember("SetDefaultPrinter", System.Reflection.BindingFlags.InvokeMethod, Nothing, wshNetwork, New Object() {strPrinterName}) End If Return rtnPrinterName End Function Public Function GetAllPrinterName() As String() Dim prn As New Printing.PageSettings Dim arrPrinter As System.Drawing.Printing.PrinterSettings.StringCollection = prn.PrinterSettings.InstalledPrinters Dim rtnName(arrPrinter.Count - 1) As String Dim ip As Integer For ip = 0 To arrPrinter.Count - 1 rtnName(ip) = arrPrinter.Item(ip) Next Return rtnName End Function End Module