请问VB.NET中怎么获取当前打印机的纸张类型
2009-04-16 19:27
381 查看
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Security
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Public Class Form1
Private Const PRINTER_ENUM_LOCAL As Integer = 2
Private Const PRINTER_ENUM_CONNECTIONS As Integer = 4
Private Const DC_PAPERNAMES As Integer = 16
Private Const DC_PAPERS As Integer = 2
Private Const DC_PAPERSIZE As Integer = 3
Private Const DC_BINNAMES As Integer = 12
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure PRINTER_INFO_5
<MarshalAs(UnmanagedType.LPTStr)> _
Public PrinterName As String
<MarshalAs(UnmanagedType.LPTStr)> _
Public PortName As String
<MarshalAs(UnmanagedType.U4)> _
Public Attributes As Int32
<MarshalAs(UnmanagedType.U4)> _
Public DeviceNotSelectedTimeout As Int32
<MarshalAs(UnmanagedType.U4)> _
Public TransmissionRetryTimeout As Int32
End Structure
<DllImport("winspool.drv", EntryPoint:="DeviceCapabilitiesA", SetLastError:=True)> _
Public Shared Function DeviceCapabilities( _
ByVal device As String, _
ByVal port As String, _
ByVal capability As Int16, _
ByVal outputBuffer As IntPtr, _
ByVal deviceMode As IntPtr) As Int32
End Function
<DllImport("winspool.drv", SetLastError:=True)> _
Public Shared Function EnumPrintersW(ByVal flags As Int32, _
ByVal printerName As String, _
ByVal level As Int32, ByVal buffer As IntPtr, ByVal bufferSize As Int32, ByRef _
requiredBufferSize As Int32, ByRef numPrintersReturned As Int32) As Boolean
End Function
<DllImport("kernel32.dll", EntryPoint:="GetLastError", SetLastError:=False, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall), _
SuppressUnmanagedCodeSecurityAttribute()> _
Public Shared Function GetLastError() As Int32
End Function
Public Shared Sub GetDefinedPapers(ByVal printerName As String)
Dim info5 As PRINTER_INFO_5
Dim PRINT_INFO As PRINTER_INFO_5 = Nothing
Dim requiredSize As Integer
Dim numPrinters As Integer
Dim intI As Integer
Dim foundPrinter As Boolean = EnumPrintersW(PRINTER_ENUM_LOCAL + PRINTER_ENUM_CONNECTIONS, _
String.Empty, 5, IntPtr.Zero, 0, requiredSize, numPrinters)
Dim info5Size As Integer = requiredSize
Dim info5Ptr As IntPtr = Marshal.AllocHGlobal(info5Size)
Dim buffer As IntPtr = IntPtr.Zero
Try
foundPrinter = EnumPrintersW(PRINTER_ENUM_LOCAL + PRINTER_ENUM_CONNECTIONS, _
String.Empty, 5, info5Ptr, info5Size, requiredSize, numPrinters)
Dim port As String = Nothing
For intI = 0 To numPrinters - 1
info5 = Marshal.PtrToStructure(CType(Integer.Parse(intI * Marshal.SizeOf(PRINT_INFO.GetType)) + CType(info5Ptr, Int32), IntPtr), PRINT_INFO.GetType)
If info5.PrinterName = printerName Then
port = info5.PortName
Exit For
End If
Next
Dim numNames As Integer
numNames = DeviceCapabilities(printerName, port, DC_PAPERNAMES, IntPtr.Zero, IntPtr.Zero)
If (numNames < 0) Then
Dim errorCode As Integer = GetLastError()
Console.WriteLine("Number of names = {1}: {0}", errorCode, numNames)
Return
End If
buffer = Marshal.AllocHGlobal(numNames * 64)
numNames = DeviceCapabilities(printerName, port, DC_PAPERNAMES, buffer, IntPtr.Zero)
If numNames < 0 Then
Dim errorCode As Integer = GetLastError()
Console.WriteLine("Number of names = {1}: {0}", errorCode, numNames)
Return
End If
Dim names(numNames - 1) As String
For intI = 0 To numNames - 1
names(intI) = Marshal.PtrToStringAnsi(CType((intI * 64) + CType(buffer, Integer), IntPtr))
Next
Marshal.FreeHGlobal(buffer)
buffer = IntPtr.Zero
Dim numPapers As Integer = DeviceCapabilities(printerName, port, DC_PAPERS, IntPtr.Zero, IntPtr.Zero)
If numPapers < 0 Then
Console.WriteLine("No papers")
Return
End If
buffer = Marshal.AllocHGlobal(numPapers * 2)
numPapers = DeviceCapabilities(printerName, port, DC_PAPERS, buffer, IntPtr.Zero)
If numPapers < 0 Then
Console.WriteLine("No papers")
Return
End If
Dim kinds(numPapers - 1) As String
For intI = 0 To numPapers - 1
kinds(intI) = Marshal.ReadInt16(buffer, intI * 2)
Next
For intI = 0 To numPapers - 1
Console.WriteLine("Paper {0} : {1}", kinds(intI), names(intI))
Next
Dim numPaperboxs As Integer = DeviceCapabilities(printerName, port, DC_BINNAMES, IntPtr.Zero, IntPtr.Zero)
If numPaperboxs < 0 Then
Console.WriteLine("No papers")
Return
End If
buffer = Marshal.AllocHGlobal(numPapers * 2)
numPaperboxs = DeviceCapabilities(printerName, port, DC_BINNAMES, buffer, IntPtr.Zero)
If numPaperboxs < 0 Then
Console.WriteLine("No papers")
Return
End If
Dim arrBoxsNm(numPaperboxs - 1) As String
For intI = 0 To numPaperboxs - 1
arrBoxsNm(intI) = Marshal.PtrToStringAnsi(CType((intI * 24) + CType(buffer, Integer), IntPtr))
Next
For intI = 0 To numPaperboxs - 1
Console.WriteLine("PaperboxNm {0}", arrBoxsNm(intI))
Next
Finally
Marshal.FreeHGlobal(info5Ptr)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetDefinedPapers("//Print/Canon LASER SHOT LBP-1420")
End Sub
End Class
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Security
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Public Class Form1
Private Const PRINTER_ENUM_LOCAL As Integer = 2
Private Const PRINTER_ENUM_CONNECTIONS As Integer = 4
Private Const DC_PAPERNAMES As Integer = 16
Private Const DC_PAPERS As Integer = 2
Private Const DC_PAPERSIZE As Integer = 3
Private Const DC_BINNAMES As Integer = 12
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure PRINTER_INFO_5
<MarshalAs(UnmanagedType.LPTStr)> _
Public PrinterName As String
<MarshalAs(UnmanagedType.LPTStr)> _
Public PortName As String
<MarshalAs(UnmanagedType.U4)> _
Public Attributes As Int32
<MarshalAs(UnmanagedType.U4)> _
Public DeviceNotSelectedTimeout As Int32
<MarshalAs(UnmanagedType.U4)> _
Public TransmissionRetryTimeout As Int32
End Structure
<DllImport("winspool.drv", EntryPoint:="DeviceCapabilitiesA", SetLastError:=True)> _
Public Shared Function DeviceCapabilities( _
ByVal device As String, _
ByVal port As String, _
ByVal capability As Int16, _
ByVal outputBuffer As IntPtr, _
ByVal deviceMode As IntPtr) As Int32
End Function
<DllImport("winspool.drv", SetLastError:=True)> _
Public Shared Function EnumPrintersW(ByVal flags As Int32, _
ByVal printerName As String, _
ByVal level As Int32, ByVal buffer As IntPtr, ByVal bufferSize As Int32, ByRef _
requiredBufferSize As Int32, ByRef numPrintersReturned As Int32) As Boolean
End Function
<DllImport("kernel32.dll", EntryPoint:="GetLastError", SetLastError:=False, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall), _
SuppressUnmanagedCodeSecurityAttribute()> _
Public Shared Function GetLastError() As Int32
End Function
Public Shared Sub GetDefinedPapers(ByVal printerName As String)
Dim info5 As PRINTER_INFO_5
Dim PRINT_INFO As PRINTER_INFO_5 = Nothing
Dim requiredSize As Integer
Dim numPrinters As Integer
Dim intI As Integer
Dim foundPrinter As Boolean = EnumPrintersW(PRINTER_ENUM_LOCAL + PRINTER_ENUM_CONNECTIONS, _
String.Empty, 5, IntPtr.Zero, 0, requiredSize, numPrinters)
Dim info5Size As Integer = requiredSize
Dim info5Ptr As IntPtr = Marshal.AllocHGlobal(info5Size)
Dim buffer As IntPtr = IntPtr.Zero
Try
foundPrinter = EnumPrintersW(PRINTER_ENUM_LOCAL + PRINTER_ENUM_CONNECTIONS, _
String.Empty, 5, info5Ptr, info5Size, requiredSize, numPrinters)
Dim port As String = Nothing
For intI = 0 To numPrinters - 1
info5 = Marshal.PtrToStructure(CType(Integer.Parse(intI * Marshal.SizeOf(PRINT_INFO.GetType)) + CType(info5Ptr, Int32), IntPtr), PRINT_INFO.GetType)
If info5.PrinterName = printerName Then
port = info5.PortName
Exit For
End If
Next
Dim numNames As Integer
numNames = DeviceCapabilities(printerName, port, DC_PAPERNAMES, IntPtr.Zero, IntPtr.Zero)
If (numNames < 0) Then
Dim errorCode As Integer = GetLastError()
Console.WriteLine("Number of names = {1}: {0}", errorCode, numNames)
Return
End If
buffer = Marshal.AllocHGlobal(numNames * 64)
numNames = DeviceCapabilities(printerName, port, DC_PAPERNAMES, buffer, IntPtr.Zero)
If numNames < 0 Then
Dim errorCode As Integer = GetLastError()
Console.WriteLine("Number of names = {1}: {0}", errorCode, numNames)
Return
End If
Dim names(numNames - 1) As String
For intI = 0 To numNames - 1
names(intI) = Marshal.PtrToStringAnsi(CType((intI * 64) + CType(buffer, Integer), IntPtr))
Next
Marshal.FreeHGlobal(buffer)
buffer = IntPtr.Zero
Dim numPapers As Integer = DeviceCapabilities(printerName, port, DC_PAPERS, IntPtr.Zero, IntPtr.Zero)
If numPapers < 0 Then
Console.WriteLine("No papers")
Return
End If
buffer = Marshal.AllocHGlobal(numPapers * 2)
numPapers = DeviceCapabilities(printerName, port, DC_PAPERS, buffer, IntPtr.Zero)
If numPapers < 0 Then
Console.WriteLine("No papers")
Return
End If
Dim kinds(numPapers - 1) As String
For intI = 0 To numPapers - 1
kinds(intI) = Marshal.ReadInt16(buffer, intI * 2)
Next
For intI = 0 To numPapers - 1
Console.WriteLine("Paper {0} : {1}", kinds(intI), names(intI))
Next
Dim numPaperboxs As Integer = DeviceCapabilities(printerName, port, DC_BINNAMES, IntPtr.Zero, IntPtr.Zero)
If numPaperboxs < 0 Then
Console.WriteLine("No papers")
Return
End If
buffer = Marshal.AllocHGlobal(numPapers * 2)
numPaperboxs = DeviceCapabilities(printerName, port, DC_BINNAMES, buffer, IntPtr.Zero)
If numPaperboxs < 0 Then
Console.WriteLine("No papers")
Return
End If
Dim arrBoxsNm(numPaperboxs - 1) As String
For intI = 0 To numPaperboxs - 1
arrBoxsNm(intI) = Marshal.PtrToStringAnsi(CType((intI * 24) + CType(buffer, Integer), IntPtr))
Next
For intI = 0 To numPaperboxs - 1
Console.WriteLine("PaperboxNm {0}", arrBoxsNm(intI))
Next
Finally
Marshal.FreeHGlobal(info5Ptr)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetDefinedPapers("//Print/Canon LASER SHOT LBP-1420")
End Sub
End Class
相关文章推荐
- VB中获取指定打印机的各种纸张类型及大小
- [VB.NET]请问:怎么获取DATAGRID中的值?
- [VB.NET]当前打印机的名字怎么取得啊?在线等!
- [VB.NET]请问在WINCE中怎么获得当前线程ID呢?有些难度的问题呀!!!!!!
- VB.Net实现打印机纸张类型自动更换的方法
- VB.net中的DataGridView获取当前被选中的行号
- VB.NET获取系统当前精确时间(毫秒级)
- VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)
- [VB.NET]请问怎么把datagridview某行的内容显示在textbox上?
- [VB.NET]请问一个页面后台中 进行2次数据库读取 上下之间的值怎么传 在线等!!!!!
- [VB.NET]vb.net里怎么获取控件的字节数
- vb.net 获取数据表 字段名和数据类型
- VB.NET中获取当前界面的分辨率
- 请问在VB.NET 3.5中如何通过SqlDataSource 获取数据
- [VB.NET]请问连接access的代码怎么写?
- [VB.NET]请问如何取得多选中的listBox当前选中的文本内容?
- vb.net 获取当前应用程序所在的路径
- WinForm(VB.NET)获取当前登录的用户名
- VB.Net 获取当前系统的时间分割符
- vb.net 获取当前程序的版本(软件自己的版本)