Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Public Class PrnDrvClass
‘ Constants for DEVMODE
Dim CCHFORMNAME As Integer = 32
Dim CCHDEVICENAME As Integer = 32
‘ Constants for PRINTER_DEFAULTS.DesiredAccess
Dim PRINTER_ACCESS_ADMINISTER As Integer = &H4
Dim PRINTER_ACCESS_USE As Integer = &H8
Dim STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
Dim PRINTER_ALL_ACCESS As Integer = (STANDARD_RIGHTS_REQUIRED Or _
PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
‘ Constants for DocumentProperties() call
Dim DM_MODIFY As Integer = 8
‘ Dim DM_IN_BUFFER As Integer = DM_MODIFY
Dim DM_COPY As Integer = 2
‘ Dim DM_OUT_BUFFER As Integer = DM_COPY
‘ Custom constants for this sample’s SelectForm function
Dim FORM_NOT_SELECTED As Integer = 0
Dim FORM_SELECTED As Integer = 1
Dim FORM_ADDED As Integer = 2
‘ ユーザー定義の用紙を示す定数の宣言
Dim FORM_USER As Int32 = &H0&
‘/* orientation selections */
Dim DMORIENT_PORTRAIT As Short = 1
Dim DMORIENT_LANDSCAPE As Short = 2
Dim DMPAPER_USER As Integer = 256
‘/* field selection bits */
‘ Dim DM_ORIENTATION As Integer = &H1&
Dim DM_FORMNAME As Integer = &H10000
Dim DM_ORIENTATION As Integer = &H1
Dim DM_PAPERSIZE As Integer = &H2
Dim DM_PAPERLENGTH As Integer = &H4
Dim DM_PAPERWIDTH As Integer = &H8
Dim DM_SCALE As Integer = &H10
Dim DM_DEFAULTSOURCE As Integer = &H200
Dim DMPAPER_A3 As Short = 8 ‘ /* A3 297 x 420 mm */
Dim DMPAPER_A4 As Short = 9 ‘/* A4 210 x 297 mm */
Dim DMPAPER_A4SMALL As Short = 10 ‘/* A4 Small 210 x 297 mm */
Dim DMPAPER_A5 As Short = 11 ‘/* A5 148 x 210 mm */
Dim DMPAPER_B4 As Short = 12 ‘/* B4 (JIS) 250 x 354 */
Dim DMPAPER_B5 As Short = 13 ‘/* B5 (JIS) 182 x 257 mm */
‘DocumentProperties Use
Private Const DM_OUT_BUFFER As Short = 2
Private Const DM_PROMPT As Short = 4
Private Const DM_IN_PROMPT As Short = DM_PROMPT
Private Const DM_IN_BUFFER As Short = 8
‘幅と高さを定義する構造体の宣言
<StructLayout(LayoutKind.Sequential)> _
Public Structure SIZEL
Public cx As Int32
Public cy As Int32
End Structure
‘矩形を定義する構造体の宣言
<StructLayout(LayoutKind.Sequential)> _
Public Structure RECTL
Public left As Int32
Public top As Int32
Public right As Int32
Public bottom As Int32
End Structure
Public Structure PRINTER_DEFAULTS
‘ Dim pDatatype As String
‘ Dim pDevMode As IntPtr ‘ DEVMODE
‘ Dim DesiredAccess As Long
Dim pDatatype As IntPtr
Dim pDevMode As IntPtr ‘ DEVMODE
Dim DesiredAccess As Long
End Structure
‘FORM_INFO_1 (用紙情報を定義する構造体)
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure FORM_INFO_1
<MarshalAs(UnmanagedType.I4)> Public Flags As Int32
<MarshalAs(UnmanagedType.LPTStr)> Public pName As String
Public Size As SIZEL
Public ImageableArea As RECTL
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
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
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Class DEVMODE
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=&H20)> _
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
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=&H20)> _
Public dmFormName As String
Public dmLogPixels As Short
Public dmBitsPerPel As Integer
Public dmPelsWidth As Integer
Public dmPelsHeight As Integer
Public dmDisplayFlags As Integer
Public dmDisplayFrequency As Integer
Public dmICMMethod As Integer
Public dmICMIntent As Integer
Public dmMediaType As Integer
Public dmDitherType As Integer
Public dmICCManufacturer As Integer
Public dmICCModel As Integer
Public dmPanningWidth As Integer
Public dmPanningHeight As Integer
Public Overrides Function ToString() As String
Return String.Concat(New Object() {”[DEVMODE: dmDeviceName=", Me.dmDeviceName, ", dmSpecVersion=", Me.dmSpecVersion, ", dmDriverVersion=", Me.dmDriverVersion, ", dmSize=", Me.dmSize, ", dmDriverExtra=", Me.dmDriverExtra, ", dmFields=", Me.dmFields, ", dmOrientation=", Me.dmOrientation, ", dmPaperSize=", Me.dmPaperSize, ", dmPaperLength=", Me.dmPaperLength, ", dmPaperWidth=", Me.dmPaperWidth, ", dmScale=", Me.dmScale, ", dmCopies=", Me.dmCopies, ", dmDefaultSource=", Me.dmDefaultSource, ", dmPrintQuality=", Me.dmPrintQuality, ", dmColor=", Me.dmColor, ", dmDuplex=", Me.dmDuplex, ", dmYResolution=", Me.dmYResolution, ", dmTTOption=", Me.dmTTOption, ", dmCollate=", Me.dmCollate, ", dmFormName=", Me.dmFormName, ", dmLogPixels=", Me.dmLogPixels, ", dmBitsPerPel=", Me.dmBitsPerPel, ", dmPelsWidth=", Me.dmPelsWidth, ", dmPelsHeight=", Me.dmPelsHeight, ", dmDisplayFlags=", Me.dmDisplayFlags, ", dmDisplayFrequency=", Me.dmDisplayFrequency, ", dmICMMethod=", Me.dmICMMethod, ", dmICMIntent=", Me.dmICMIntent, ", dmMediaType=", Me.dmMediaType, ", dmDitherType=", Me.dmDitherType, ", dmICCManufacturer=", Me.dmICCManufacturer, ", dmICCModel=", Me.dmICCModel, ", dmPanningWidth=", Me.dmPanningWidth, ", dmPanningHeight=", Me.dmPanningHeight, "]“})
End Function
End Class
‘================================================================
‘
‘
‘
‘================================================================
‘Win32API
Private Declare Auto Function DocumentProperties Lib “winspool.drv” ( _
ByVal hwnd As IntPtr, _
ByVal hPrinter As IntPtr, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal pDeviceNameg As String, _
ByVal pDevModeOutput As IntPtr, _
ByVal pDevModeInput As IntPtr, _
ByVal fMode As Integer) As Int32
Private Declare Auto Function GlobalLock Lib “kernel32″ (ByVal hmem As IntPtr) As IntPtr
Private Declare Auto Function GlobalFree Lib “kernel32″ (ByVal hmem As IntPtr) As Int32
Private Declare Auto Function GlobalUnlock Lib “kernel32″ (ByVal hMem As IntPtr) As Int32
Private Declare Auto Function OpenPrinter Lib “winspool.drv” ( _
<MarshalAs(UnmanagedType.LPTStr)> ByVal pPrinterName As String, _
ByRef phPrinter As IntPtr, _
ByRef pDefault As PRINTER_DEFAULTS) As Int32
Private Declare Auto Function ClosePrinter Lib “winspool.drv” (ByVal hPrinter As IntPtr) As Int32
Private Declare Auto Function SetPrinter Lib “winspool.drv” ( _
ByVal hPrinter As IntPtr, _
ByVal Level As Integer, _
ByVal pForm As IntPtr, _
ByVal Command As Integer) As Boolean
Private Declare Auto Function GetPrinter Lib “winspool.drv” ( _
ByVal hPrinter As IntPtr, ByVal dwLevel As Integer, _
ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, _
ByRef pcbNeeded As Integer) As Boolean
Private Declare Auto Function AddForm Lib “winspool.drv” ( _
ByVal hPrinter As IntPtr, _
ByVal Level As Integer, _
ByVal pForm As IntPtr) As Integer
Private Declare Auto Function DeleteForm Lib “winspool.drv” (ByVal hPrinter As IntPtr, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal pName As String) As Integer
‘================================================================
‘ プリンター設定を指定された用紙サイズと名前で追加し
‘ デフォルト設定する
‘
‘ パラメータ
‘ hwnd :Wndハンドルを指定する
‘ printerName :設定したいプリンター名(コントロールパネルで設定済みのもの)
‘ paper :用紙名称 好きな名前を付ける、削除する際に必要
‘ width :用紙幅
‘ height :用紙高さ
‘
‘ 戻り値 なし
‘
‘
‘================================================================
Public Sub addPaper(ByVal hwnd As IntPtr, ByVal printerName As String, _
ByVal paper As String, ByVal width As Integer, ByVal height As Integer)
Dim ret As Integer
Dim hPrinter As IntPtr
Dim FormInfo1Level As Integer = 1
Dim FormInfo1 As FORM_INFO_1
Dim prtFormInfo1 As IntPtr
Dim pDevMode As DEVMODE
Dim _PrinterName As String = printerName
Dim pd As New PRINTER_DEFAULTS
Dim pPrinterDef As IntPtr = IntPtr.Zero
Dim pi2 As PRINTER_INFO_2
Dim pPrinterInfo As IntPtr = IntPtr.Zero
Try
pd.DesiredAccess = PRINTER_ALL_ACCESS
‘ プリンタのハンドルを取得
ret = OpenPrinter(printerName, hPrinter, pd)
If ret = 0 Or hPrinter.Equals(IntPtr.Zero) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
Return
End If
With FormInfo1
‘ 特性にユーザー定義を指定
.Flags = FORM_USER
‘ 用紙名を指定
.pName = paper
‘ 幅と高さを指定
.Size.cx = width
.Size.cy = height
‘印刷範囲を指定()
.ImageableArea.left = 50
.ImageableArea.top = 50
.ImageableArea.right = width – 50
.ImageableArea.bottom = height – 50
End With
prtFormInfo1 = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(FORM_INFO_1)))
Marshal.StructureToPtr(FormInfo1, prtFormInfo1, True)
‘ 用紙情報を追加
ret = AddForm(hPrinter, FormInfo1Level, prtFormInfo1)
Dim Struct1 As FORM_INFO_1
Struct1 = CType(Marshal.PtrToStructure(prtFormInfo1, GetType(FORM_INFO_1)), FORM_INFO_1)
‘=========================================
‘必要なバイト数を取得する
Dim needed As Integer
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, needed)
If needed <= 0 Then
Throw New Exception(”失敗しました。”)
End If
Debug.Print(”needed=” & needed)
‘メモリを割り当てる
pPrinterInfo = Marshal.AllocHGlobal(needed)
‘ Dim globalDevIn As IntPtr = GlobalLock(PtrDMIn)
‘プリンタ情報を取得する
Dim temp As Integer
If Not GetPrinter(hPrinter, 2, pPrinterInfo, needed, temp) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
Debug.Print(”temp=” & temp)
‘PRINTER_INFO_2型にマーシャリングする
pi2 = CType(Marshal.PtrToStructure( _
pPrinterInfo, GetType(PRINTER_INFO_2)), PRINTER_INFO_2)
‘ 構造体とポインターの変換 pi2.pDevMode
pDevMode = CType(Marshal.PtrToStructure(pi2.pDevMode, _
GetType(DEVMODE)), DEVMODE)
Debug.Print(”変更前=” & pDevMode.dmFormName)
Debug.Print(”変更前dmPaperWidth=” & pDevMode.dmPaperWidth)
Debug.Print(”変更前dmPaperLength=” & pDevMode.dmPaperLength)
pDevMode.dmFormName = paper & Chr(0) ‘ Must be NULL terminated!
‘ Set the dmFields bit flag to indicate what you are changing.
pDevMode.dmFields = DM_FORMNAME
Debug.Print(”変更後=” & pDevMode.dmFormName)
Debug.Print(”変更後dmPaperWidth=” & pDevMode.dmPaperWidth)
Debug.Print(”変更後dmPaperLength=” & pDevMode.dmPaperLength)
Marshal.StructureToPtr(pDevMode, pi2.pDevMode, False)
ret = DocumentProperties(hwnd, hPrinter, _PrinterName, pi2.pDevMode, _
pi2.pDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
Marshal.StructureToPtr(pi2, pPrinterInfo, False)
‘/* プリンタ情報のアップデート */
Dim b As Boolean = SetPrinter(hPrinter, 2, pPrinterInfo, 0)
If b = True Then
MsgBox(”プリンタードライバーの設定。”)
Else
Debug.Print(”err=” & Marshal.GetLastWin32Error())
‘ Dim LangOptions As Long
‘ LangOptions = &H0
‘ Dim buffer As String
‘ buffer = Space(256)
‘ lngReturn = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0&, Err.LastDllError, LangOptions, buffer, Len(buffer), 0&)
‘ MsgBox(”Could not set printer: ” & buffer)
MsgBox(Marshal.GetLastWin32Error())
‘ Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
If ret <= 0 Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
Catch ex As Exception
MessageBox.Show(ex.Message, “エラー”)
Finally
‘後始末をする
Marshal.FreeHGlobal(pPrinterInfo)
If Not hPrinter.Equals(IntPtr.Zero) Then
ClosePrinter(hPrinter)
End If
‘ Marshal.FreeHGlobal(prtFormInfo1)
End Try
End Sub
‘================================================================
‘ユーザー定義サイズ用紙削除
‘
‘ パラメータ
‘ printerName :設定したいプリンター名(コントロールパネルで設定済みのもの)
‘ paper :用紙名称 好きな名前を付ける、削除する際に必要
‘
‘ 戻り値 なし
‘
‘
‘================================================================
Public Sub DeletePaperForm(ByVal printerName As String, ByVal paper As String)
Dim ret As Integer
Dim hPrinter As IntPtr
Dim pd As New PRINTER_DEFAULTS
pd.DesiredAccess = PRINTER_ALL_ACCESS
Try
‘ プリンタのハンドルを取得
ret = OpenPrinter(printerName, hPrinter, pd)
If ret = 0 Or hPrinter.Equals(IntPtr.Zero) Then
Return
End If
‘ 用紙の削除
ret = DeleteForm(hPrinter, paper)
If ret <= 0 Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
MsgBox(”delete sucess”)
Catch ex As Exception
MessageBox.Show(ex.Message, “エラー”)
Finally
If Not hPrinter.Equals(IntPtr.Zero) Then
ClosePrinter(hPrinter)
End If
End Try
End Sub
End Class