16,549
社区成员
发帖
与我相关
我的任务
分享
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'PrintDocument对象作成
Dim pd As New System.Drawing.Printing.PrintDocument
'PrintController属性设定为StandardPrintController
pd.PrintController = _
New System.Drawing.Printing.StandardPrintController
'PrintPage事件处理器追加
AddHandler pd.PrintPage, AddressOf pd_PrintPage
'开始打印
pd.Print()
End Sub
Public Class LPTControl
<StructLayout(LayoutKind.Sequential)> _
Private Structure OVERLAPPED
Private Internal As Integer
Private InternalHigh As Integer
Private Offset As Integer
Private OffSetHigh As Integer
Private hEvent As Integer
End Structure
Private Shared <DllImport("kernel32.dll")> _
Function CreateFile(lpFileName As String, dwDesiredAccess As System.UInt32, dwShareMode As Integer, lpSecurityAttributes As Integer, dwCreationDisposition As Integer, dwFlagsAndAttributes As Integer, hTemplateFile As Integer) As Integer 'ToDo: Unsigned Integers not supported
Private Shared <DllImport("kernel32.dll")> _
Function WriteFile(hFile As Integer, lpBuffer() As Byte, nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByRef lpOverlapped As OVERLAPPED) As Boolean
Private Shared <DllImport("kernel32.dll")> _
Function CloseHandle(hObject As Integer) As Boolean
Private iHandle As Integer
Public Function Open() As Boolean
iHandle = CreateFile("LPT1", &H40000000, 0, 0, 3, 0, 0)
If iHandle <> - 1 Then
Return True
Else
Return False
End If
End Function
Public Overloads Function Write(ByVal Mystring As String) As Boolean
If iHandle <> -1 Then
Dim x As New OVERLAPPED()
Dim i As Integer = 0
Dim mybyte As Byte() = System.Text.Encoding.Default.GetBytes(Mystring)
Dim b As Boolean = WriteFile(iHandle, mybyte, mybyte.Length, i, x)
Return b
Else
Throw New Exception("不能连接到打印机!")
End If
End Function
Public Overloads Function Write(ByVal mybyte() As Byte) As Boolean
If iHandle <> -1 Then
Dim x As New OVERLAPPED()
Dim i As Integer = 0
WriteFile(iHandle, mybyte, mybyte.Length, i, x)
Return True
Else
Throw New Exception("不能连接到打印机!")
End If
End Function
Public Function Close() As Boolean
Return CloseHandle(iHandle)
End Function
End Class