|
Imports System.Runtime.InteropServices Public Class Form1 'Public Const DMPAPER_A4 = 9 Public Const DC_PAPERS As Integer = 2 Public Const DC_PAPERNAMES As Integer = 16 Public Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000 Public Const PRINTER_ACCESS_ADMINISTER As Integer = &H4 Public Const PRINTER_ACCESS_USE As Integer = &H8 Public Const PRINTER_ALL_ACCESS As Integer = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE) <DllImport("winspool.drv", CharSet:=CharSet.Auto)> _ Private Shared Function OpenPrinter( _ ByVal pPrinterName As String, _ ByRef hPrinter As IntPtr, _ ByRef pDefault As PRINTER_DEFAULTS) As Boolean End Function <DllImport("winspool.drv", CharSet:=CharSet.Auto)> _ Private Shared 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 <DllImport("winspool.drv", CharSet:=CharSet.Auto)> _ Private Shared Function SetPrinter( _ ByVal hPrinter As IntPtr, _ ByVal dwLevel As Integer, _ ByVal pPrinter As IntPtr, _ ByVal Command As Integer) As Boolean End Function <DllImport("winspool.drv", CharSet:=CharSet.Auto)> _ Private Shared Function ClosePrinter( _ ByVal hPrinter As IntPtr) As Boolean End Function Declare Function DeviceCapabilities Lib "winspool.drv" Alias "DeviceCapabilitiesA" ( _ ByVal pDevice As String, _ ByVal pPort As String, _ ByVal fwCapability As Short, _ ByVal pOutput() As Short, _ ByVal pDevMode As IntPtr) As Integer Declare Function DeviceCapabilities Lib "winspool.drv" Alias "DeviceCapabilitiesA" ( _ ByVal pDevice As String, _ ByVal pPort As String, _ ByVal fwCapability As Short, _ ByVal pOutput As String, _ ByVal pDevMode As IntPtr) As Integer Friend Structure PRINTER_DEFAULTS Public pDatatype As IntPtr Public pDevMode As IntPtr Public DesiredAccess As Integer End Structure <Serializable(), StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ Friend 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 <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ Friend Structure DevMode <VBFixedString(32), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _ Public dmDeviceName As String Public dmSpecVersion As Short Public dmDriverVersion As Short Public dmSize As Short Public dmDriverExtra As Short Public dmFields As Integer Public dmOrientation As Short Public dmPaperSize As Short Public dmPaperLength As Short Public dmPaperWidth As Short Public dmScale As Short Public dmCopies As Short Public dmDefaultSource As Short Public dmPrintQuality As Short Public dmColor As Short Public dmDuplex As Short Public dmYResolution As Short Public dmTTOption As Short Public dmCollate As Short <VBFixedString(32), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _ Public dmFormName As String Public dmUnusedPadding As Short Public dmBitsPerPel As Short Public dmPelsWidth As Integer Public dmPelsHeight As Integer Public dmDisplayFlags As Integer Public dmDisplayFrequency As Integer End Structure Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try ' バッファサイズを取り出す。 Dim nameCount As Integer nameCount = DeviceCapabilities("TEC B-858-R", String.Empty, DC_PAPERNAMES, vbNullString, IntPtr.Zero) ' 用紙名を取り出す。 Dim paperNames As String = New String(" "c, nameCount * 64) Dim paperIndex As Integer = 0 Dim hEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift-JIS") Dim paperName As String DeviceCapabilities("TEC B-858-R", String.Empty, DC_PAPERNAMES, paperNames, IntPtr.Zero) For i As Integer = 0 To nameCount - 1 paperName = hEncoding.GetString(hEncoding.GetBytes(paperNames), i * 64, 64) If paperName.Trim.CompareTo("USER") = 0 Then paperIndex = i Exit For End If Next ' バッファサイズを取り出す。 Dim numberCount As Integer numberCount = DeviceCapabilities("TEC B-858-R", String.Empty, DC_PAPERS, 0, IntPtr.Zero) ' 用紙番号を取り出す。 Dim paperNumbers(numberCount - 1) As Short DeviceCapabilities("TEC B-858-R", String.Empty, DC_PAPERS, paperNumbers, IntPtr.Zero) ' アクセス権を設定する。 Dim pDefault As New PRINTER_DEFAULTS With pDefault .DesiredAccess = PRINTER_ALL_ACCESS End With ' プリンターを開く。 Dim hPrinter As IntPtr Dim returnValue As Boolean returnValue = OpenPrinter("TEC B-858-R", _ hPrinter, pDefault) Debug.Print("OpenPrinter:" & returnValue) ' バッファサイズを取り出す。 Dim byteNeeded As Integer returnValue = GetPrinter(hPrinter, _ 2, IntPtr.Zero, 0, byteNeeded) ' メモリの割当て。 Dim pPrinter As IntPtr pPrinter = Marshal.AllocHGlobal(byteNeeded) ' プリンター情報を読み取りする。 returnValue = GetPrinter(hPrinter, _ 2, pPrinter, byteNeeded, byteNeeded) Debug.Print("GetPrinter:" & returnValue) ' 構造体にメモリのデータをコピーする。 Dim infoPrinter As PRINTER_INFO_2 Dim modeDev As DevMode infoPrinter = CType(Marshal.PtrToStructure(pPrinter, GetType(PRINTER_INFO_2)), PRINTER_INFO_2) modeDev = CType(Marshal.PtrToStructure(infoPrinter.pDevMode, GetType(DevMode)), DevMode) ' 用紙サイズを変更する。 modeDev.dmPaperSize = paperNumbers(paperIndex) ' メモリに構造体のデータをコピー する。 Marshal.StructureToPtr(modeDev, infoPrinter.pDevMode, True) Marshal.StructureToPtr(infoPrinter, pPrinter, True) ' プリンター情報を書き込みする。 returnValue = SetPrinter(hPrinter, _ 2, pPrinter, 0) Debug.Print("SetPrinter:" & returnValue) ' プリンターを閉じる。 returnValue = ClosePrinter(hPrinter) Debug.Print("ClosePrinter:" & returnValue) ' メモリを開放する。 Marshal.FreeHGlobal(pPrinter) Exit Sub Catch ex As Exception Debug.Print(ex.ToString) End Try End Sub End Class
|